python - 我们如何让我们的玩家在pygame中跳转
问题描述
import pygame
import sys
from pygame import *
from pygame.locals import RESIZABLE
pygame.init()
WINDOW_SIZE = (800, 600)
screen = pygame.display.set_mode(WINDOW_SIZE, RESIZABLE, 32)
player_img = pygame.image.load('ClipartKey_738895_adobespark.png')
player_X = 130
player_Y = 500
player_change_X=0
def player():
screen.blit(player_img, (player_X, player_Y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
if event.type == KEYDOWN:
if event.key == K_RIGHT:
player_change_X = 0.3
if event.key == K_LEFT:
player_change_X = -0.3
if event.key == K_SPACE:
player_Y += 40
elif event.type == KEYUP:
if event.key == K_RIGHT:
player_change_X = 0
if event.key == K_LEFT:
player_change_X = 0
screen.fill((0, 200, 255))
player()
player_X += player_change_X
pygame.display.update()
我想让玩家跳大约 4 年,但不能这样做。请告诉我我该怎么做,如果告诉任何功能,请告诉我它是什么以及它是如何做到的,因为我是 pygame 的新手。
解决方案
您可以使用像on_floor
. 如果它在地板上(使用碰撞),那么它允许程序开始跳跃(使用另一个变量,比如y_speed
。我通常让玩家像这样跳跃:
y_speed = 0
on_floor = False
# main loop
if on_floor:
if pygame.key.get_pressed()[K_SPACE]:
y_speed = -40 # start the jump if space pressed
# set to the value you used, but you should move according the the framerate
if y_speed > -40 # speed limit
y_speed += 1 # change the speed, to make a parabol-shape fall
player.y += y_speed
此外,你可以用这个答案让玩家像这样跳跃,它做类似的工作。
推荐阅读
- python - 计算 django 电子商务中的订单总额
- class - 在另一个类 C++ 的方法中创建类对象
- python - 如何使用 python ssl 模块获取证书的签名密钥长度
- arrays - 从数组中的每个文件中读取行——条件永远不会成功
- php - 完整性约束违规:1048 laravel Laravel
- sql-server - 使用变量调试 CTE?
- swift4 - swift4 UISwipeGestureRecognizer Viewcontroller 带有大标题和单元格不起作用
- ansible - Ansible 在没有密码文件的情况下无法运行
- javascript - jQuery Transverse Up(通过 sibs、parents、grandPs 等)直到找到 Element 的第一个实例
- extjs - ExtJS 数据存储不适用于短名称模型