gpt4 book ai didi

python - 怎样才能让 Action 更顺畅呢?

转载 作者:行者123 更新时间:2023-12-01 05:53:39 25 4
gpt4 key购买 nike

我正在开发一个非常基本的引擎,基于 Pygame 的教程,并且我在“平滑度”方面遇到了一些问题。如何让我的球员行走“更顺畅”?

我的事件处理程序非常基本,非常标准,没有什么新内容,我什至想出了如何进行“提升”(运行)以进行测试。但问题是,在 pygame.KEYUP 处,那些大量的零破坏了我的小播放器的“平滑度”,我不希望这样,但我不希望它走广告无限。

import pygame
import gfx

# Main Class

class Setup:

background = gfx.Images.background
player = gfx.Images.player

pygame.init()

# Configuration Variables:

black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
title = "Ericson's Game"

# Setup:

size = [700,700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption(title)
done = False
clock = pygame.time.Clock()

# Logic Variables

x_speed = 0
y_speed = 0
x_speed_boost = 0
y_speed_boost = 0
x_coord = 350
y_coord = 350
screen.fill(white)

# Main Loop:

while done == False:

screen.blit(background,[0,0])
screen.blit(player,[x_coord,y_coord])

for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True

if event.key == pygame.K_a:
x_speed = -6
x_speed_boost = 1
if event.key == pygame.K_d:
x_speed = 6
x_speed_boost = 2
if event.key == pygame.K_w:
y_speed = -6
y_speed_boost = 1
if event.key == pygame.K_s:
y_speed = 6
y_speed_boost = 2

if event.key == pygame.K_LSHIFT:

if x_speed_boost == 1:
x_speed = -10
if x_speed_boost == 2:
x_speed = 10
if y_speed_boost == 1:
y_speed = -10
if y_speed_boost == 2:
y_speed = 10

if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
x_speed = 0
x_speed_boost = 0
if event.key == pygame.K_d:
x_speed = 0
x_speed_boost = 0
if event.key == pygame.K_w:
y_speed = 0
y_speed_boost = 0
if event.key == pygame.K_s:
y_speed = 0
y_speed_boost = 0

x_coord = x_coord + x_speed
y_coord = y_coord + y_speed

pygame.display.flip()
pygame.display.update()

clock.tick(20)

pygame.quit()

最佳答案

使用键状态轮询,代码将变得更简单/更清晰,供您使用。如果游戏的其他部分使用“按下时”逻辑,您可以使用事件处理。所以你的 Action 是:

如果您正在调用pygame.display.flip(),那么您就不会使用pygame.display.update()。事实上,使用两者可能会减慢速度。

我使用了您的x_coord变量。但使用元组或向量来表示玩家位置会简化事情。您可以使用浮子,以实现更平滑的运动精度。然后它作为 int 传输到屏幕。

while not done:
for event in pygame.event.get():
# any other key event input
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
if event.key == K_ESC:
done = True

vel_x = 0
vel_y = 0
speed = 1

if pygame.key.get_mods() & KMOD_SHIFT
speed = 2


# get key current state
keys = pygame.key.get_pressed()
if keys[K_A]:
vel_x = -1
if keys[K_D]:
vel_x = 1
if keys[K_W]:
vel_y = -1
if keys[K_S]:
vel_y = 1


x_coord += vel_x * speed
y_coord += vel_y * speed

关于python - 怎样才能让 Action 更顺畅呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13378846/

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