python遍历子弹数组 改变子弹坐标 超出界面移除

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值