0

我一直在尝试为我的播放器添加跳跃功能,但我想不出办法。

import pygame
pygame.init()
SCREENX,SCREENY = 500,500
screen = pygame.display.set_mode([SCREENX, SCREENY])
clock = pygame.time.Clock()

VEL = 5
VELY = 5
FPS = 60

PODLOGAY = 450

podloga = pygame.draw.rect(screen,(0,0,0),pygame.Rect(0,PODLOGAY,500,50))

onground = False

class Gracz():
    def __init__(self,x,y,jumptime):
        self.x = x
        self.y = y
        self.isjump = False
        self.jumptime = jumptime
        self.speed = jumptime
    def draw(self):
        pygame.draw.rect(screen,(255,255,255),pygame.Rect(self.x,self.y,50,50))
    def move(self):
        if pygame.key.get_pressed()[pygame.K_d]:
            self.x += VEL
        elif pygame.key.get_pressed()[pygame.K_a]:
            self.x -= VEL

    def checkcollision(self):
        global onground
        if not podloga.colliderect(pygame.Rect(self.x,self.y + VELY,50,50)) and self.isjump == False:
            self.y += VELY
            onground = False
        elif self.y + 50 > PODLOGAY:
            overlap = self.y + 50 - PODLOGAY
            self.y -= overlap
        else:
            onground = True
    def jump(self):
        global onground
        if self.isjump:
            if self.speed >= self.jumptime - (self.jumptime // 2) and onground == False:
                self.y -= self.speed
                self.speed -= 1
    
gracz = Gracz(225,0,10)

running = True
while running:

    screen.fill("deepskyblue2")

    podloga = pygame.draw.rect(screen,(0,0,0),pygame.Rect(0,PODLOGAY,500,50))

    gracz.draw()
    gracz.move()
    gracz.checkcollision()
    gracz.jump()

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and onground:
                gracz.isjump = True
                onground = False


    clock.tick(FPS)

pygame.quit()

我在谷歌上寻找任何教程,但如果不更改大部分代码,我找不到适合我情况的教程。我试图做的跳跃与类似。如果有人想知道为什么会有 podlogay,那只是我的语言

谢谢!

4

0 回答 0