我使用 Pygame 创建了一个简单的动画,其中包含一个移动的蛋糕和一根上下跳动的蜡烛。但动画效果出现问题,蛋糕和蜡烛的运动与预期不符。

我使用的代码如下:

import pygame

pygame.init()

# 设置屏幕大小
size = (700, 500)
screen = pygame.display.set_mode(size)

# 加载蛋糕图片
cake_img = pygame.image.load('cake.png')
cake_rect = cake_img.get_rect()

# 设置蛋糕初始位置和速度
cake_x = 0
cake_y = 200
cake_speed = 5

# 设置蜡烛初始位置和速度
candle_x = 50
candle_y = 150
candle_speed = 2

# 设置字体
font = pygame.font.SysFont('comicsansms', 72)

# 游戏循环
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # 移动蛋糕
    cake_x += cake_speed
    if cake_x > size[0]:
        cake_x = -cake_rect.width

    # 移动蜡烛
    candle_y += candle_speed
    if candle_y < 150 or candle_y > 250:
        candle_speed = -candle_speed

    # 绘制蛋糕和蜡烛
    screen.blit(cake_img, (cake_x, cake_y))
    pygame.draw.rect(screen, (255, 255, 0), (candle_x, candle_y, 20, 50))
    pygame.draw.circle(screen, (255, 0, 0), (candle_x + 10, candle_y - 20), 10)

    # 绘制文字
    text = font.render('Happy Birthday!', True, (0, 0, 255))
    text_rect = text.get_rect(center=(size[0] // 2, 50))
    screen.blit(text, text_rect)

    pygame.display.flip()

pygame.quit()

请描述你遇到的具体问题,例如:

  • 蛋糕的移动方向或速度是否正确?
  • 蜡烛的跳动是否正常?
  • 是否出现了其他异常行为?

提供更详细的信息,以便我能够更好地帮助你解决问题。

Pygame 动画问题:蛋糕和蜡烛运动异常

原文地址: https://www.cveoy.top/t/topic/nJ1Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录