Reputation: 35
I'm facing a problem in pygame where the sprite is not moving regardless the inputs, i've checked it several times but have no clue what's wrong with it. There are 3 pages, first contains the main loop of the game, second contains the code of player sprite and third contains some game functions.
main---------page-01
import pygame
import sys
from player import Player
import game_functions as gf
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:
# Check if user wants to quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Navy screen
screen.fill((0,0,128))
gf.responses(screen, player)
player.p_movements()
# Make the player appear
player.draw_player()
# Make the newly made screen visible
pygame.display.flip()
run_game()
Player--------------------page-02
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
def p_movements(self):
if self.moving_left:
self.rect.x -= 5
if self.moving_right:
self.rect.x += 5
if self.moving_up:
self.rect.y -= 30
def draw_player(self):
''' Draw the player on the screen'''
pygame.draw.rect(self.screen, (255,255,255), self.rect)
game_functions------------------page-03
import pygame
def responses(screen, player):
''' Check for responses'''
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.moving_up == True
elif event.key == pygame.K_LEFT:
player.moving_left == True
elif event.key == pygame.K_RIGHT:
player.moving_right == True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
player.moving_up == False
elif event.key == pygame.K_LEFT:
player.moving_left == False
elif event.key == pygame.K_RIGHT:
player.moving_right == False
Upvotes: 1
Views: 50
Reputation: 881153
The first thing you appear to do in your main event loop is to totally clear the queue while looking for a quit event. That's not going to necessarily go down well with your second event loop in the game functions file.
In fact, it's quite messy to have two event loops, one of which throws away anything that's not a quit event - this may well lead to lost events.
I suspect a better way would be to provide a response
game function which just handles one event (and doesn't touch the event queue at all):
def response(screen, player, evt):
if evt.type == pygame.KEYDOWN:
blah blah blah
and have the main (now the only) event loop call that for events it does not want to handle itself:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
gf.response(screen, player, event)
You may also want to consider what you would expect when both the up and down button are pressed (or the left and right, for that matter). At a minimum, you probably want to make that a test case :-)
Upvotes: 1