python - 无法正确刷新 Pong 游戏中的显示

标签 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. 您只需调用 pygame.display.update 一次,而不是在每次 pygame.draw 之后调用,
  2. clock.tick 在主游戏循环中设置,而不是在其他地方设置,
  3. 绘图前必须先清空屏幕。

以下内容可能会按您的预期工作:

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. 不要使用全局!这是糟糕代码的味道。当然有更好的方法来实现您想要的而不使用它。
  2. 尽量避免大量条件嵌套。这本书很难读,一两天后需要很大的脑力才能读完。

祝你编码愉快!

关于python - 无法正确刷新 Pong 游戏中的显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48755293/

相关文章:

android - SurfaceView绘制性能

python - pySerial - 有没有办法一次选择多个端口?

python - 如何读取本地存储中的文件(如内存中的文件)并将其附加到电子邮件中?

python - 为什么 pygame 在退出之前需要 time.sleep 来播放声音?

python - .py转.exe生成黑窗应用几秒后消失

javascript - 尝试将我的 js 游戏放入 html 文件中,不会使用 Canvas 区域

python - Django 要求定义 `model`,而它已经定义了

python - Argparse,处理可重复的项目集

python - 如何从一组图像中获取随机图像?

java - LIBGDX 主菜单上有超过 1 个按钮