我查看了这里提出的类似问题,但没有一个问题可以解决我的问题(或者,我不知道如何在我的代码中实现它们)。如果有人能让我理解,我将不胜感激。我知道您必须做到这一点,以使x和y坐标不会低于0,但是不知道如何。
import pygame
pygame.init()
import math
#set screen
win = pygame.display.set_mode((500, 450))
clock = pygame.time.Clock()
#set title
pygame.display.set_caption("Game")
#player values (going to implement later)
playerHealth = 100
playerStamina = 100
#values
x = 10
y = 380
width = 40
height = 60
vel = 5 #velocity
isJump = 0
jumpCount = 10
#Loop
#if not running, quit
run = True
while run:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#import keys
keybinds = pygame.key.get_pressed()
#if left arrow key pressed go left
if keybinds[pygame.K_a] or keybinds[pygame.K_LEFT]:
x -= vel
if keybinds[pygame.K_LSHIFT]:
vel = 10
else:
vel = 5
#if right arrow key pressed go right
if keybinds[pygame.K_d] or keybinds[pygame.K_RIGHT]:
x += vel
#jump
if not(isJump):
if keybinds[pygame.K_SPACE] or keybinds[pygame.K_UP] and y < vel:
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
#fill background colour black
win.fill((0))
#draw player object
pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
pygame.display.update()
pygame.quit()