I'm new in this python industry, when i press 'd' my character should go right, but this isn t hapening, same thing to the 'a'. When I change K_a and K_d with K_LEFT and K_RIGHT it works, my character is moving well, but if I wanna make the character move with 'a' and 'd', it doesn't work, what's wrong with my code:
import pygame
import sys
import os
pygame.init()
screen = pygame.display.set_mode((1366,768))
pygame.display.set_caption("Adventure in the Woods")
icon = pygame.image.load('tree.png')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('ninja.png')
playerX = 100
playerY = 650
velocity = 0.1
run = True
while run:
screen.fill( (9, 66, 2) )
screen.blit(playerImg, (playerX,playerY) )
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
pygame.display.update()
Problem Insight
The problem lies in the scope of your if event.type == pygame.KEYDOWN: ... as it should be within the for-loop for event in pygame.event.get(): ... :
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- From here
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity #<-------------- to here
Solution
This can be fixed by indenting the code block starting from if event.type == pygame.KEYDOWN: ..., like so:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN: #<-------------- Now inside the for-loop
if event.key == pygame.K_a:
print('a')
playerX -= velocity
if event.key == pygame.K_d:
print('d')
playerX += velocity
Other Remarks
To be more familiar with python code structures and styles, review some of the fundamental syntaxes like Indentation by visiting the style guide # https://www.python.org/dev/peps/pep-0008/
Cheers!
Related
This question already has an answer here:
How to get if a key is pressed pygame [duplicate]
(1 answer)
Closed 1 year ago.
alright so I have been trying to start learning game development in the past couple days and I finally started working on a project. The problem is that for some reason my controls aren't working even when I made everything like it is supposed to be. Please help. (also this is the entire code because I dont know where I am wrong)
#instalize the game VERY IMPORTANT
pygame.init()
# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)
#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0
def player(x,y):
screen.blit(playerImg, (x,y))
#game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
#RGB
screen.fill((0, 0, 0))
playerX += PlayerX_change
player(playerX, playerY)
pygame.display.update()
It is a matter of Indentation. You cannot nest events:
unning = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# INDENTATION
#<--|
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
# [...]
Your identation is not correct. I have corrected it and attached the code.
#instalize the game VERY IMPORTANT
pygame.init()
# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)
#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0
def player(x,y):
screen.blit(playerImg, (x,y))
#game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
#RGB
screen.fill((0, 0, 0))
playerX += PlayerX_change
player(playerX, playerY)
pygame.display.update()
Your indentation is incorrect
Try usinng this;
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
Also if you use event.KEYDOWN it not checks multiple keys
although you can use pygame.key.get_pressed()
Use:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
code:
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 am new to Python and especially new to Pygame. Been working on a basic space invader type game to attempt to learn more about Pygame, but I cannot figure out the code for moving the user ship. Have looked up some tutorials on it, and I THINK my code looks good, but I might be looking over something. I am in Python version 3.8 and Pygame version 1.9.6.
'''
This script is creating a space invader type game with the Pygame module.
Tutorial following YT video from freecodecamp.org
(https://www.youtube.com/watch?v=FfWpgLFMI7w&ab_channel=freeCodeCamp.org)
'''
import sys
import pygame
# Initializing Pygame
# (ALWAYS REQUIRED)
pygame.init()
# Screen Dimensions
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
# Other Game Settings
framerate = pygame.time.Clock()
framerate.tick(60)
# Setting Title and Images
pygame.display.set_caption('Space Invaders')
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)
player_ship = pygame.image.load('space-invaders.png')
def player(x,y):
'''
Draws the player's ship on the screen at (x,y) coordinates.
'''
screen.blit(player_ship,(x, y))
# Game Function
def game():
'''
Actual code for the game itself.
'''
# Sets the starting position for the player's ship
playerX = 368 # Middle of Screen (on x-axis)
playerY = 506 # 30px off bottom of the screen (y-axis)
x_change = 0
# Game Loop
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_ESCAPE:
game_exit = True
elif event.type == pygame.K_d:
x_change = 5
elif event.type == pygame.K_a:
x_change = -5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_a:
x_change = 0
playerX += x_change
print(x_change) # Using this to see if the script is recognizing the user keystrokes
# Setting Screen RGB
screen.fill((0,0,0))
player(playerX, playerY)
# Screen Update
# (ALWAYS REQUIRED)
pygame.display.update()
game()
pygame.quit()
sys.exit()
Thanks for your help!
The issue is you're checking for an event.type of pygame.K_d, etc.
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_ESCAPE: # <-- HERE
game_exit = True
elif event.type == pygame.K_d: # <-- HERE
x_change = 5
elif event.type == pygame.K_a: # <-- AND HERE
x_change = -5
The event.type cannot be equal to both pygame.KEYDOWN and pygame.K_d at the same time!. If you check the documentation on event, notice that the key-code is sent in event.key, so it's a simple fix.
KEYDOWN key, mod, unicode, scancode
KEYUP key, mod
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # <-- FIX HERE
game_exit = True
elif event.key == pygame.K_d: # <-- FIX HERE
x_change = 5
elif event.key == pygame.K_a: # <-- AND FIX HERE
x_change = -5
I have a python program using pygame, but when I run it it gives me a syntax error on the pygame.display.update line. I'm not sure what is causing it. Any help is appreciated!
import pygame, sys
import time
from pygame.locals import *
pygame.init()
area = pygame.display.set_mode((400, 300))
red=(255, 0, 0)
black=(0, 0, 0)
fps = 30
clock=pygame.time.Clock()
x=100
y=100
a=0
b=0
pygame.display.set_caption('Reddy For School')
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
b=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
b=10
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
a=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
a=10
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
a=0
if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
a=0
x+=a
y+=b
pygame.draw.rect(area, red, (x, y, 100, 100)
python.display.update()
area.fill(black)
clock.tick(fps)
As per the comments, you're missing a parentheses.
I'm not sure what the python.display.update() was trying to achieve, but I guess flush the display-updates to the screen. I substituted this with pygame.display.flip().
There's a couple of painting issues too - the code is drawing a red square, but then filling the screen with black. These needed to be re-ordered.
Also your main event-loop was not exiting cleanly (under Linux, I could not close the window with the titlebar (x) ). And finally, you had not parameterised the window width and height. Soon you will need to calculate something that relies on the window width (e.g.: centering something), so it is best to start with these in a parameter instead of having "400" & "300" all over the code.
import pygame, sys
import time
from pygame.locals import *
pygame.init()
window_width = 400
window_height = 300
area = pygame.display.set_mode((window_width, window_height))
red=(255, 0, 0)
black=(0, 0, 0)
fps = 30
clock=pygame.time.Clock()
x=100
y=100
a=0
b=0
pygame.display.set_caption('Reddy For School')
running = True
while running:
for event in pygame.event.get():
# check for closing the window
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
b=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
b=10
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
a=-10
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
a=10
if event.type == pygame.KEYUP and event.key == pygame.K_UP:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
b=0
if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
a=0
if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
a=0
x+=a
y+=b
area.fill(black)
pygame.draw.rect(area, red, (x, y, 100, 100) )
clock.tick(fps)
pygame.display.flip() #quicker process to draw things
pygame.quit()
I recently started tinkering a bit with pygame and I made this little piece of code (following a tutorial):
import pygame
pygame.init()
display_width = 1920
display_height = 1080
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Galvadon's Adventure")
clock = pygame.time.Clock()
galvadon = pygame.image.load("galvadonModelVersion1.0.png")
def galvadonIsHere(x,y):
gameDisplay.blit(galvadon,(x,y))
x = (display_width * 0.30)
y = (display_height * 0.2)
y_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.key == pygame.K_DOWN:
y_change = 5
elif event.key == pygame.K_UP:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y += y_change
gameDisplay.fill(white)
galvadonIsHere(x,y)
print(event)
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
and to an extent this code works, the problem is that after I move the image using the up and down arrows, the image starts to respond to any mouse movement by just gliding in the direction it last moved towards. I probably missed something out, but I just can't seem to find what it is. I looked at various websites looking for the answer but I couldn't find anyone asking a similar question, hence why I make this topic.
There are couple of things that you can do better here. But lets start with fixing the issue.
'Bug' is that after you press keys, y_change is set and never reset for next engine loop. This should help you:
...
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
y_change = 5
elif event.key == pygame.K_UP:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y += y_change
gameDisplay.fill(white)
galvadonIsHere(x, y)
print(event)
pygame.display.update()
clock.tick(30)
y_change = 0
...
Pay close attention at the last line in my snippet. Here you reset y_change so in next engine loop, whatever happens or doesn't, it wont affect position of your image.
Refactor and improve
First, you can agree that checking if event type is KEY_DOWN and nesting checks if it is particular key button is pain to read and work with. Not to mention you check if event type is KEYUP even though you know that is KEYDOWN here:
if event.type == pygame.KEYDOWN:
...
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
So, what I do is define helper function, that can be usual one or lambda. Now, many would argue that lambda functions are affecting readability but here it can certainly help you with these checks.
Example:
key_pressed = lambda event, key: event.type == pygame.KEYDOWN and event.key == key
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
break
elif key_pressed(event, pygame.K_DOWN):
y_change = 5
elif key_pressed(event, pygame.K_UP):
y_change = -5
else:
y_change = 0
y += y_change
gameDisplay.fill(white)
galvadonIsHere(x, y)
print(event)
pygame.display.update()
clock.tick(30)
y_change = 0
One more thing, pay attention at condition that there was pygame.QUIT event. break exits the for loop and then while loop ends too. This way you do not process any queued events, nor you update and blit image.
Your problem is in the while loop. The if statement for keyup is in the if statement for keydown, so it is preventing it from working. The following solves your problem:
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
y_change = 5
elif event.key == pygame.K_UP:
y_change = -5
else:
y_change = 0
Hope this helps.