【发布时间】:2021-04-08 07:43:21
【问题描述】:
我是 Python 新手,我一直在尝试制作一个简单的游戏,您可以在其中将正方形向左、向右移动并使其跳跃。在过去的几天里,我一直在尝试进行跳跃运动,但没有成功。有人可以帮我吗?
import pygame
pygame.init()
xscreen, yscreen = 1000, 500
screen = pygame.display.set_mode((xscreen, yscreen))
pygame.display.set_caption("Pygame")
width, height = 50, 50
x, y = 950, 200
vel, jumping_points = 10, 10
run = True
while run:
pygame.time.delay(100)
# Checks if the player wants to quit the game.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Checks if a key was pressed and moves the square accordingly.
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and x + width < xscreen:
x += vel
if keys[pygame.K_LEFT] and x > 0:
x -= vel
# Unfinished jump movement.
if keys[pygame.K_SPACE] and y > 0:
while True:
if jumping_points >= -10:
y -= (jumping_points ** 2) / 5
jumping_points -= 1
animation()
else:
jumping_points = 10
break
# Updates the screen surface.
animation()
pygame.quit()
编辑:已删除,对问题不重要。
【问题讨论】: