moving a sprite while holding down a key not working - python

So im trying the make my sprite move constantly when im holding my arrow keys, but i need to keep pressing it to move it. any idea why?
here's my code:
yes i have imported pygame and everything
class Block(pygame.sprite.Sprite):
def __init__(self, color = blue,widht = 64, height = 64):
super(Block, self).__init__()
self.image = pygame.Surface((widht, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.sound = pygame.mixer.Sound("2dSounds/Walk.wav")
self.hspeed = 0
self.vspeed = 0
to update the sprite, so it changes places depending what key i press
def update(self):
self.rect.x += self.hspeed
self.rect.y += self.vspeed
to change the speed using a_block.change_speed(...)
def change_speed(self, hspeed, vspeed):
self.hspeed += hspeed
self.vspeed += vspeed
to set the position of the sprite when i first create it
def set_position(self, x, y):
self.rect.x = x
self.rect.y = y
to set a image for my sprite i just created
def set_image(self, filename = None):
if(filename != None):
self.image = pygame.image.load(filename)
self.rect = self.image.get_rect()
to play a sound
def play_sound():
self.sound.play()
the gameloop
def game_loop():
a_block = Block()
global event
gameDisplay.fill(white)
#Quit
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
the controls that dont work
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
a_block.change_speed(-20, 0)
if event.key == pygame.K_RIGHT:
a_block.change_speed(20, 0)
if event.key == pygame.K_UP:
a_block.change_speed(0, -20)
if event.key == pygame.K_DOWN:
a_block.change_speed(0, 20)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
a_block.change_speed(0, 0)
if event.key == pygame.K_RIGHT:
a_block.change_speed(0, 0)
if event.key == pygame.K_UP:
a_block.change_speed(0, 0)
if event.key == pygame.K_DOWN:
a_block.change_speed(0, 0)
To draw a_block and other things
block_group = pygame.sprite.Group()
gameDisplay.fill(white)
a_block.set_image('2dImages/brick.png')
a_block.set_position(display_width/2, display_height/2)
a_block.update()
block_group.add(a_block)
block_group.draw(gameDisplay)
update display
pygame.display.update()
clock.tick(60)
thanks alot in advance!!

At the beginning of your code (but after you call pygame.init()), add the following line of code:
pygame.key.set_repeat(10)
This will post keyboard events to the event queue every 10 milliseconds even if the key was already pressed.

Related

How to select individual sprites for movement by clicking them? [duplicate]

This question already has answers here:
Pygame mouse clicking detection
(4 answers)
Closed 1 year ago.
I am struggling to be able to move individual cars in my game. I want it so that when you click on a car and move the arrow keys, the selected car is the only one that moves. I have created a Point class and begun to write some code about that colliding with the cars to select them however I'm unsure what to do next. Can anyone help me to be able to select different cars by clicking on them and then be able to move that car alone.
Here is my code:
import pygame, random, time
pygame.init()
class Point(pygame.sprite.Sprite):
def __init__(self,x,y):
super().__init__()
self.rect=pygame.Rect(x,y,0,0)
class Car(pygame.sprite.Sprite):
def __init__(self,image,spawnx,spawny):
super().__init__()
self.image= pygame.Surface((100,100))
self.image = image
self.type = "car"
self.rect = self.image.get_rect()
self.rect.x = spawnx
self.rect.y = spawny
cars.add(self)
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
screen = pygame.display.set_mode((1150, 650))
background_image = pygame.image.load("carpark2.jpg")
cars=pygame.sprite.Group()
clock = pygame.time.Clock()
done = False
black=Car(pygame.image.load("blackcar.png").convert_alpha(),100,100)
blue=Car(pygame.image.load("bluecar.png").convert_alpha(),200,100)
yellow=Car(pygame.image.load("yellowcar.png").convert_alpha(),300,100)
purple=Car(pygame.image.load("purplecar.png").convert_alpha(),400,100)
orange=Car(pygame.image.load("orangecar.png").convert_alpha(),500,100)
white=Car(pygame.image.load("whitecar.png").convert_alpha(),600,100)
green=Car(pygame.image.load("greencar.png").convert_alpha(),700,100)
brown=Car(pygame.image.load("browncar.png").convert_alpha(),800,100)
pink=Car(pygame.image.load("pinkcar.png").convert_alpha(),900,100)
current_car=Car(pygame.image.load("redcar.png").convert_alpha(),100,400)
while done==False:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
x,y = event.pos
for car in pygame.sprite.spritecollide(Point(x,y,), cars, False):
current_car.deselect()
current_car = car.select()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_car.move(-50,0)
if event.key == pygame.K_RIGHT:
current_car.move(50,0)
if event.key == pygame.K_UP:
black.move(0,-50)
if event.key == pygame.K_DOWN:
black.move(0,50)
screen.blit(background_image, [0, 0])
cars.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.display.flip()
pygame.quit()
The pygame.Rect object needs to have a size of at least 1x1:
self.rect=pygame.Rect(x,y,0,0)
self.rect=pygame.Rect(x, y, 1, 1)
Anyway, I recommend to use pygame.Rect.collidepoint instead of pygame.sprite.spritecollide.
You don't need a select and deselect method at all. Just set current_car = car:
for car in cars:
if car.rect.collidepoint(x, y):
current_car = car
Application loop:
while done==False:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
x,y = event.pos
for car in cars:
if car.rect.collidepoint(x, y):
current_car = car
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_car.move(-50,0)
if event.key == pygame.K_RIGHT:
current_car.move(50,0)
if event.key == pygame.K_UP:
current_car.move(0,-50)
if event.key == pygame.K_DOWN:
current_car.move(0,50)
screen.blit(background_image, [0, 0])
cars.draw(screen)
pygame.display.flip()

Pygame player movement stops when too many keys are pressed at the same time

When I try to move my character and press any other key at the same time, the movement suddenly stops. For example, the space key in my code is used to shoot a tiny orb out of the spaceship. Every time I press space and I am moving left and right quickly, the orb will shoot out but the player movement will be frozen for a second or two.
I have tried to switch to different ways of handling the way keys are input, but all of them seem to lead to this same problem. pygame.key.get_pressed() also has this problem when in my code.
I am not quite sure if this is a problem with my laptop keyboard or something in the code, so the code for the entire file is below.
import pygame, sys, decimal
# Screen Size
SCREEN_X = 400
SCREEN_Y = 400
# Loading Images
backgroundImg = pygame.image.load('StarBackground.png')
menuBar = pygame.image.load('Menu_Bar.png')
shipImg = pygame.image.load('PowerShip.png')
orb = pygame.image.load('Orb00.png')
class Ship(pygame.sprite.Sprite):
# Movement rate of change
change_x = 0
# Methods
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = shipImg.convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = SCREEN_X / 2 - 8
self.rect.y = SCREEN_Y - 40
def move(self, speed):
self.change_x = speed
def stop(self):
self.change_x = 0
def update(self, screen):
self.rect.x += self.change_x
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.right > SCREEN_X:
self.rect.x -= 1
screen.blit(self.image, self.rect)
class MenuBar(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = menuBar.convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = 10
self.rect.y = 0
def update(self, screen):
screen.blit(self.image,self.rect)
class Bullet1(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = orb.convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.alive = True
def update(self):
if self.alive == True:
self.rect.y -= 1
if self.alive == False:
self.rect.y = -10000
class HealthBar(pygame.sprite.Sprite):
pass
class EnergyBar(pygame.sprite.Sprite):
pass
class PointsBar(pygame.sprite.Sprite):
pass
class Background(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = backgroundImg.convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = 0
def update(self, screen):
if self.rect.top > 0:
self.rect.y = SCREEN_Y * -1
self.rect.y += 1
screen.blit(self.image, self.rect)
def main():
pygame.init()
size = [SCREEN_X, SCREEN_Y]
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF) # Set the height and width of the screen
pygame.display.set_caption("Space Adventure") # Setting the game name in the title bar
background = Background() # Creating the game objects
menubar = MenuBar()
ship = Ship()
finished = False # Close button exit code
bullet1Enabled = True
bullet1Count = 1
spacePressed = False
clock = pygame.time.Clock() # Manages the frames per second
lastkey = None # Variable that stores the last key pressed
bulletlist = []
# Game loop
while not finished:
for event in pygame.event.get():
print(lastkey)
if event.type == pygame.QUIT:
finished = True
pygame.event.set_blocked(pygame.MOUSEMOTION)
if event.type == pygame.KEYDOWN:
if lastkey != pygame.K_SPACE:
lastkey = event.key
if event.key == pygame.K_SPACE:
spacePressed = True
if bullet1Enabled == True:
bullet1 = Bullet1(ship.rect.x, ship.rect.y)
bulletlist.append(bullet1)
bullet1Count = 1
else:
spacePressed = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT and lastkey != pygame.K_LEFT:
lastkey = None
ship.move(0)
if event.key == pygame.K_LEFT and lastkey != pygame.K_RIGHT:
lastkey = None
ship.move(0)
if event.key == pygame.K_RIGHT or lastkey == pygame.K_LEFT:
spacePressed = False
if event.key == pygame.K_LEFT or lastkey == pygame.K_RIGHT:
spacePressed = False
#Bullet Delay
if spacePressed == True:
bullet1Count = True
if spacePressed == False:
bullet1Count = False
if lastkey == pygame.K_RIGHT:
ship.move(1)
if lastkey == pygame.K_LEFT:
ship.move(-1)
clock.tick(240) # Frames per second
background.update(screen) # Background update
# Menu Bar update
ship.update(screen) # Ship update
for b in bulletlist:
if b.rect.bottom <= 0:
b.alive = False
b.update()
screen.blit(b.image, b.rect)
menubar.update(screen)
pygame.display.flip() # Updates the display for everything
pygame.quit() # Clean shutdown on IDLE
if __name__ == "__main__":
main()
The problem occurs because you don't reset lastkey to None after you release the space bar, so you have to press left or right twice.
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
lastkey = None
I don't see why you need the lastkey variable at all. I'd remove these lines from the main loop,
if lastkey == pygame.K_RIGHT:
ship.move(1)
if lastkey == pygame.K_LEFT:
ship.move(-1)
insert them in the event loop and change lastkey to event.key:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.move(1)
if event.key == pygame.K_LEFT:
ship.move(-1)
Now you should be able to remove the lastkey completely.

How to move a rectangle in python using pygame in this code?

I want to move a rectangle with the arrow keys, so far I only can draw the rectangle but I don't get the keyhandler to work.
I'm a newbie in python and pygame.
This is my main.py:
import pygame
import time
import random
from Player import *
# Initialize Pygame
pygame.init()
game_over = False
# Color
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (192, 192, 192)
# Setting up the window
surfaceWidth = 800
surfaceHeight = 500
surface = pygame.display.set_mode((surfaceWidth, surfaceHeight))
pygame.display.set_caption("Rectangle Runner")
clock = pygame.time.Clock()
#Player(surface, surface_height, surface_width, size, jump_height, color)
player = Player(surface, surfaceHeight, surfaceWidth, 50, 200, BLACK)
# Game Loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if game_over == True:
pygame.quit()
quit()
surface.fill(GRAY)
#Player.draw()
player.keyHandling()
player.checkBoundaries()
player.draw()
#Obstacle.draw()
pygame.display.update()
clock.tick(60)
# This code shall be unreachable
pygame.quit()
quit()
This is the Player.py:
import pygame
class Player:
def __init__(self, surface, surface_height, surface_width, size, jump_height, color):
self.surface = surface
self.surface_height = surface_height
self.surface_width = surface_width
self.size = size
self.jump_height = jump_height
self.color = color
self.x = (0 + (self.surface_width/10))
self.y = self.surface_height - self.size
self.x_move = 0
self.y_move = 0
self.rect = pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
def keyHandling(self):
# Draw the player
#pygame.draw.rect(self.surface, self.color,
#[self.x, self.y, self.size, self.size])
# KeyListener
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.y_move = -10
#self.y += self.y_move
#self.rect.move_ip(0, -10)
if event.key == pygame.K_LEFT:
self.x_move = -5
#self.x += self.x_move
#self.rect.move_ip(-5, 0)
if event.key == pygame.K_RIGHT:
self.x_move = 5
#self.x += self.x_move
#self.rect.move_ip(5, 0)
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.y_move = 0
#self.y += self.y_move
#self.rect.move_ip(0, 10)
if event.key == pygame.K_LEFT:
self.x_move = 0
if event.key == pygame.K_RIGHT:
self.x_move = 0
self.x += self.x_move
self.y += self.y_move
def checkBoundaries(self):
# Checking for Boundaries
#if self.y <= self.jump_height:
# self.y_move = -10
if self.y > self.surface_height - self.size:
self.y_move = 0
if self.y <= self.jump_height + self.size:
self.y_move = -10
def draw(self):
pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
#pygame.draw.rect(self.surface, self.color,[self.x, self.y, self.size, self.size])
I need help. I'm thinking thru this the whole day.
Thanks in advance :D
The problem in your code is that pygame.event.get() is a generator. Each item that is obtained from it can ONLY be obtained once. Hence, each time the clock ticks, you are grabbing everything within the events generator and they are all destroyed. Next, when the Player object iterates over pygame.event.get, it does nothing, as the generator was already made empty. See this about generators.
What you need to do is catch the KEYDOWN event in the main.py module and then pass key information onto the Player object.
Replace the main loop in main.py with the following:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
player.keyDown(event.key)
if game_over == True:
pygame.quit()
quit()
surface.fill(GRAY)
player.checkBoundaries()
player.draw()
pygame.display.update()
clock.tick(60)
Remove the keyHandling method from Player.py and replace with the following method:
def keyDown(self, key):
if key == pygame.K_w:
self.y -= 10
elif key == pygame.K_a:
self.x -= 10
elif key == pygame.K_d:
self.x += 10
This now removes the issue with the generator.
Full code

Sprites and Movements with Arrows keys

So I have been working on making a sprite for a game. My goal is to make my picture of a square to move. However I am facing difficulties in actually moving the square. Any help for as to what is wrong with the key event part of my code would be very much so appreciated.
import pygame
import sys
import time
from time import sleep
import random
from pygame.locals import *
image1 = pygame.image.load('square.png')
class sprite(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load('square.png')
self.image = self.image.convert_alpha()
self.rect=self.image.get_rect()
self.x = 100
self.y = 100
self.dx=50
self.dy=50
def update(self):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.x -= self.dx
if event.key == pygame.K_DOWN:
self.x += self.dx
if event.key == pygame.K_LEFT:
self.x -= self.dx
if event.key == pygame.K_RIGHT:
self.x += self.dx
self.rect.center=(self.x,self.y)
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
sprite.dy = 0
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
sprite.dx = 0
def background(self, surface):
self.image2 = pygame.image.load(white.png)
screen.blit(self.image2, (0,0))
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
screen = pygame.display.set_mode((1250, 500))
screen.fill((255,255,255))
my_sprite=sprite()
while True:
pygame.init()
pygame.event.get()
pygame.event.pump
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
my_sprite.draw(screen)
pygame.display.update()
First of all, don't put pygame.init() in your loop.
I have a feeling that your problem is because in your update() method of the sprite, event is not even defined. Add the line for event in pygame.event.get(): at the start of the method and indent the rest under it.
def update(self):
for event in pygame.event.get():
... # Rest of the method

Python & Pygame: Unexpected crash during run-time

I am currently working on a pong clone in Python using Pygame. I am no where near an expert at the subject of programming, and have run into an error. Whenever I run the code I currently have, which will be listed below separated by Blockquotes, the program will freeze up, and not respond. I cannot tell what the issue may be with the little knowledge I have. I was hoping that someone would be able to assist me on this issue. I am currently using a tutorial to help instruct me through the making of this game, and the code looks pretty identical at this point. I will include a hyperink to the tutorial here. I appreciate any and all feedback.
3000X_runner.py
import GameObj
if __name__ == '__main__':
game = GameObj.Game()
game.run()
PongPaddle.py
import os, pygame
class Paddle(pygame.sprite.Sprite):
def __init__(self, user, xy):
pygame.sprite.Sprite.__init__(self)
if user == 1:
self.image = pygame.image.load(os.path.join("images", "playerPaddle.png"))
else:
self.image = pygame.image.load(os.path.join("images", "opponentPaddle.png"))
self.rect = self.image.get_rect()
self.rect.centerx, self.rect.centery = xy
self.movement_speed = 5
self.velocity = 0
def up(self):
self.velocity -= self.movement_speed
def down(self):
self.velocity += self.movement_speed
def move(self, dy):
if self.rect.bottom + dy > 720:
self.rect.bottom = 720
elif self.rect.top + dy < 0:
self.rect.bottom = 0
else: self.rect.y += dy
def update(self):
self.move(self.velocity)
GameObj.py
import os, pygame, PongPaddle
class Game(object):
def __init__(self):
pygame.init()
#setup screen width/height
self.screen = pygame.display.set_mode((1080, 720))
self.clock = pygame.time.Clock() #setups internal pygame clock
#Mandatory silly name featuring extra numbers for game name
pygame.display.set_caption("Hyper Pong 3000-X")
#create background.
self.background = pygame.Surface((1080, 720))
self.background.fill((0,0,0)) #black background
#draws a white line down the middle of screen
#line(Surface, color, start_pos, end_pos, width=1) -> Rect
pygame.draw.line(self.background, (255, 255, 255), (540, 0), (540, 720), 5)
self.screen.blit(self.background, (0, 0)) #blits to screen
#I shall render all the sprites here:
self.sprites = pygame.sprite.OrderedUpdates()
#setting up the paddles and adding them to my sprite group
self.player_paddle = PongPaddle.Paddle(1, (50, 200))
self.sprites.add(self.player_paddle)
self.opponent_paddle = PongPaddle.Paddle(2, (1030, 200))
self.sprites.add(self.opponent_paddle)
def run(self):
stillPlaying= True
while stillPlaying:
self.clock.tick(30)
#handles our pygame events, giving us a easy to view, clutter free game loop
stillPlaying = self.handleEvents
#updates all of our sprites
for sprite in self.sprites:
sprite.update()
self.sprites.clear(self.screen, self.background)
dirty = self.sprites.draw(self.screen)
pygame.display.update(dirty)
def handleEvents(self):
#here we handle all of our events, such as when either player will
#press a key to move, quit the game, etc etc.
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
#player1 keydown events
if event.key == pygame.K_w:
self.player_paddle.up()
if event.key == pygame.K_s:
self.player_paddle.down()
#player2 keydown events
if event.key == pygame.K_UP:
self.opponent_paddle.up()
if event.key == pygame.K_DOWN:
self.opponent_paddle.down()
elif event.type == pygame.KEYUP:
#player1 keyup events
if event.key == pygame.K_w:
self.player_paddle.down()
if event.key == pygame.K_s:
self.player_paddle.up()
#player2 keyup events
if event.key == pygame.K_UP:
self.opponent_paddle.down()
if event.key == pygame.K_DOWN:
self.opponent_paddle.up()
return True
Simply forgot to add () in GameObj.py after
stillPlaying = self.handleEvents
It should look like: stillPlaying = self.handleEvents()

Categories