in my game i have set my character to move. How it is set is:
if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()
if event.key == pygame.K_LEFT:
player_x_change = -2
if event.key == pygame.K_RIGHT:
player_x_change = 2
if event.key == pygame.K_UP:
player_y_change = -2
if event.key == pygame.K_DOWN:
player_y_change = 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT
player_x_change = 0
if event.type == pygame.KEYUP:
player_y_change = 0
Is it a way to change this to a while function or something so the character doesn't stop moving when release a arrow-key (even though another arrow-key is held down)?
the issue is the condition in event.type == pygame.KEYUP.
if event.type == pygame.KEYUP:
# [...]
if event.type == pygame.KEYUP:
player_y_change = 0
Just reset either player_x_change or player_y_change, dependent on the key that is released:
if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()
if event.key == pygame.K_LEFT:
player_x_change = -2
if event.key == pygame.K_RIGHT:
player_x_change = 2
if event.key == pygame.K_UP:
player_y_change = -2
if event.key == pygame.K_DOWN:
player_y_change = 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT
player_x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player_y_change = 0
If you want to handle the keys simultaneously, that means you want to move the player diagonal when 2 keys are pressed and you don't want to that the player stops, when 1 key is released, then I recommend to use pygame.key.get_pressed() rather than the KEYDOWN event:
for event in in pygame.event.get():
if game_over_state == False:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pause = True
game_paused()
if game_over_state == False:
keys = pygame.key.get_pressed()
player_x_change, player_y_change = 0, 0
if keys[pygame.K_LEFT]:
player_x_change -= 2
if keys[pygame.K_RIGHT]:
player_x_change += 2
if keys[pygame.K_UP]:
player_y_change -= 2
if keys[pygame.K_DOWN]:
player_y_change += 2
Related
class GameState():
def init(self):
self.state = 'main_game'
def main_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN: # this is where the error comes from
mouse_state = 1
pygame.mouse.set_pos(mouse_x,mouse_y + 1)
else:
mouse_state = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
snake.yV = 0
snake.xV = -1
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
snake.yV = 0
snake.xV = 1
if event.key == pygame.K_UP or event.key == pygame.K_w:
snake.xV = 0
snake.yV = -1
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
snake.xV = 0
snake.yV = 1
mouse_x = pygame.mouse.get_pos()[0]
mouse_y = pygame.mouse.get_pos()[1]
pygame.display.set_caption("Snake, FPS: " + str(clock.get_fps()))
screen.fill(GREY)
snake.update()
food.update()
utility.update()
bombs.update()
food.draw()
snake.draw()
utility.draw()
bombs.draw()
pygame.display.flip()
button = Button()
snake = Snake()
food = Food()
utility = Utility()
bombs = Bombs()
game_state = GameState()
while not done:
game_state.main_game()
clock.tick(50)
pygame.quit()
This is just the snippit of code i can send the rest if needed the thing i did previously i had it under a game loop but this time im making a class and object so i can make multiple levels in the game
The if event.type == pygame.MOUSEBUTTONDOWN: and if event.type == pygame.KEYDOWN: are both outside of the for loop that defines the "event" variable.
def main_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN: # this is where the error comes from
mouse_state = 1
pygame.mouse.set_pos(mouse_x,mouse_y + 1)
else:
mouse_state = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
snake.yV = 0
snake.xV = -1
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
snake.yV = 0
snake.xV = 1
if event.key == pygame.K_UP or event.key == pygame.K_w:
snake.xV = 0
snake.yV = -1
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
snake.xV = 0
snake.yV = 1
mouse_x = pygame.mouse.get_pos()[0]
mouse_y = pygame.mouse.get_pos()[1]
pygame.display.set_caption("Snake, FPS: " + str(clock.get_fps()))
screen.fill(GREY)
snake.update()
food.update()
utility.update()
bombs.update()
food.draw()
snake.draw()
utility.draw()
bombs.draw()
pygame.display.flip()
button = Button()
snake = Snake()
food = Food()
utility = Utility()
bombs = Bombs()
game_state = GameState()
Your code have indentation problems. Which makes event.MOUSEBUTTONDOWN and other events outside the loop. You code should have looks like this.
I created this program and when I run it the player is not moving. I checked and everything is right. Just in case you need it I am using python 3.8.6. Can you please help me? Here is my code.
# Game Loop
running = True
while running:
screen.fill((128, 20, 128))
for event in pygame.event.get():
player(playerX, playerY)
pygame.display.update()
# Movment
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
if event.key == pygame.K_d:
player_x_change += 10
if event.key == pygame.K_w:
player_y_change -= 10
if event.key == pygame.K_s:
player_y_change += 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player_x_change = 0
if event.key == pygame.K_d:
player_x_change = 0
playerX += player_x_change
playerY += player_y_change
# Close the game
if event.type == pygame.QUIT:
running = False
It's probably just an indentation error (perhaps pasting to the question), but none of your events are being handled because of the if event.type clauses are not inside the for event loop.
Simply fixing this indentation alleviates this.
The next problem is that you calculate the player_x_change and player_y_change, but never apply the amounts to playerX and playerY.
Since these changes only happen when pygame.KEYDOWN is received, it's easy to simply add this inside the event handler:
# Movment
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
elif event.key == pygame.K_d:
player_x_change += 10
elif event.key == pygame.K_w:
player_y_change -= 10
elif event.key == pygame.K_s:
player_y_change += 10
# Move the player
playerX += player_x_change
playerY += player_y_change
Note the use of if .. elif. If the event is one particular type, it cannot possibly be another type too (at the same time). This means it's not necessary to keep re-checking the type. Using elif ("else if") helps with these checks, by stopping the check once a match is found. It is more efficient.
Final code:
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF
BACKGROUND = (128, 20, 128)
### initialisation
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Not Moving")
# Game Loop
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
# Close the game
if event.type == pygame.QUIT:
running = False
# Movment
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player_x_change -= 10
elif event.key == pygame.K_d:
player_x_change += 10
elif event.key == pygame.K_w:
player_y_change -= 10
elif event.key == pygame.K_s:
player_y_change += 10
# Move the player
playerX += player_x_change
playerY += player_y_change
# DON'T REALLY NEED THIS
#elif event.type == pygame.KEYUP:
# if event.key == pygame.K_a:
# player_x_change = 0
# elif event.key == pygame.K_d:
# player_x_change = 0
# Re-draw the screen
screen.fill( BACKGROUND )
player(playerX, playerY)
pygame.display.update()
clock.tick(60) # limit the re-fresh rate to save electricity
pygame.quit()
I also added a frame-rate limiter with a pygame.time.Clock() object, just to save some CPU.
I was able to fix the problem now, if anyone wants the code here it is. :) It turns out I put the "close program" code too early and it doesn't work until the close button is pressed. So my player did move, but only for a second.
# Game Loop
running = True
while running:
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
I'm trying to make my paddle movements smoother in my pong game. My player_paddle1 has smooth movements and will stop whenever I let go of the key. However, my other player_paddle2, which incorporates the same keypress algorithm has the other paddle, does not do so. It will keep on going even if I release the key.
if game_option == "Two Player":
if event.type == KEYDOWN:
if event.key == K_UP:
player_paddle1.direction = -1
elif event.key == K_DOWN:
player_paddle1.direction = 1
if event.key == K_w:
player_paddle2.direction = -1
elif event.key == K_s:
player_paddle2.direction = 1
if event.type == KEYUP:
if event.key == K_UP and player_paddle1.direction == -1:
player_paddle1.direction = 0
elif event.key == K_DOWN and player_paddle1.direction == 1:
player_paddle1.direction = 0
if event.key == K_UP and player_paddle2.direction == -1:
print("The key is now up!")
player_paddle2.direction = 0
elif event.key == K_UP and player_paddle2.direction == 1:
player_paddle2.direction = 0
Also, while the up and down keys are extremely responsive, the W and S keys are not too responsive, which means, a keypressed will not immedieatly result in paddle motion. How can I fix this?
You check K_UP for second player in KEYUP but it has to be K_w and K_s.
Besides you don't have to check player_paddle2.direction
if event.type == KEYUP:
if event.key == K_UP:
player_paddle1.direction = 0
elif event.key == K_DOWN:
player_paddle1.direction = 0
elif event.key == K_w: # <-- there was K_UP
print("The key is now up!")
player_paddle2.direction = 0
elif event.key == K_s: # <-- there was K_UP
player_paddle2.direction = 0
it can be shorter
elif event.type == KEYUP:
if event.key in (K_UP, K_DOWN)
player_paddle1.direction = 0
elif event.key in (K_w, K_s):
player_paddle2.direction = 0
BTW: you can use elif in some places.
Update: manish kumar gave me the solution for the problem. I had to write event.key instead of event.type in a certain part of the code.
check it out at it below:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
I am trying to make a simple game by using Pygame.
Simply I have written code to move a car to the left and right.
Everything works well except that the keys do not move the car.
here is the code:
import pygame
from pygame.locals import *
pygame.init()
black = (0,0,0)
white = (255,255,255)
GD1 = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Racing!")
clock = pygame.time.Clock()
carimg = pygame.image.load("C:/Users/Abdulaziz/Downloads/my_app___car_sprite_5_by_nicolaspok-d65xysp.png")
def car(x,y):
GD1.blit(carimg,(x,y))
x = (800 * 0.45)
y = (600 * 0.7)
x_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -5
elif event.type == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
GD1.fill(white)
car(x,y)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()
the problem is probably in this part of the code:
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
x_change = -5
elif event.type == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
Again, the car appears but do not move.
you have to use event.key instead of event.type.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
I think the problem lies here:
for event in pygame.event.get():
# modifications to x_change
x += x_change
If the call to pygame.event.get() returns two events, a KEYDOWN and a KEYUP, then x_change will be set to 5 or -5, but then it will be set back to 0.
You should update x at each iteration:
for event in pygame.event.get():
# modifications to x_change
x += x_change
Just looking for assistance,
How can I adjust this so my character only moves when the LEFT of RIGHT key is held down & stops when key is released?
player_x = 10
player_y = 245
player_x_change = 0
clock = pygame.time.Clock()
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change = -20
if event.key == pygame.K_RIGHT:
player_x_change = 20
player_x += player_x_change
You have to check KEYUP and change player_x_change
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change = -20
elif event.key == pygame.K_RIGHT:
player_x_change = 20
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT,pygame.K_RIGHT):
player_x_change = 0
EDIT:
if you need to stop object when LEFT and RIGHT are pressed at the same time
player_x_change = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change -= 20
elif event.key == pygame.K_RIGHT:
player_x_change += 20
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player_x_change += 20
elif event.key == pygame.K_RIGHT:
player_x_change -= 20