Python游戏射击

2024-03-29 09:59:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用另一个rect生成一个rect,但是当我调用这个函数时,什么也没有发生。我不明白为什么。我用速度变量改变了y坐标,但还是什么都没发生。这是我的密码: 任何帮助都将不胜感激

 import pygame

pygame.init()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
width, height = 800, 600
gameDisplay = pygame.display.set_mode((width, height))
velocity = 0.2
toggle_flag = 1

class App():
    def __init__(self):
        pass
    def fill_screen(self, color):
        self.color = color
        gameDisplay.fill(self.color)
    def draw_player(self, color, lead_x, lead_y, block_size):
        self.color = color
        self.lead_x = lead_x
        self.lead_y = lead_y
        self.block_size = block_size
        pygame.draw.rect(gameDisplay, self.color, [self.lead_x, self.lead_y, self.block_size, self.block_size])
class Player():
    lead_x = width/2
    lead_y = height/2
    block_size = 10
    def __init__(self):
        pass
    def shoot_player(self, lead_x, lead_y, block_size):
        self.lead_x = lead_x
        self.lead_y = lead_y
        self.block_size = block_size
        lead_y2 = self.lead_y
        pygame.draw.rect(gameDisplay, RED, [self.lead_x, lead_y2, self.block_size, self.block_size])
        lead_y2 += -velocity


exitGame = False
app = App()
player = Player()

while not exitGame:
    app.fill_screen(WHITE)
    app.draw_player(BLACK, width/2, height/2, 10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitGame = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot_player(width/2, height/2, 2)
    pygame.display.update()


pygame.quit()
quit()

Tags: rectselfeventsizeinitdefwidthblock