【发布时间】:2020-07-07 16:45:46
【问题描述】:
我是 pygame 的新手,我正在看一些关于如何开始使用它的教程,我编写了这个基本代码,它只有运动和跳跃“功能”,但它真的很不稳定,我不认为它是我的硬件( MacBook Pro 2018)。有人知道发生了什么吗?
代码如下:
import pygame
pygame.init()
win_width = 500
win_height = 500
win = pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("First PyGame")
width = 40
height = 60
x = win_width/2
y = win_height/2
vel = 10
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("adios putito")
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < win_width - width - vel:
x+= vel
if not (isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN]and y < win_height - height - vel:
y += vel
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0,0,0))
pygame.draw.rect(win,(255,255,255), (x,y,width,height))
pygame.display.update()
pygame.quit()
【问题讨论】:
-
我对 pygame 了解不多,但乍一看
pygame.time.delay(100)看起来很可疑。 -
在视频中只是说它就像“游戏时钟”,但我不知道为什么,但我的代码与教程中的人完全一样,但我的代码很不稳定:/
-
@Phix 你是对的,我刚刚删除了那行,一切正常!谢谢!
-
@AbrahamEsquivel 这是什么教程?这不是你使用游戏时钟的方式。您应该在程序开始时使用
clock = pygame.time.Clock()创建一个时钟,然后在您拥有pygame.time.delay(100)的地方使用clock.tick(60)。 -
确保将您的答案标记为已接受。
标签: python macos animation pygame