【问题标题】:Pygame not bliting images when pressing key [duplicate]按键时Pygame不闪烁图像[重复]
【发布时间】:2021-04-05 13:10:47
【问题描述】:

所以,我正在尝试制作游戏,并且正在研究基础知识,我试图弄清楚为什么当我按下 0 键时,它会打印“a”,但是画面不变。按一个也是一样。

import pygame
from pygame.display import mode_ok

pygame.init()
size = width, height = 1000, 800
speed = [2, 2]
black = 1, 2, 0
screen = pygame.display.set_mode(size)
mode_ok(size, flags=1, depth=5, display=0)
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

pygame.display.set_caption('Game')
# image database
pic = pygame.image.load(r"intro_ball.gif")
pic2 = pygame.image.load("poeple.png")
pic3 = pygame.image.load("frame_04_delay-0.1s.gif")
pic4 = pygame.image.load("frame_17_delay-0.1s.gif")
screen.blit(pygame.transform.scale(pic, (1000, 800)), (0, 0))
screen.blit(pic2, (500, 400))
pygame.display.flip()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                screen.blit(pic3, (0, 0))
                print("a")
            if event.key == pygame.K_1:
                screen.blit(pygame.transform.scale(pic4, (1000, 800)), (0, 0))
                print("B")
    pygame.event.pump()


如果有人能提供帮助,将不胜感激。

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    事件循环仅在事件发生时运行一次。在事件循环中绘制图像时,它仅在短时间内可见。如果要永久绘制图片,则需要在应用程序循环中进行绘制。
    将当前图片设置为current_pic 变量。事件发生时更改变量。在应用循环中不断绘制current_pic变量所指示的图片:

    current_pic = pygame.transform.scale(pic, (1000, 800))
    
    clock = pygame.time.Clock()
    run = True
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_0:
                    current_pic = pic3
                    print("a")
                if event.key == pygame.K_1:
                    current_pic = pygame.transform.scale(pic4, (1000, 800))
                    print("B")
        
        screen.blit(current_pic, (0, 0))
        screen.blit(pic2, (500, 400))
        pygame.display.flip()
    
    pygame.quit()
    

    典型的 PyGame 应用程序循环必须:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多