Sprite not moving despite inputs in pygame - python

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

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 :-)

Related

Trying to program a basic snake game in python (keeps "crashing")

So I'm trying to teach myself a little bit of python for fun following this tutorial: https://www.youtube.com/watch?v=8dfePlONtls
I made it to the point where the dot keeps moving automatically (left, right, up or down) depending on which arrow key I pressed last. But for some reason it all stops after a few seconds unless I keep inputting random keys or even just hovering the mouse over the window will do the trick. If I stop doing anything, the program "stops".
I also noticed the CPU usage going up to 22% for the program when I stop doing anything, but goes back down when I start inputting keys or moving the mouse again.
I've tried following exactly what was done in the video but I can't find any error and pycharm doesn't detect any obvious error in my code.
Here is what I have so far:
import pygame
from pygame.locals import *
import time
class Snake:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
self.block = pygame.image.load("resources/block.jpg").convert()
self.x = 100
self.y = 100
self.direction = 'down'
def move_left(self):
self.direction = 'left'
def move_right(self):
self.direction = 'right'
def move_up(self):
self.direction = 'up'
def move_down(self):
self.direction = 'down'
def draw(self):
self.parent_screen.fill((117, 17, 8))
self.parent_screen.blit(self.block, (self.x, self.y))
pygame.display.update()
def walk(self):
if self.direction == 'left':
self.x -= 10
if self.direction == 'right':
self.x += 10
if self.direction == 'up':
self.y -= 10
if self.direction == 'down':
self.y += 10
self.draw()
class Game:
def __init__(self):
pygame.init()
self.surface = pygame.display.set_mode((500, 500))
self.surface.fill((117, 17, 8))
self.snake = Snake(self.surface)
self.snake.draw()
def run(self):
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_UP:
self.snake.move_up()
if event.key == K_DOWN:
self.snake.move_down()
if event.key == K_LEFT:
self.snake.move_left()
if event.key == K_RIGHT:
self.snake.move_right()
elif event.type == QUIT:
running = False
self.snake.walk()
time.sleep(0.2)
if __name__ == "__main__":
game = Game()
game.run()
Thanks in advance!
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_UP:
self.snake.move_up()
if event.key == K_DOWN:
self.snake.move_down()
if event.key == K_LEFT:
self.snake.move_left()
if event.key == K_RIGHT:
self.snake.move_right()
elif event.type == QUIT:
running = False
self.snake.walk()
time.sleep(0.2)
Your original code indentation means that you are handling keypresses every pygame event, but also updating your snake every pygame event. Removing the indent on the last two lines which move the snake means they will execute in the outer while loop instead of the event loop. This means the snake will update its position each time the loop runs instead of only when an event occurs.

Continuous Movement in Pygame

I am trying to make a ship on the surface move continuously on screen but it only accepts one key press at a time. I have tried all solutions online and they aren't working.
import pygame
#initialize the pygame module
pygame.init()
#set the window size
screen = pygame.display.set_mode((1280, 720))
#change the title of the window
pygame.display.set_caption("Space Invaders")
#change the icon of the window
icon = pygame.image.load("alien.png")
pygame.display.set_icon(icon)
#add the ship to the window
shipx = 608
shipy = 620
def ship(x, y):
ship = pygame.image.load("spaceship.png").convert()
screen.blit(ship, (x,y))
running = True
while running:
#background screen color
screen.fill((0, 0, 0))
#render the ship on the window
ship(shipx,shipy)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
shipx -= 30
if keys[pygame.K_RIGHT]:
shipx += 30
pygame.display.update()
I'm still new to Pygame. How can I fix this?
Its a matter of Indentation. pygame.key.get_pressed() has to be done in the application loop rather than the event loop. Note, the event loop is only executed when
event occurs, but the application loop is executed in every frame:
running = True
while running:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#<--| INDENTATION
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
shipx -= 30
if keys[pygame.K_RIGHT]:
shipx += 30
# [...]
The problem that I found is that your application is replying only on one key pressed, not on a continuous movement. When you set the pygame.key.set_repeat function like in the example below, everything should be running smoothly.
import sys
import pygame
#initialize the pygame module
pygame.init()
#set the window size
screen = pygame.display.set_mode((1280, 720))
# Images
ship_img = pygame.image.load("spaceship.png")
ship_rect = ship_img.get_rect()
def draw():
screen.blit(ship_img, ship_rect)
pygame.key.set_repeat(10)
while True:
#background screen color
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
ship_rect = ship_rect.move((-30, 0))
if keys[pygame.K_RIGHT]:
ship_rect = ship_rect.move((30, 0))
#render the ship on the window
draw()
pygame.display.update()
For me, if I need to move an object in Pygame continuously, a velocity variable can be assigned to control the speed.
Here is part of the code for my robot movement program inside of the game_on loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Detect keyboard input on your computer check if it is the direction keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow is pressed")
robotX_speed = -60
if event.key == pygame.K_RIGHT:
print("Right arrow is pressed")
robotX_speed = 60
if event.key == pygame.K_UP:
print("Up arrow is pressed")
robotY_speed = -60
if event.key == pygame.K_DOWN:
print("Down arrow is pressed")
robotY_speed = 60
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("Keystoke L/R has been released")
robotX_speed = 0
if event.type == pygame.K_DOWN or event.key == pygame.K_UP:
print("Keystoke Up/Down has been released")
robotY_speed = 0
# update the coordinates in the while loop
robotX += robotX_speed
robotY += robotY_speed
Hope these codes can help you!

Python game over screen won't accept input

I'm trying to make it so when the game over screen shows the user can press space to get back into the game. Currently, when a game over happens, it displays the game over screen but accepts no input or at least doesn't do anything with the input. For some context, the game is basically about moving left and right to avoid obstacles. Currently, I only have one obstacle, but I just have not gotten to that yet. Thanks!
import pygame
import random
import math
pygame.init()
screenWidth = 700
screenHeight = 800
x = screenWidth / 2
y = (screenHeight / 4) * 3
width = 50
height = 50
win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Test Game")
bg = pygame.image.load("background.png").convert()
gameover = pygame.image.load("gameover.png").convert()
bgx = (screenWidth / 6) * 2
bgy = 0
clock = pygame.time.Clock()
class enemy():
def __init__(self,c,y,width,height):
self.c = c
self.y = y
self.width = width
self.height = height
self.vel = 5
def draw(self, win):
if self.c == 1:
self.x = 250
#250
elif self.c == 2:
self.x = 350
#350
else:
self.x = 450
#450
self.y += self.vel
pygame.draw.rect(win, (0,0,255), (self.x,self.y,self.width,self.height))
evil = enemy(random.randint(1,3),0,50,50)
#def redrawGameWindow():
# evil.draw(win)
# pygame.display.update()
running = True
gameOver = False
while running:
clock.tick(60)
while gameOver:
win.blit(gameover, (0,0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if pygame.event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
gameOver = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x+=100
if event.key == pygame.K_LEFT:
x-=100
win.fill((0,0,0))
win.blit(bg, (bgx, bgy))
evil.draw(win)
dist = math.hypot(evil.x - x, evil.y - y)
if dist <= 50:
print("Game Over!")
running = False
gameOver = True
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
#redrawGameWindow()
while gameOver:
win.blit(gameover, (0,0))
pygame.display.update()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
gameOver = False
pygame.quit()
The main problem is that your game over scene is the loop behind the main while running: loop and there's no way to go back to the first loop when you reach it. When you touch the evil object, you set running = False, leave the main and enter the while gameOver: loop.
In this loop you also need to call pygame.event.pump(), otherwise the pygame.key.get_pressed() function doesn't work correctly and the window freezes after a while because the events are not handled. If you want to restart the game, you should rather use only the nested while gameOver loop. Actually, I would recommend that you restructure the scenes even more and use a finite state machine instead (here's an answer in which I use functions as scenes but check out the link in the comment below as well).
Here's a working version of your code. I've changed a few things and added comments to explain the changes.
while running:
# -----The game over scene.-----
while gameOver:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# pygame.quit only uninitializes the pygame modules and
# doesn't quit the program.
pygame.quit()
# This will quit the whole program. You need to import sys.
sys.exit()
elif event.type == pygame.KEYUP: # event.type not pygame.event.type
if event.key == pygame.K_SPACE:
# Change it to False to break out of the loop.
gameOver = False
# Reset the game. You could reset the position of the
# `evil` object or instantiate a new one.
evil.x = 350
evil.y = 0
win.blit(gameover, (0,0))
pygame.display.update()
clock.tick(60) # You need to call tick in this loop as well.
# ------------------------------
# -----The main scene.-----
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
x += 100
elif event.key == pygame.K_LEFT:
x -= 100
win.fill((0,0,0))
win.blit(bg, (bgx, bgy))
evil.draw(win)
dist = math.hypot(evil.x - x, evil.y - y)
if dist <= 50:
print("Game Over!")
# running = False # This would stop the main loop.
gameOver = True
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
clock.tick(60)
# The other while loop was removed.
pygame.quit()

I cant move my sprite after moving movement controls to a player class

I cant seem to get my sprite to move left or right after moving it to a class. I am using pygame. I have a velocity variable and add to that to my x for my shape yet it still doesnt move. Any help would be really appreciated. Movement was working before i decided to put all my the things to do with the player into a class.
import pygame
import Constants
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()
class player:
def __init__(self,velocity):
self.velocity = velocity
def location(self,x,y):
self.x = x
self.y = y
self.xVel = 0
def keys(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.xVel = - self.velocity
elif event.key == pygame.K_RIGHT:
self.xVel = self.velocity
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self.xVel = 0
def move(self):
self.x = + self.xVel
def draw(self):
display = pygame.display.get_surface()
pygame.draw.rect(window,(255,255,255),[self.x,self.y,20,40])
def do(self):
self.keys()
self.move()
self.draw()
P = player(10)
P.location(Constants.SCREEN_WIDTH/2,0)
#Map
def draw_map():
window.fill(Constants.BLUE)
#sprites
def draw_player():
player = Player(50,50)
all_sprite_list.add(player)
#Game loop
def game_loop():
GameQuit = False
while not GameQuit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameQuit = True
draw_map()
P.do()
pygame.display.update()
clock.tick(30)
#game initialisation
game_loop()
pygame.quit()
quit()
`
The problem is that pygame.event.get() clears all events from the event queue, so when your player calls it, all events are already gone. From the docs
This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue.
You can solve this by specifying which events should be removed from the event queue e.g.
for event in pygame.event.get(QUIT):
GameQuit = True
The other events should still be in the queue for your player to use
Leon Z. has already explained that pygame.event.get() empties the event queue. I usually pass the events from the event loop to the objects that need the events. You could also assign the list that pygame.event.get() returns to a variable and then pass this variable to the player instance: events = pygame.event.get().
BTW, there was a little mistake in the move method: = + instead of +=.
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Platformer")
window = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
all_sprite_list = pygame.sprite.Group()
class player:
def __init__(self,velocity):
self.velocity = velocity
def location(self,x,y):
self.x = x
self.y = y
self.xVel = 0
def keys(self, event):
"""Handle the events that get passed from the event loop."""
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.xVel = -self.velocity
elif event.key == pygame.K_RIGHT:
self.xVel = self.velocity
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self.xVel = 0
def move(self):
self.x += self.xVel # Add the self.xVel to self.x.
def draw(self):
display = pygame.display.get_surface()
pygame.draw.rect(display, (255,255,255), [self.x, self.y, 20, 40])
def do(self):
self.move()
self.draw()
P = player(10)
P.location(window.get_width()/2, 0)
def draw_map():
window.fill(pygame.Color('blue'))
def draw_player():
player = Player(50,50)
all_sprite_list.add(player)
def game_loop():
GameQuit = False
while not GameQuit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
GameQuit = True
# Pass the event to all objects that need it.
P.keys(event)
draw_map()
P.do()
pygame.display.update()
clock.tick(30)
game_loop()
pygame.quit()

I am unable to make an object move from point A to point B without user input using python 2.7 with pygame

I am attempting to make a game where the 'player' (who spawns as the moon furthest to the right) can move around using the 'WASD' keys and enemies fall from the top of the screen in order to hit the player and end the game. I am currently doing this for an assignment and i have set up a test to see whether it is possible for me to do this and so far i have been able to create a 'player' object that is controllable by the 'WASD' keys and make a screen which requires players to press 'SPACE' to play the game. I am having trouble though with being able to have the enemies fall on the 'Y' axis down the screen smoothly. The picture moves down the screen only when the user presses or releases a key, this creates a jagged movement or when the user moves the mouse over the pygame screen, creating a very smooth movement.
#import necessary modules and pygame.
import pygame, sys, random
from pygame.locals import *
#set global variables
pygame.init()
WINDOWWIDTH = 800
WINDOWHEIGHT = 800
BACKGROUNDCOLOUR = (255,255,255)
TEXTCOLOUR = (0,0,0)
FPS = 30
ENEMYMINSIZE = 10
BOMBSAWAY = -1
ENEMYMAXSIZE = 40
ENEMYMINSPEED = 1
ENEMYMAXSPEED = 10
ADDNEWENEMYRATE = 5
PLAYERMOVERATE = 5
FSIZE = 48
BLUE = (0,0,255)
global PLAY
PLAY = False
global fpsClock
fpsClock = pygame.time.Clock()
# set up pygame and GUI
MainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
windowSurface.fill(BACKGROUNDCOLOUR)
pygame.display.set_caption('Bubble Dash')
enemy = pygame.image.load('player.jpg')
char = pygame.image.load('player.jpg')
# set up fonts
basicFont = pygame.font.SysFont(None, 48)
# set up the text
text = basicFont.render('Press any key to play!', True, (255,255,0))
textRect = text.get_rect()
textRect.centerx = windowSurface.get_rect().centerx
textRect.centery = windowSurface.get_rect().centery
# draw the text onto the surface
# set up x and y coordinates
# music
windowSurface.blit(text, textRect)
def playgame(PLAY):
x,y = 0,0
movex,movey = 0,0
charx=300
chary=200
direction = 'down'
enemyx= 10
enemyy=10
while PLAY:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == ord('m'):
pygame.mixer.music.stop()
if event.key == ord('n'):
pygame.mixer.music.play()
if event.key == ord('a'):
movex = -0.5
if event.key == ord('d'):
movex = 0.5
if event.key == ord('w'):
movey = -0.5
if event.key == ord('s'):
movey = 0.5
if event.type ==KEYUP:
if event.key == ord('a'):
movex = 0
if event.key == ord('d'):
movex = 0
if event.key == ord('w'):
movey = 0
if event.key == ord('s'):
movey = 0
if direction == 'down':
enemyy += 7
windowSurface.fill(BLUE)
windowSurface.blit(char, (charx, chary))
windowSurface.blit(enemy, (enemyx, enemyy))
pygame.display.update()
charx+=movex
chary+=movey
def playertopresskey(PLAY):
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
PLAY = True
if PLAY == True:
playgame(PLAY)
pygame.display.update()
playertopresskey(PLAY)
I would like for the 'enemy' object to be able to fall from the top without the user either needing to keypress or to key up or to have to move the mouse on the screen, rather the 'enemy' would just fall from the top as soon as the subroutine is called.
You may need to tweak it a bit because it is set up for me at the moment but i deleted a few things that would give you bother. Hopefully someone can help me. Thanks.
The below is link to a picture similar to mine which you can download and replace in the code for both the 'char' and the 'enemy' variables to view this yourself for i cannot access the courses at the present time.
http://www.roleplaygateway.com/roleplay/the-five-elements/characters/miss-joy/image
I found your error, you would have caught it yourself if you would have divided your code into some functions. Here is the problem:
for event in pygame.event.get():
if event.type == QUIT:
...
if event.type == KEYDOWN:
...
if event.type == KEYUP:
...
if direction == 'down':
enemyy += 7
your code moving the enemy is called only when an event is waiting in the queue. Move it out of the loop, and you will be good to go.
You have to change indention for:
if direction == 'down':
enemyy += 7
Now it is inside for event in pygame.event.get():

Categories

Resources