火狐精灵在跳跃时不动

2022-04-05 00:00:00 python pygame sprite pygame-surface

问题描述

我搞不懂为什么我的精灵在跳跃时一动不动。我检查了几次,一遍又一遍地更改代码,但都没有成功。我的代码如下,包含3个页面,第一个页面包含主循环,第二个页面包含Player类,第三个页面包含一些游戏函数。

Main

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在";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()对象的方法tick()以这种方式延迟了游戏,循环的每次迭代都消耗相同的时间段。请参阅pygame.time.Clock.tick()

此方法应每帧调用一次。

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

        # [...]

相关文章