python - Pygame 多颗子弹不产生

标签 python python-3.x pygame bullet

所以我是 pygame 的新手,正在编写我的第一个项目 - 横向卷轴射击游戏。我遇到的问题是我的子弹:当我按空格键时,一些子弹会出现,但有时什么也没有发生,并且当我跳跃时不会产生子弹。不太确定如何解决这个问题 - 任何想法将不胜感激。 代码如下:

import pygame
import math, random, sys, pygame.mixer
from pygame.locals import *
pygame.init()
pygame.mixer.pre_init(44100, -16, 2, 8192)
pygame.mixer.init()
jump = False
jump_offset = 0
jump_height = 250

k = pygame.key.get_pressed()

def events():
        for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                        pygame.quit()
                        sys.exit()

def do_jumping():
        global jump_height
        global jump
        global jump_offset
        if jump:
                jump_offset += 3

                if jump_offset >= jump_height:
                        jump = False
        elif jump_offset > 0 and jump == False:
                jump_offset -= 3

#Defining colours
BLACK = (   0,   0,   0)
WHITE = ( 255, 255, 255)

#Window Settings
w = 1280
h = 720
half_w = w /2
half_h = h /2
AREA = w*h

#Initialising the window
pygame.init()
display = pygame.display.set_mode((w,h)) #Sets the size of the window
pygame.display.set_caption("Cattleman") #Sets the title of the window
Clock = pygame.time.Clock() #clockspeed for the game ie. 60fps
FPS = 600

#pygame.mouse.set_visible(True) #Allows the mouse to be shown in the game window.

background = pygame.image.load("background.png").convert()
backgroundWidth, backgroundHeight = background.get_rect().size

stageWidth = backgroundWidth*2 #sets the area which the player can move in
stagePosX = 0 #Records position of stage as the player moves

startScrollPosX = half_w

circleRadius = 25
circlePosX = circleRadius

playerPosX = circleRadius
playerPosY = 602
playerVelocityX = 0

playersprite = pygame.image.load("player_spriteR2.png").convert_alpha()
playersprite = pygame.transform.scale(playersprite, (130,130))

bullets = []

bulletSprite = pygame.image.load("Bullet1.png").convert_alpha()
bulletSprite = pygame.transform.scale(bulletSprite, (20,10))

#Sounds
#gunSounds = ["pew1.wav", "pew2.wav", "pew3.wav", "pew4.wav"]
#SOUNDS
shot = pygame.mixer.Sound("pew1.wav")
#------------------------MAIN PROGRAM LOOP------------------------#
while True:
    events()
    do_jumping()
    k = pygame.key.get_pressed()
    if k[K_RIGHT]:
        playerVelocityX = 2 #Moves the player right
        playersprite = pygame.image.load("player_spriteR2.png").convert_alpha()
        playersprite = pygame.transform.scale(playersprite, (130,130))
    if k[K_LEFT]:
        playerVelocityX = -2 #Moves the player left
        playersprite = pygame.image.load("player_spriteL2.png").convert_alpha()
        playersprite = pygame.transform.scale(playersprite, (130,130))
    if k[K_UP] and jump == False and jump_offset == 0:
            jump = True
    if not k[K_RIGHT] and not k[K_LEFT]:
            playerVelocityX = 0 #If no input detected, the player does not move
    if k[K_SPACE]:
            for event in pygame.event.get():
                bullets.append([circlePosX-100, playerPosY-20])
            shot.play()

    playerPosX += playerVelocityX
    if playerPosX > stageWidth - circleRadius-25: playerPosX = stageWidth - circleRadius-25 #Checks if the player trie to go past the right boundary
    if playerPosX < circleRadius+55:playerPosX = circleRadius+55 #Checks if the player tries to go past the left boundary
    if playerPosX < startScrollPosX: circlePosX = playerPosX
    elif playerPosX > stageWidth - startScrollPosX: circlePosX = playerPosX - stageWidth + w
    else:
        circlePosX = startScrollPosX
        stagePosX += -playerVelocityX

    for b in range(len(bullets)):
            bullets[b][0] -= 3

    for bullet in bullets[:]:
            if bullet[0] < 0:
                    bullets.remove(bullet)

    rel_x = stagePosX % backgroundWidth
    display.blit(background,(rel_x - backgroundWidth, 0))
    if rel_x < w:
            display.blit(background, (rel_x, 0))

    for bullet in bullets:
            display.blit(bulletSprite, pygame.Rect(bullet[0], bullet[1], 0, 0,))

    #pygame.draw.circle(display,WHITE, (int(circlePosX),playerPosY - jump_offset), circleRadius, 0)
    display.blit(playersprite, (int(circlePosX-80),playerPosY-100 - jump_offset))

    pygame.display.update()
    Clock.tick(FPS)
    display.fill(BLACK)

最佳答案

我在代码中做到了这一点,留下了多个类似于子弹的球:

class Bullet():

def __init__(self, x, y):
    # self.image = pygame.image.load("SingleBullet.png")
    self.image = pygame.image.load("ball.png")
    self.image = pygame.transform.scale(self.image, (25, 25))

    self.rect = self.image.get_rect()
    self.rect.centerx = x
    self.rect.centery = y

    self.is_alive = True

# --------------------

def update(self):
    self.rect.y -= 15

    if self.rect.y < 0:
        self.is_alive = False

# --------------------

def draw(self, screen):
    screen.blit(self.image, self.rect.topleft)

获取键盘:

 def event_handler(self, event):

    if event.type == KEYDOWN:
        if event.key == K_LEFT:
            self.move_x = -5
        elif event.key == K_RIGHT:
            self.move_x = 5
        elif event.key == K_SPACE:
            if len(self.shots) < self.max_shots:
                self.shots.append(Bullet(self.rect.centerx, self.rect.top))

    if event.type == KEYUP:
        if event.key in (K_LEFT, K_RIGHT):
            self.move_x = 0

关于python - Pygame 多颗子弹不产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460228/

相关文章:

python-3.x - 如何在大数据集(Train、Dev、Test)上使用 CountVectorizer 和 TfidfTransformer?

python - 在python中将罗马数字转换为整数

python - 在pygame中用鼠标移动可缩放矩形

python - Pandas DataFrame Group 和 Rollup 一次操作

单进程的Python多处理速度

python - 如何使用 Conda 安装 scikit-multilearn

python - 在 PYGAME 中如何同时移动多个对象

python - wxPython 对话框 : "Enter" keyboard button would not "ok" the dialog

python - while 循环覆盖结果

python - AttributeError: 'pygame.math.Vector2' 对象没有属性