I’m confused why my sprite is not moving while jumping. I’ve checked several times and changed my code over and over with no luck. My code is below and contains 3 pages, first contain the main loop, second contain the player class and third contain some game functions.
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()
Player
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)
Game functions
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
Advertisement
Answer
Actually, the player moves when you jump, but it is hardly noticeable in the case of ” self.moving_up
due to pygame.time.delay(20)
.
Remove delay
from your code, but increase the movement of the player:
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
But use pygame.time.Clock
to control the frames per second and thus the game speed.
The method 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) # [...]