无法弄清楚为什么我的图像不会在pygame中移动

时间:2017-11-15 00:51:02

标签: python pygame

我只是搞乱了Pygame而且我无法看到我做错了什么让红色圆圈用箭头键移动。我无法判断它是否在我的主循环中。我也无法通过Pygame找到关于sprite或循环动画的很多教程。例如,如果我想制作一个方形振荡,例如移动平台,我该怎么做?

import pygame import time ## event handling varibles player_x = 0 player_y = 0 x = 250 y = 250 ## screen display display_width = 500 display_height = 500 pygame.init() game_screen = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption("test") # player def player(): pygame.draw.circle(game_screen,red,(x,y),15) # colors white = (255,255,255) red = (255,0,0) black = (0,0,0) ### main loop dead = False while dead != True: for event in pygame.event.get(): if event.type == pygame.QUIT: dead = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x = -1 elif event.key == pygame.K_RIGHT: player_x = +1 if event.key == pygame.K_UP: player_y = +1 elif event.key == pygame.K_DOWN: player_y = -1 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or pygame.K_RIGHT: player_x = 0 if event.key == pygame.K_UP or pygame.K_DOWN: player_y = 0 game_screen.fill(black) player() pygame.display.update() x -= player_x y -= player_y pygame.quit() quit()

1 个答案:

答案 0 :(得分:1)

  1. 你的压抑都搞砸了。
  2. 除非事件是QUIT,否则您的代码将无法执行任何操作...然后退出。

    1. 您的布尔逻辑错误。
    2. 这不是正确的语法event.key == pygame.K_UP or pygame.K_DOWN。此处的优先顺序如下(event.key == pygame.K_UP) or (K_DOWN)。由于K_DOWN是真实的,它总是正确的,因此整个陈述总是正确的。

      我认为你的意思是:event.key == pygame.K_UP or event.key == pygame.K_DOWN

      1. 最后,它不会'按照你的意愿继续前进。
      2. 只有在队列中有事件时才会移动。您可以通过生成事件使其继续移动。也许有一个事件计时器。

        这是一个固定版本,希望这会有所帮助:

        import pygame
        import time
        
        ## event handling varibles
        player_x = 0
        player_y = 0
        x = 250
        y = 250
        
        ## screen display
        display_width = 500
        display_height = 500
        pygame.init()
        game_screen = pygame.display.set_mode((display_width,display_height))
        pygame.display.set_caption("test")
        
        # player
        def player(game_screen, red, point):   # Use parameters not globals
            pygame.draw.circle(game_screen, red, point, 15)
        
        # colors
        white = (255,255,255)
        red = (255,0,0)
        black = (0,0,0)
        
        ### main loop
        pygame.time.set_timer(pygame.USEREVENT, 1)  # 1 per second
        
        dead = False
        while not dead:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    dead = True
                elif event.type == pygame.USEREVENT:
                    pygame.time.set_timer(pygame.USEREVENT, 1)  # Set another timer for another 1 second
        
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        player_x = +10
                    elif event.key == pygame.K_RIGHT:
                        player_x = -10
                    elif event.key == pygame.K_UP:
                        player_y = +10
                    elif event.key == pygame.K_DOWN:
                        player_y = -10
                elif event.type == pygame.KEYUP:
                    if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:
                        player_x = 0
                    elif event.key in [pygame.K_UP, pygame.K_DOWN]:
                        player_y = 0
                game_screen.fill(black)
                player(game_screen,red,(x,y))
                pygame.display.update()
                x -= player_x
                y -= player_y
        
        pygame.quit()