python - Pygame - 按退格键更改 Sprite 颜色

标签 python pygame

我正在尝试制作一个游戏,我希望用户能够在按退格键时更改 Sprite 的颜色。截至目前,我绘制的内容没有出现在屏幕上,我不知道为什么。欢迎任何详细的答案,解释如何以及为什么改变 Sprite 的颜色: 这是我的代码:

import pygame

pygame.init()

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
width = 800
height = 600
FPS = 100
font = pygame.font.SysFont(None, 25)

gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()

def message_to_screen(msg, color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [width/2, height/2])

def gameloop():
    exitGame = False
    lead_x = width/2
    lead_y = height/2
    lead_x_change = 0
    lead_y_change = 0
    block_size = 10
    velocity = 0.1
    rectangle = pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])

    while not exitGame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exitGame = True
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_w:
                    lead_y_change += -velocity
                    lead_x_change = 0
                if event.key == pygame.K_s:
                    lead_y_change += velocity
                    lead_x_change = 0
                if event.key == pygame.K_a:
                    lead_x_change += -velocity
                    lead_y_change = 0
                if event.key == pygame.K_d:
                    lead_x_change += velocity
                    lead_y_change = 0
                if event.key == pygame.K_BACKSPACE:
                    rectangle.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_d:
                   lead_x_change = 0
                if event.key == pygame.K_w or event.key == pygame.K_s:
                   lead_y_change = 0


    lead_x += lead_x_change
    lead_y += lead_y_change
    gameDisplay.fill(white)
    pygame.display.update()
    clock.tick(FPS)
    gameloop()

最佳答案

首先,将渲染函数按适当的顺序放置,并将它们放入 while 循环中:

gameDisplay.fill(white)  # Display fill comes first
pygame.draw.rect(gameDisplay, black, rectangle)  # Then draw your objects
pygame.display.flip()  # Update the screen

然后,正确定义 pygame.Rect 对象

rectangle = pygame.Rect(lead_x, lead_y, block_size, block_size)

这就是您可能想要找到的内容(经过我的一些修复):

import pygame
pygame.init()

# Constants
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WIDTH = 800
HEIGHT = 600
FPS = 100
FONT = pygame.font.SysFont(None, 25)
BLOCK_SIZE = 10
VELOCITY = 10

gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()

def message_to_screen(msg, color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [width/2, height/2])

def gameloop():
    exitGame = False
    x = WIDTH / 2
    y = HEIGHT / 2
    rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
    rectangle_color = BLACK

    while not exitGame:
        clock.tick(FPS)

        # Pygame events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exitGame = True
                pygame.quit()
                quit()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    y -= VELOCITY
                if event.key == pygame.K_s:
                    y += VELOCITY
                if event.key == pygame.K_a:
                    x -= VELOCITY
                if event.key == pygame.K_d:
                    x += VELOCITY
                if event.key == pygame.K_BACKSPACE:
                    rectangle_color = RED if rectangle_color == BLACK else BLACK

        # Update rect coords 
        rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)

        # Render everything
        gameDisplay.fill(WHITE)
        pygame.draw.rect(gameDisplay, rectangle_color, rectangle)
        pygame.display.flip()

gameloop()

关于python - Pygame - 按退格键更改 Sprite 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066935/

相关文章:

python - Pyinstaller EXE 在启动时崩溃

python - 如何使用pygame.surface.scroll()?

c++ - scons:对象、源代码、可执行文件,都在单独的目录中

python - 使用 libclang 在 python 中解析 C 但生成错误的 AST

Python 错误无法从空轴中执行非空取值

python - Pygame 中的 NameError - 未定义全局名称 "name"

python - 如何使用 PyGame 更改标签文本而不重叠?

python - Django Rest 框架中的多部分解析器

python - 当一个列表或另一个列表与第三个列表相同时,如何使程序停止?

python-3.x - 使用 ALSA 在 RaspBerry Pi 4+ 上使用 PyGame 的音频 cdrom 没有声音