On PyCharm my left key and right key isn't responding - python

#enable pygame mode
import pygame
pygame.init()
#create screen
screen = pygame.display.set_mode((900,600))
#Title + Logo
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("chicken.png")
pygame.display.set_icon(icon)
#Player icon
player_icon = pygame.image.load("spaceship.png")
playerX = 400
playerY = 500
def player(x, y):
screen.blit(player_icon, (x, y))
#game loop
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")
#Player change in coordinates
playerX += 0
playerY += 0
player(playerX, playerY)
pygame.display.update()
I have been learning about pygame and game programing using python during this quarantine. I have been doing this by watching a tutorial on youtube. Please don't downgrade me I have tried harder to make my question better last time it was my first question and got 2 downgrades. Thankyou for your time.

It s a matter of Indentation. In your code the pygame.KEYDOWN event is only evaluated in case of event.type == pygame.QUIT. "Move" the pygame.KEYDOWN event handling:
running = True
while running:
# backround colour RGB
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# <--| INDENTATION
#If key pressed check wether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("key left pressed")
if event.key == pygame.K_RIGHT:
print("key right pressed")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("key stroke has benn released")

Related

why isnt event.type == pygame.KEYDOWN working [duplicate]

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:

pygame not detecting keypresses

I've been trying to get pygame to detect keypresses so later on I can move a character around on the screen. I'm new to coding so I might just be missing something simple, but I can't figure out what's wrong with the code, should I switch to pygame.key.get_pressed() or something instead? thanks in advance.
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
It is a matter of Indentation. You must evaluate the events in the event loop instead of the application loop.
If you want to determine if a certain key is pressed, the you've to verify if the event type is pygame.KEYDOWN (or pygame.KEYUP for button release) and if the .key attribute of the event is equal the key enumerator.
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: #<-- "key" instead of "type"
print("left")
if event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
print("right")
if event.key == pygame.K_UP: #<-- "key" instead of "type"
print("up")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: #<-- "key" instead of "type"
print("stop")
#-->|
# INDENTATION
However, I recommend to use pygame.key.get_pressed() instead of the KEYDOWN event.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement
pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.
clock = pygame.time.Clock()
velocity = 5
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * velocity
playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * velocity
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
You need to indent this part of your code to be in the for loop:
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
Otherwise, the event will always only be the last event from the pygame.event.get() list.
So basically, from
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# detecting key presses
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_LEFT:
print("left")
if event.type == pygame.K_RIGHT:
print("right")
if event.type == pygame.K_UP:
print("up")
if event.type == pygame.KEYUP:
if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT:
print("stop")
pygame.display.update()
to:
running = True
while running:
screen.fill((0,0,0))
# set background
screen.blit(background, (0,0))
player(playerX, playerY)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("left")
if event.key == pygame.K_RIGHT:
print("right")
if event.key == pygame.K_UP:
print("up")
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
print("stop")
pygame.display.update()
(Notice the event.keys I replaced part of your event.types with.)

My player is not moving correctly, when the key is held down, it should move continuoulsy, but only moves once

I'm trying to write something simple in pygame, but this is something I don't understand.
I'm trying to add moving but it's not continuous. I press and hold the key but the key only goes through 1 x_change
import pygame, sys
from pygame.locals import *
pygame.init()
gDis=pygame.display.set_mode((400,300))
Black=(0,0,0)
White=(255,255,255)
Red=(255,0,0)
Blue=(0,0,255)
Silver=(192,192,192)
clock=pygame.time.Clock()
x=195
y=270
wid=20
hght=20
speed=5
x_change=0
y_change=0
left=False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
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
gDis.fill(Black)
pygame.draw.rect(gDis, Silver, (x, y, wid, hght))
clock.tick(60)
pygame.display.update()
It is a matter of Indentation. You must move and draw the player in the application loop instead of the event loop:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
x_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
# INDENTATION
#<--|
x += x_change
gDis.fill(Black)
pygame.draw.rect(gDis, Silver, (x, y, wid, hght))
pygame.display.update()
clock.tick(60)

Keystrokes not registering in Python/Pygame?

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

My space ship keeps on moving even when i don't press the side keys

import pygame
#initialize the screen
pygame.init()
#create the screen
screen = pygame.display.set_mode((800, 700))
#tile and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load("player.png")
playerx = 370
playery = 600
playerx_change = 0.39
def player(x,y):
screen.blit(playerImg, (x,y))
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keystroke
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerx_change = -0.39
if event.key== pygame.K_RIGHT:
playerx_change = 0.39
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.type == pygame.K_RIGHT:
playerx_change = 0
playerx += playerx_change
player(playerx,playery)
pygame.display.update()
My spaceship keeps on moving even when i don't press side keys. i am creating space invaders using python and pygame.the ship doesn't stop as it should have according to the code. i use the community version of visual studio 2019.
There is a typo in your code event.key == pygame.K_RIGHT rather than event.type == pygame.K_RIGHT.
Further more, there is an Indentation issue:
running = True
while running:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keystroke
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerx_change = -0.39
if event.key== pygame.K_RIGHT:
playerx_change = 0.39
#<--| INDENTATION
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerx_change = 0
Anyway I recommend to use pygame.key.get_pressed() rather than the key events for the movement:
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerx -= 0.39
if keys[pygame.K_RIGHT]:
playerx += 0.39
player(playerx,playery)
pygame.display.update()
The key events KEYDOWN and KEYUP occur once when a key is pressed respectively released, but the states which are returned by pygame.key.get_pressed() are True as long a key is hold down.

Categories

Resources