无法在Pong游戏

时间:2018-02-12 20:49:28

标签: python pygame 2d-games pygame-clock

import pygame, sys

pygame.init()

display_width = 800
display_height = 500

white = (255, 255, 255)

display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()

platform_y = display_height * 0.38

def platform(color, x, y):
    global platform_y

    pygame.draw.rect(display, color, (x, y, 25, 100))

    pygame.display.update()
    clock.tick(80)

def ball():
    pygame.draw.circle(display, white, (400, 250), 12)
    pygame.display.update()

def game():
    global platform_y
    y_change = 0
    while True:
        platform(white, 100, platform_y)
        platform(white, 675, platform_y)
        ball()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                sys.exit(0)

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change -= 2
                if event.key == pygame.K_DOWN:
                    y_change += 2

            if event.type == pygame.KEYUP:
                    if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                        y_change = 0

        platform_y += y_change
        pygame.display.update()
        clock.tick(80)

if __name__ == '__main__':
    game()

我想这不是最好的代码,但是我一直在搞乱pygame并且在我必须创建Pong时已经到了这一点,虽然我不确定为什么矩形(平台)只是变得更高而不是去上下(我知道两个平台会一起上升,会解决这个问题,这只是为了测试)

1 个答案:

答案 0 :(得分:3)

您的代码存在以下几个问题:

  1. 您只需拨打一次pygame.display.update,而不是每次pygame.draw
  2. clock.tick设置在主游戏循环中,而不是在其他地方,
  3. 您必须在绘图前清除屏幕。
  4. 以下是您可能会按预期工作的内容:

    import pygame, sys
    
    pygame.init()
    
    display_width = 800
    display_height = 500
    
    white = (255, 255, 255)
    
    display = pygame.display.set_mode((display_width, display_height))
    pygame.display.set_caption('Pong')
    clock = pygame.time.Clock()
    
    platform_y = display_height * 0.38
    
    
    def platform(color, x, y):
        global platform_y
    
        pygame.draw.rect(display, color, (x, y, 25, 100))
    
    
    def ball():
        pygame.draw.circle(display, white, (400, 250), 12)
    
    
    def game():
        global platform_y
        y_change = 0
    
        while True:
            platform(white, 100, platform_y)
            platform(white, 675, platform_y)
            ball()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit(0)
                elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    sys.exit(0)
    
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        y_change -= 2
                    if event.key == pygame.K_DOWN:
                        y_change += 2
    
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                        y_change = 0
    
            platform_y += y_change
    
            pygame.display.update()
            clock.tick(80)
            display.fill((0, 0, 0))
    
    
    if __name__ == '__main__':
        game()
    

    一对建议:

    1. 不要使用global!它的代码很糟糕。如果不使用它,肯定有更好的方法可以达到你想要的效果。
    2. 尽量避免嵌套很多条件。它很难阅读,一两天后需要很多精力才能阅读。
    3. 快乐的编码!

相关问题