My sprite keeps moving off of the screen in pygame - python

so I've been messing around with sprites and movement in pygame and the issue I keep running into with my code is that when you hold down the key the sprite won't stop moving, but if it is past the point at which it is supposed to stop and you let go and try again it will do what it is meant to and not move, is there any way I can have it happen straight away so it won't keep disappearing from my screen?
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700:
moveX = 4
else:
moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0:
moveX = 0
else:
moveX = -4
if event.key == pygame.K_UP:
moveY = -4
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moveX = 0
if event.key == pygame.K_LEFT:
moveX = 0
if event.key == pygame.K_UP:
moveY = 0
player.falling = True
player.collision = False
player.onground = False

Something seems not right with the indentation.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700:
moveX = 4 # <----- should this be here ?
else: # <----- this should match the `event.key == pygame.K_RIGHT` condition
moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0:
moveX = 0
else:
moveX = -4
if event.key == pygame.K_UP:
moveY = -4
Can you please redo the indentation and check?
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
if player.x + 4 < 700: moveX = 4
else: moveX = 0
if event.key == pygame.K_LEFT:
if player.x == 0: moveX = 0 # here you might want to try player.x <= 0 ...
else: moveX = -4
...
may be easier to handle ...

Related

pygame having trouble with wasd keybinding

trying to do a dumb little begginer game and i wanted to have two players, one using arrow keys and one using wasd, arrows are working with no issue but i have no idea how to do wasd, i apologize in advance im a begginer in python and my names are in portuguese so before i send the code let me just help out in saying that im using "jogador1" and "jogador2" to mean "player1" and "player2"
here's the full code
# jogo plataformer 1
import pygame
# começar o programa
pygame.init()
# ecrã
ecrã = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Trump attack")
backgroundImg = pygame.image.load("weirdbackground.png")
def background():
ecrã.blit(backgroundImg, (0, 0))
funcionar = True
# loop do jogo
# jogador1
jogadorImg =pygame.image.load("biden1.png")
jogadorX = 282
jogadorY = 58
jogadorX_mudar = 0
jogadorY_mudar = 0
# jogador2
jogador2Img = pygame.image.load("trump1.png")
jogador2X = 436
jogador2Y = 58
jogador2Y_mudar = 0
jogador2X_mudar = 0
# bala do jogador 1
balaImg = pygame.image.load("trump2.png")
balaX = jogadorX
balaY = int(jogadorY - 5)
balaY_mudar = 0
balaX_mudar = 0
bala_estado = "pronto"
def disparar(x, y):
global bala_estado
bala_estado = "fire"
ecrã.blit(balaImg, (balaX, balaY))
def jogador2(x, y):
ecrã.blit(jogador2Img, (x, y))
def jogador(x, y):
ecrã.blit(jogadorImg, (x, y))
while funcionar:
for event in pygame.event.get():
if event.type == pygame.QUIT:
funcionar = False
# keybinds jogador 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
jogadorX_mudar = -1
if event.key == pygame.K_RIGHT:
jogadorX_mudar = 1
if event.key == pygame.K_UP:
jogadorY_mudar =-1
if event.key == pygame.K_DOWN:
jogadorY_mudar = 1
if event.key == pygame.K_SPACE:
disparar(balaX, balaY)
print("hello")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
jogadorX_mudar = 0
if jogadorY < -30:
jogadorY_mudar += 1
if jogadorY == float(231.0):
jogadorY_mudar *= 0
jogadorY -= 1
#keybinds jogador 2
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
jogador2X_mudar = -1
if event.key == pygame.K_d:
jogador2X_mudar = 1
if event.key == pygame.K_w:
jogador2Y_mudar =-1
if event.key == pygame.K_s:
jogador2Y_mudar = 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
jogador2X_mudar = 0
if jogador2Y < -30:
jogador2Y_mudar += 1
if jogador2Y == float(231.0):
jogador2Y_mudar *= 0
jogador2Y -= 1
# bloco de cima jogador1
if (jogadorY <=200 and jogadorY >= 45) and (jogadorX >= 227 and jogadorX <= 448):
jogadorY_mudar *= 0
if (jogadorY <= 45) and (jogadorX >= 448 or jogadorX <= 227):
jogadorY_mudar += 1
# bloco de cima jogador2
if (jogador2Y <=200 and jogador2Y >= 45) and (jogador2X >= 227 and jogador2X <= 448):
jogador2Y_mudar *= 0
if (jogador2Y <= 45) and (jogador2X >= 448 or jogador2X <= 227):
jogador2Y_mudar += 1
if bala_estado == "fire":
disparar(balaX, balaY)
balaY += 1
background()
jogadorX += jogadorX_mudar
jogadorY += jogadorY_mudar
jogador2Y += jogador2Y_mudar
jogador2X += jogador2X_mudar
jogador2(jogador2X, jogador2Y)
jogador(jogadorX, jogadorY)
pygame.display.update()
```
It is a matter of Indentation. The events must be handled in the event loop, instead of after the event loop:
while funcionar:
for event in pygame.event.get():
if event.type == pygame.QUIT:
funcionar = False
# INDENTQTION
#-->|
# keybinds jogador 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
jogadorX_mudar = -1
if event.key == pygame.K_RIGHT:
jogadorX_mudar = 1
if event.key == pygame.K_UP:
jogadorY_mudar =-1
if event.key == pygame.K_DOWN:
jogadorY_mudar = 1
if event.key == pygame.K_SPACE:
disparar(balaX, balaY)
print("hello")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
jogadorX_mudar = 0
#[...]

trying to make a sprite jump

**When ever i press up arrow the sprite jumps but when it comes down it carrys on going down. Here is my code. it does not do this with left and rght keys.
x = (display_width * 0.1)
y = (display_height * 0.75)
x_change = 0
y_change = 0
over = False
while over == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change -= 5
if event.key == pygame.K_RIGHT:
x_change += 5
if event.key == pygame.K_UP:
y_change = -30
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
if event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP:
y_change = 30
x += x_change
y += y_change
The problem is caused by the y_change remaining at 30 during later processing.
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
if event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP:
y_change = 30 # <=== HERE
So when the event fires, the y_change becomes 30 - so the position moves back down. But on the next loop, y_change is still 30, so it keeps moving down, again and again.

Pygame: KEYDOWN and KEYUP being triggered at the same time causing no net movement

I've made it so when a user clicks WASD it makes smooth_x or smooth_y to 5 to constantly add to their x or y coordinates to simulate motion.
However I have a problem where if the user is holding down A then they click D at the same time, it causes smooth_x to be 0, causing the user to stay in place.
E.G User Clicks D (smooth_x = 5) User Clicks A (smooth_x = -5) User is holding D then holds A then lets go of D resulting in smooth_x being = 0 causing the user to stop moving which I don't want. In this scenario smooth_x should be = -5
while gameloop == True:
num_scraps = 0
fps.tick(60) #Sets FPS to 60
for event in pygame.event.get(): #Checks each event
if event.type == pygame.QUIT: #If one of the events are quit (when the user clicks the X in the top right corner) the window closes
pygame.quit()
if event.type == pygame.KEYUP:
print(event)
#If the user stop pressing one of the arrow keys it sets all the smooth values to 0 so it stops increasing the x or y coordinate
if event.key == pygame.K_w:
smoothy = 0
if event.key == pygame.K_s:
smoothy = 0
if event.key == pygame.K_a:
smoothx = 0
if event.key ==pygame.K_d:
smoothx = 0
if event.type == pygame.KEYDOWN: #Checks for a keypress
print(event)
if event.key == pygame.K_w:
smoothy -= 5 #reduces the y by 5 so player moves up
if event.key == pygame.K_s:
smoothy += 5 #increases the y by 5 so player moves down
if event.key == pygame.K_a:
smoothx -= 5 #reduces the x by 5 so player moves left
if event.key == pygame.K_d:
smoothx += 5 #increases the x by 5 so player moves right
I usually handle the movement in this way: I set the x- and y-velocity to the desired value if a key was pressed and update the position in the main loop. To stop the left-right movement, I also check if the character is moving to the left or to the right smoothx > 0 before I set the value back to 0. Then the character won't stop if you press both the left and right key at the same time.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
smoothy = -5
elif event.key == pygame.K_s:
smoothy = 5
elif event.key == pygame.K_a:
smoothx = -5
elif event.key == pygame.K_d:
smoothx = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w and smoothy < 0:
smoothy = 0
elif event.key == pygame.K_s and smoothy > 0:
smoothy = 0
elif event.key == pygame.K_a and smoothx < 0:
smoothx = 0
elif event.key ==pygame.K_d and smoothx > 0:
smoothx = 0
For the up-down movement it doesn't matter so much, because it's very hard to press both up and down at the same time, but of course you can check that as well to be sure.
Use add/substract in KEYUP like you do in KEYDOWN but with the opposite sign.
if event.type == pygame.KEYUP:
print(event)
if event.key == pygame.K_w:
smoothy += 5
if event.key == pygame.K_s:
smoothy -= 5
if event.key == pygame.K_a:
smoothx += 5
if event.key ==pygame.K_d:
smoothx -= 5
if event.type == pygame.KEYDOWN: #Checks for a keypress
print(event)
if event.key == pygame.K_w:
smoothy -= 5 #reduces the y by 5 so player moves up
if event.key == pygame.K_s:
smoothy += 5 #increases the y by 5 so player moves down
if event.key == pygame.K_a:
smoothx -= 5 #reduces the x by 5 so player moves left
if event.key == pygame.K_d:
smoothx += 5 #increases the x by 5 so player moves right

How do I stop a sprite from moving diagonally using Pygame?

I am making a snake game. Anytime I press an arrow key to move in one direction then press a key in another direction, the snake will diagonally. (E.g. if I first press right then press up.) This happens even if the previous key is released. How can I stop this?
# x and y marks the player's position
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
if event.key == pygame.K_UP:
y_change = -10
if event.key == pygame.K_DOWN:
y_change = 10
x += x_change
y += y_change
I have added y_change = 0 and x_change = 0 to reset the values to only keep orthogonal movement.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
y_change = 0
if event.key == pygame.K_RIGHT:
x_change = 10
y_change = 0
if event.key == pygame.K_UP:
y_change = -10
x_change = 0
if event.key == pygame.K_DOWN:
y_change = 10
x_change = 0
x += x_change
y += y_change

Pygame simple issue, object movement and display

player_1 = pygame.image.load(player1)
#
def player1(x,y):
window.blit(player_1, (x,y))
x = (110)
y = (150)
x_change = 0
y_change = 0
player1_speed = 0
while not gameover:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameover = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
x_change = 0
x += x_change
y += y_change
player1(x,y)
pygame.display.update()
fpsClock.tick(60)
screen.blit(background_surface, (0,0))
# update display
pygame.display.flip()
Hi, I have a program where unfortunatley when moving the object, it moves left & right perfectly but up and down does not seem to stop. There is also an issue of the screen flashing. Sorry for the long question but any help would be appreciated. Thanks
1. As far as I see you are only blitting player1. Where is player2?
2. You are missing some indentation in your code. I guess the correct form would be:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
x_change = 0
3. Then your are only setting x_change to 0. I guess you want to set y_change to 0 as well... even though you have to make your query more specific by grouping up vertical and horizontal keys.
Like:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
4. You call the blit method after updating the screen. Moving pygame.display.update() after screen.blit(..) would fix some things...
5. The flip method is outside of your loop. It will only be called if your game loop breaks!
Little hint: Copy-Pasting is a very bad practice. Try to do it yourself instead!

Categories