多个子弹pygame

时间:2018-11-06 16:25:46

标签: python pygame

我知道有几个主题,但是我仍然不知道如何使我的机器人发射多发子弹...我想按我的太空按钮并发射多发子弹,但是我仍然只能发射一发子弹。我知道我的代码很乱,很抱歉。感谢您的帮助!

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

这是我的代码:

import matplotlib.pyplot as plt
from numba import jit
import math

@jit
def weird_increment_loop(n):
    i = 1
    j = 0
    iterations = 0
    while i <= n:
        j = n // (n // i)
        i = j + 1
        iterations = iterations + 1

    return iterations

iterations = []
func_2sqrt = []
domain = range(0,1000000001,1000000)
for n in domain:
    iterations.append(weird_increment_loop(n))
    func_2sqrt.append(math.sqrt(n)*2)

plt.plot(domain,iterations)
plt.plot(domain,func_2sqrt)
plt.xlabel("n")
plt.ylabel("iterations(n) and 2*sqrt(n)")
plt.show()

这是主循环:

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 ()

1 个答案:

答案 0 :(得分:0)

好吧,在没有任何图像的情况下运行该代码将很困难,但是从外观上看,您需要一种更好的跟踪和更新项目符号的方法。例如,播放器类可能具有:

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