Pygame 精灵在跳跃时不动

Pygame sprite not moving while jumping

我很困惑为什么我的精灵在跳跃时不动。我检查了好几次,一遍又一遍地更改我的代码,但没有成功。我的代码如下,包含 3 页,第一页包含主循环,第二页包含播放器 class,第三页包含一些游戏功能。

主要

import pygame
from player import Player
import game_functions as gf
import sys
import time

def run_game():
    # Intialise the game and start the screen
    pygame.init()   
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("AmarCreep")
    
    player = Player(screen)
    
    # Main loop
    while True:
        
        # Navy screen
        screen.fill((0,0,128))
        
        
        for event in pygame.event.get():
            # Check if user wants to quit
            if event.type == pygame.QUIT:
                sys.exit()
            gf.responses(screen, player, event)
            
        player.p_movements()
        
        
        # Make the player appear
        player.draw_player()
        
        
        # Make the newly made screen visible
        pygame.display.flip()
    
run_game()

玩家

import pygame
from pygame.sprite import Sprite


class Player(Sprite):
    # Initialise the main player
    
    def __init__(self, screen):
        super(Player, self).__init__()
        
        self.screen = screen
        self.screen_rect = screen.get_rect()
        
        # Specifying the position of the player at start 
        self.rect = pygame.Rect(0, 0, 30, 30)
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = 590
        
        self.moving_left = False
        self.moving_right = False
        self.moving_up = False
        self.y = 30
        self.y_ud = 5
        
        
    def p_movements(self):
        
        if self.moving_left and self.rect.x > 5:
            self.rect.x -= 1
        if self.moving_right and self.rect.x < 765:
            self.rect.x += 1    
        if self.moving_up:
            self.rect.y -= self.y
            self.y -= 2
            if self.y == -30:
                self.moving_up = False
                self.y = 30
                self.rect.bottom = 590
            pygame.time.delay(20)   
        
                
            
            
            
    def draw_player(self):
        ''' Draw the player on the screen'''
        pygame.draw.rect(self.screen, (255,255,255), self.rect)
    

游戏功能

import pygame


def responses(screen, player, event):
    ''' Check for responses'''
    
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            player.moving_up = True
            
                
        if event.key == pygame.K_LEFT:
            player.moving_left = True
                
        if event.key == pygame.K_RIGHT:
            player.moving_right = True
                
    elif event.type == pygame.KEYUP:
        
                
        if event.key == pygame.K_LEFT:
            player.moving_left = False
                
        if event.key == pygame.K_RIGHT:
            player.moving_right = False
        

实际上,玩家在跳跃时会移动,但由于pygame.time.delay(20).
,在“self.moving_up的情况下几乎不会被注意到 从您的代码中删除 delay,但增加玩家的移动:

class Player(Sprite):

    def p_movements(self):
        
        if self.moving_left and self.rect.x > 5:
            self.rect.x -= 5                         # <---
        if self.moving_right and self.rect.x < 765:
            self.rect.x += 5                         # <--- 
        if self.moving_up:
            self.rect.y -= self.y
            self.y -= 2
            if self.y == -30:
                self.moving_up = False
                self.y = 30
                self.rect.bottom = 590
            # pygame.time.delay(20)                    <--- DELETE

但是使用pygame.time.Clock来控制每秒的帧数,从而控制游戏速度。

方法tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

def run_game():
    # [...]
    
    # Main loop
    clock = pygame.time.Clock()
    while True:
        clock.tick(60)

        # [...]