如何在 python 中跳跃?

How to make a jumping in python?

在 Python 的帮助下,我正在做自己的游戏。我需要跳到我的精灵可以跳。但是,我的精灵不能跳。如果我的程序有 1 个或更多错误,我的游戏将无法运行。拜托,找错我的精灵可以跳。这是我的程序:

from pygame.locals import *
import pygame
import os
import random

WIDTH = 800
HEIGHT = 600
FPS = 60
usr_y = 400

# Задаем цвета
GREEN = (75, 0, 130)
BLUE = (255, 20, 147)

# Создаем игру и окно
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini-games by Latypov Vildan 10'a' Class")
clock = pygame.time.Clock()
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 40))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH - 360
        self.rect.centery = HEIGHT - 200

    def update(self):
        self.speedx = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -8
        if keystate[pygame.K_RIGHT]:
            self.speedx = 8
        self.rect.x += self.speedx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        


    

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


def print_text(message, x, y, font_color = (0, 0, 0), font_type = 'фыы.ttf', font_size = 30):
    font_type = pygame.font.Font(font_type, font_size)
    text = font_type.render(message, True, font_color)
    screen.blit(text, (x, y))

print_text('Hi', 250, 250)

jump = False
counter = -30

def make():
    global  usr_y, counter, jump
    if counter >=  -30:
        usr_y-= counter / 2.5
        counter -= 1
    else:
        counter = 30
        jump = False
    

# Цикл игры

running = True
while running:
    # Держим цикл на правильной скорости
    clock.tick(FPS)
    # Ввод процесса (события)
    for event in pygame.event.get(): 
        # check for closing window
        if event.type == KEYDOWN:    
            if event.key == K_ESCAPE:
                running = False
            elif event.type == QUIT:
                running = False
            elif event.key == K_SPACE:
                jump = True

                if jump:
                    make()
                
                


                                       
    # Обновление
    all_sprites.update()
    # Рендеринг
    screen.fill(BLUE)
    all_sprites.draw(screen)
    # После отрисовки всего, переворачиваем экран
    pygame.display.flip()


pygame.quit()

这是Indentation的事情。您必须在应用程序循环中跳转。当事件发生时,状态 jump 被设置一次。但是,跳跃需要在连续帧中进行动画处理。

你的事件循环混乱了,你必须改变玩家对象的坐标而不是 usr_y:

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 40))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH - 360
        self.rect.centery = HEIGHT - 200
        self.y = self.rect.y                # <--- floating point y coordinate

    def update(self):
        self.rect.y = round(self.y)         # <--- update player rectangle
        self.speedx = 0
        # [...]
jump = False
counter = 30

def make():
    global counter, jump
    if counter >= -30:
        player.y -= counter / 2.5  # <--- change player.y
        counter -= 1
    else:
        counter = 30
        jump = False
running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get(): 
        if event.type == QUIT:
            running = False 
        elif event.type == KEYDOWN:    
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_SPACE:
                jump = True

    # INDENTATION
    #<----------|
    if jump:
        make()

    # [...]