我想用pygame製作一個隨機包裹或反彈沙灘球圖像的程序。彈跳效果很好,但是當它試圖包裹球時,沿着邊緣的球毛刺會消失。消失後我檢查了x和y的位置,它仍然在移動。這是代碼:Pygame球包裝故障
import pygame, sys, random
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255])
ball = pygame.image.load('beach_ball.png')
x = 50
y = 50
xspeed = 10
yspeed = 10
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
movement = random.choice(["wrap", "bounce"])
pygame.time.delay(20)
pygame.draw.rect(screen, [255,255,255], [x, y, 90, 90], 0)
x = x + xspeed
y = y + yspeed
if movement == "bounce":
if x > screen.get_width() - 90 or x < 0:
xspeed = -xspeed
if y > screen.get_height() - 90 or y <0:
yspeed = -yspeed
if movement == "wrap":
if x > screen.get_width():
x = -90
if x < 0:
x = screen.get_width()
if y > screen.get_height():
y = -90
if y < 0:
y = screen.get_width()
screen.blit(ball, [x, y])
pygame.display.flip()
pygame.quit()