import sys, pygame, pygame.mixer
from pygame.locals import *
class zidan:
def __init__(self):
self.x=self
self.y=self
def hua(self):
bulletpicture = pygame.image.load("player_car.png").convert_alpha()
screen.blit(bulletpicture, (self.x, self.y))
pygame.init()
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
bullets = []
background = pygame.image.load("stripes.png").convert()
background=pygame.transform.scale(background,(800,600))
ship = pygame.image.load("人.png").convert_alpha()
ship = pygame.transform.scale(ship, (64, 64))
bulletpicture = pygame.image.load("player_car.png").convert_alpha()
#shot = pygame.mixer.Sound("shot.wav")
#soundin = pygame.mixer.Sound("sound.wav")
#soundin.play()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
print([event.pos[0]-32])
#shot.play()
zd=zidan()
zd.x=int(event.pos[0]-32)
zd.y=500
#print([event.pos[0]-32, 500])
bullets.append(zd) # 在数组中加入鼠标点击的xy坐标
clock.tick(60)
mx, my = pygame.mouse.get_pos()
for b in bullets: # 遍历数组
b.x -= 10 # 当前位置数据x-10
# print(bullets[b][0])
# Iterate over a slice copy if you want to mutate a list.
# for bullet in bullets: # 查找在数组中的数据,如果x小于0移除出数组
#for z in bullets:
#z.x=z.x+10
screen.blit(background, (0, 0))
for bullet in bullets: # 遍历数组中的数据
if bullet.x < 0:
bullets.remove(bullet)
else:
bullet.hua()
# print(pygame.Rect(bullet[0], bullet[1], 0, 0))
screen.blit(ship, (mx-32, 500))
pygame.display.flip()
python遍历子弹数组 改变子弹坐标 超出界面移除
于 2022-01-21 18:37:05 首次发布