python - 多子弹pygame

标签 python pygame

我知道有几个关于此的主题,但我仍然不知道如何让我的机器人发射多发子弹...我想按下我的空格按钮并发射多发子弹,但我仍然只能发射一颗子弹。我知道我的代码很困惑,所以我很抱歉。感谢您的帮助!

https://github.com/20nicolas/Game.git

这是我的代码:

import pygame as py
import os

py.init ()

screen = py.display.set_mode ((800,600))



bg = py.image.load('game-assets-game-background-sidescroller.png')
clock = py.time.Clock ()


idle = [py.image.load(os.path.join('player','Idle (1).png')),py.image.load(os.path.join('player','Idle (2).png')),py.image.load(os.path.join('player','Idle (3).png')),py.image.load(os.path.join('player','Idle (4).png')),py.image.load(os.path.join('player','Idle (5).png')),py.image.load(os.path.join('player','Idle (6).png')),py.image.load(os.path.join('player','Idle (7).png')),py.image.load(os.path.join('player','Idle (8).png')),py.image.load(os.path.join('player','Idle (9).png')),py.image.load(os.path.join('player','Idle (10).png'))]
run_right = [py.image.load(os.path.join('player','Run (1).png')),py.image.load(os.path.join('player','Run (2).png')),py.image.load(os.path.join('player','Run (3).png')),py.image.load(os.path.join('player','Run (4).png')),py.image.load(os.path.join('player','Run (5).png')),py.image.load(os.path.join('player','Run (6).png')),py.image.load(os.path.join('player','Run (7).png')),py.image.load(os.path.join('player','Run (8).png'))]
jump = [py.image.load(os.path.join('player','Jump (1).png')),py.image.load(os.path.join('player','Jump (2).png')),py.image.load(os.path.join('player','Jump (3).png')),py.image.load(os.path.join('player','Jump (4).png')),py.image.load(os.path.join('player','Jump (5).png')),py.image.load(os.path.join('player','Jump (6).png')),py.image.load(os.path.join('player','Jump (7).png')),py.image.load(os.path.join('player','Jump (8).png')),py.image.load(os.path.join('player','Jump (9).png')),py.image.load(os.path.join('player','Jump (10).png'))]
shoot_idle = [py.image.load(os.path.join('player','Shoot (1).png')),py.image.load(os.path.join('player','Shoot (2).png')),py.image.load(os.path.join('player','Shoot (3).png')),py.image.load(os.path.join('player','Shoot (4).png'))]

class player(object):
    def __init__(self,x,y,width,lenght):
        self.x = x
        self.y = y
        self.width = width
        self.lenght = lenght
        self.vel = 5
        self.right = False
        self.left = False
        self.standing = True
        self.idlecount = 0
        self.runcount = 0
        self.jumping = False
        self.jumpcount = 14
        self.direction = 1
        self.jumpingcount = 0
        self.shooting = False
        self.shootingcount = 0

    def draw (self,screen):
        if self.idlecount + 1 >= 30:
            self.idlecount = 0

        if self.runcount + 1 >= 24:
            self.runcount = 0

        if self.jumpingcount + 1 >= 30:
            self.jumpingcount = 0

        if self.shootingcount + 1 >= 9:
            self.shootingcount = 0

        if not (self.jumping):
            if not (self.standing):

                if self.right:
                    screen.blit (run_right[self.runcount//3],(self.x,self.y))
                    self.runcount += 1

                elif self.left:
                    screen.blit (run_left[self.runcount//3],(self.x,self.y))
                    self.runcount += 1

            else:
                if self.shooting:
                    if self.direction == 1:
                        screen.blit (shoot_idle[self.shootingcount//2],(self.x,self.y))
                        self.shootingcount += 1

                    elif self.direction == -1:
                        screen.blit (shoot_idle2[self.shootingcount//2],(self.x,self.y))
                        self.shootingcount += 1

                elif self.direction == 1:
                    screen.blit (idle[self.idlecount//3],(self.x,self.y))
                    self.idlecount += 1

                elif self.direction == -1:
                    screen.blit (idle2[self.idlecount//3],(self.x,self.y))
                    self.idlecount += 1

        else:
            if self.direction == 1:
                screen.blit (jump[self.jumpingcount//3],(self.x,self.y))
                self.jumpingcount += 1
                self.runcount = 0

            elif self.direction == -1:
                screen.blit (jump2[self.jumpingcount//3],(self.x,self.y))
                self.jumpingcount += 1
                self.runcount = 0



pows = [py.image.load(os.path.join('player','Bullet_000.png')),py.image.load(os.path.join('player','Bullet_001.png')),py.image.load(os.path.join('player','Bullet_002.png')),py.image.load(os.path.join('player','Bullet_003.png')),py.image.load(os.path.join('player','Bullet_004.png'))]

class bullets (object):
    def __init__ (self,width,lenght):
        self.x = 0
        self.y = 0
        self.lenght = lenght
        self.width = width
        self.vel = 10
        self.shoot = False
        self.shootcount = 0

    def draw (self,screen):
        if self.shootcount + 1 >= 15:
            self.shootcount = 0

        if man.direction == 1:
            if self.shoot:
                screen.blit (pows[self.shootcount//3],(self.x,self.y))
                self.shootcount += 1
                self.x += self.vel

        elif man.direction == -1:
            if self.shoot:
                screen.blit (pows2[self.shootcount//3],(self.x,self.y))
                self.shootcount += 1
                self.x -= self.vel

def drawGameScreen ():
    screen.blit(bg,(0,0))
    man.draw (screen)
    bullet.draw (screen)

    py.display.update ()

这是主循环:

    ##main loop

man = player (100,430,100,100)
bullet = bullets (50,50)
game = True

idle = [(py.transform.scale(idle[0],(man.width,man.lenght))),(py.transform.scale(idle[1],(man.width,man.lenght))),(py.transform.scale(idle[2],(man.width,man.lenght))),(py.transform.scale(idle[3],(man.width,man.lenght))),(py.transform.scale(idle[4],(man.width,man.lenght))),(py.transform.scale(idle[5],(man.width,man.lenght))),(py.transform.scale(idle[6],(man.width,man.lenght))),(py.transform.scale(idle[7],(man.width,man.lenght))),(py.transform.scale(idle[8],(man.width,man.lenght))),(py.transform.scale(idle[9],(man.width,man.lenght)))]
run_right = [(py.transform.scale(run_right[0],(man.width,man.lenght))),(py.transform.scale(run_right[1],(man.width,man.lenght))),(py.transform.scale(run_right[2],(man.width,man.lenght))),(py.transform.scale(run_right[3],(man.width,man.lenght))),(py.transform.scale(run_right[4],(man.width,man.lenght))),(py.transform.scale(run_right[5],(man.width,man.lenght))),(py.transform.scale(run_right[6],(man.width,man.lenght))),(py.transform.scale(run_right[7],(man.width,man.lenght)))]
jump = [(py.transform.scale(jump[0],(man.width,man.lenght))),(py.transform.scale(jump[1],(man.width,man.lenght))),(py.transform.scale(jump[2],(man.width,man.lenght))),(py.transform.scale(jump[3],(man.width,man.lenght))),(py.transform.scale(jump[4],(man.width,man.lenght))),(py.transform.scale(jump[5],(man.width,man.lenght))),(py.transform.scale(jump[6],(man.width,man.lenght))),(py.transform.scale(jump[7],(man.width,man.lenght))),(py.transform.scale(jump[8],(man.width,man.lenght))),(py.transform.scale(jump[9],(man.width,man.lenght)))]
run_left = [(py.transform.flip(run_right[0],True,False)),(py.transform.flip(run_right[1],True,False)),(py.transform.flip(run_right[2],True,False)),(py.transform.flip(run_right[3],True,False)),(py.transform.flip(run_right[4],True,False)),(py.transform.flip(run_right[5],True,False)),(py.transform.flip(run_right[6],True,False)),(py.transform.flip(run_right[7],True,False))]
idle2 = [(py.transform.flip(idle[0],True,False)),(py.transform.flip(idle[1],True,False)),(py.transform.flip(idle[2],True,False)),(py.transform.flip(idle[3],True,False)),(py.transform.flip(idle[4],True,False)),(py.transform.flip(idle[5],True,False)),(py.transform.flip(idle[6],True,False)),(py.transform.flip(idle[7],True,False)),(py.transform.flip(idle[8],True,False)),(py.transform.flip(idle[9],True,False))]
jump2 = [(py.transform.flip(jump[0],True,False)),(py.transform.flip(jump[1],True,False)),(py.transform.flip(jump[2],True,False)),(py.transform.flip(jump[3],True,False)),(py.transform.flip(jump[4],True,False)),(py.transform.flip(jump[5],True,False)),(py.transform.flip(jump[6],True,False)),(py.transform.flip(jump[7],True,False)),(py.transform.flip(jump[8],True,False)),(py.transform.flip(jump[9],True,False))]
shoot_idle = [(py.transform.scale(shoot_idle[0],(man.width,man.lenght))),(py.transform.scale(shoot_idle[1],(man.width,man.lenght))),(py.transform.scale(shoot_idle[2],(man.width,man.lenght))),(py.transform.scale(shoot_idle[3],(man.width,man.lenght)))]
shoot_idle2 = [(py.transform.flip(shoot_idle[0],True,False)),(py.transform.flip(shoot_idle[1],True,False)),(py.transform.flip(shoot_idle[2],True,False)),(py.transform.flip(shoot_idle[3],True,False))]
pows = [(py.transform.scale(pows[0],(bullet.width,bullet.lenght))),(py.transform.scale(pows[1],(bullet.width,bullet.lenght))),(py.transform.scale(pows[2],(bullet.width,bullet.lenght))),(py.transform.scale(pows[3],(bullet.width,bullet.lenght))),(py.transform.scale(pows[4],(bullet.width,bullet.lenght)))]
pows2 = [(py.transform.flip(pows[0],True,False)),(py.transform.flip(pows[1],True,False)),(py.transform.flip(pows[2],True,False)),(py.transform.flip(pows[3],True,False)),(py.transform.flip(pows[4],True,False))]


while game:
    clock.tick (30)

    for event in py.event.get():
        if event == py.QUIT:
            game = False

    keys = py.key.get_pressed ()



    if keys[py.K_RIGHT] and man.x <= 700:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
        man.idlecount = 0
        man.direction = 1

    elif keys[py.K_LEFT] and man.x >= 0:
        man.x -= man.vel
        man.right = False
        man.left = True
        man.standing = False
        man.idlecount = 0
        man.direction = -1

    elif keys [py.K_SPACE]:
        if man.direction == 1:
            bullet.x = man.x + 75
            bullet.y = man.y + 20
            man.shooting = True
            bullet.shoot = True

        elif man.direction == -1:
            bullet.x = man.x - 25
            bullet.y = man.y + 20
            man.shooting = True
            bullet.shoot = True


    else:
        man.standing = True
        man.shooting = False

    if not(man.jumping):
        if keys[py.K_UP]:
            man.jumping = True
            man.right = False
            man.left = False
            man.standing = False
            man.walkcount = 0
    else:
        if man.jumpcount >= -14:
            neg = 1
            if man.jumpcount < 0:
                neg = -1
            man.y -= (man.jumpcount ** 2) * 0.2 * neg
            man.jumpcount -= 1
        else:
            man.jumping = False
            man.jumpcount = 14


    drawGameScreen ()

最佳答案

好吧,在没有任何图像的情况下运行该代码会很困难,但从看起来你需要一种更好的方法来跟踪和更新项目符号。例如,玩家类可以具有:

self.bullets = []

然后每当您按空格键时,您都会添加一个新项目符号:

new_bullet = Bullet()
# Set up x and y of bullet and such (could add them to the constructor)
man.bullets.append(Bullet())

然后,每当您需要更新项目符号或绘制它们时:

for each bullet in man.bullets:
    bullet.x += .....
    bulett.y += .....
    # any additional updates and drawing and such

关于python - 多子弹pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53175956/

相关文章:

python - 调用 np.array(data) 时给出 MemoryError 的大而稀疏的列表

python - 计算自上次刻度以来的 x 和 y 坐标变化

python - 在矩阵中生成数独谜题而不是绘制 GUI

python - 在不阻塞 Pygame UI 的情况下录制声音

python - 数据库列不存在

python - 指纹疤痕/折痕产生器

python - 如果匹配则返回匹配

Python opencv 排序轮廓

python - 集成 Rails 和 Python

python - 我的 NPC 不会追赶我的玩家或对碰撞使用react