gpt4 book ai didi

Python pygame - 带有 float 的平滑移动

转载 作者:行者123 更新时间:2023-12-01 00:17:26 27 4
gpt4 key购买 nike

所以我是Python新手,我正在尝试通过编写一个小汽车游戏来学习pygame模块。游戏的“引擎”基于滚动背景,当用户按下“向上”键时,背景会向下移动。

我试图使汽车减速(当用户释放“向上”键时)而不是立即停止。为此,我尝试使用 float for 循环(使用 numpy 模块)将背景移动的速度从 5 降低到 0 -0.1,但它无法正常工作。实际发生的情况是,当我释放“向上”键时,游戏卡住了一秒钟,然后背景不平滑地向下移动了几个像素。

这是我的代码,希望你能帮我解决这个问题:

import pygame
import numpy

pygame.init()
clock = pygame.time.Clock()
run = True

# screen & bg setting
wnw = 500
wnh = 700
wn = pygame.display.set_mode((wnw, wnh))
bgcolor = (70, 190, 255)
bg = pygame.image.load('road.jpg')
bgy = 0
bgy2 = -bg.get_height()

# loading objects
class Car():
sprite = pygame.image.load('car.gif')
w = 100
h = 100
x = 200
y = 350
ver_spd = 5
hor_spd = 2
car = Car()

def drawbg():
wn.blit(bg, (0, bgy))
wn.blit(bg, (0, bgy2))
drawbg()

while run:

clock.tick(60)

# keys binding
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if car.x + car.hor_spd > 0:
drawbg()
car.x -= car.hor_spd
if keys[pygame.K_RIGHT]:
if car.x + car.hor_spd < wnw - car.w:
drawbg()
car.x += car.hor_spd
if keys[pygame.K_UP]:
if car.y - car.ver_spd > 0:
bgy, bgy2 = bg_movement(bgy, bgy2, car.ver_spd)
def bg_movement(bgy, bgy2, spd):
drawbg()
bgy += float(spd)
bgy2 += float(spd)
if bgy > bg.get_height():
bgy = -bg.get_height()
if bgy2 > bg.get_height():
bgy2 = -bg.get_height()
return bgy, bgy2


for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
for i in numpy.arange(5.0, 0.0, -0.1):
bgy, bgy2 = bg_movement(bgy, bgy2, float(i))

wn.blit(car.sprite, (car.x, car.y))

pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
print(event)

pygame.quit()

最佳答案

第一个 pygame.event.get()获取所有消息并将它们从队列中删除。因此,第一个或第二个循环都会获取一个事件,但两个循环都不会获取所有事件。这导致一些事件似乎被错过。
在您的应用程序中实现 1 个事件循环。

一旦释放UP,您就必须设置减速状态。只要decelerate > 0,减少变量并将每帧中的焦点向前移动一定量:

def drawbg():
wn.blit(bg, (0, int(bgy)))
wn.blit(bg, (0, int(bgy2)))
drawbg()

def bg_movement(bgy, bgy2, spd):
bgy += float(spd)
bgy2 += float(spd)
if bgy > bg.get_height():
bgy = -bg.get_height()
if bgy2 > bg.get_height():
bgy2 = -bg.get_height()
return bgy, bgy2

decelerate = 0
while run:

clock.tick(60)

# keys binding
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if car.x + car.hor_spd > 0:

car.x -= car.hor_spd
if keys[pygame.K_RIGHT]:
if car.x + car.hor_spd < wnw - car.w:
car.x += car.hor_spd
if keys[pygame.K_UP]:
decelerate = 0
if car.y - car.ver_spd > 0:
bgy, bgy2 = bg_movement(bgy, bgy2, car.ver_spd)

if decelerate > 0:
bgy, bgy2 = bg_movement(bgy, bgy2, decelerate)
decelerate -= 0.1


for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
decelerate = car.ver_spd


drawbg()
wn.blit(car.sprite, (car.x, car.y))

pygame.display.update()

关于Python pygame - 带有 float 的平滑移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com