如何在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()
瘋兔76

這是縮進的問題您必須在應用程序循環中進行跳轉。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()

    # [...]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档