pygame.key.get_pressed is not working correctly [duplicate] - python

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
How can I make a sprite move when key is held down
(6 answers)
Closed 2 days ago.
I'm trying to get a rectangle to move on screen, but the keyboard presses aren't working, and I can't solve it. Can someone help?
import pygame
WIN = pygame.display.set_mode((900, 500))
pygame.display.set_caption("coolioso")
FPS = 60
keys_pressed = pygame.key.get_pressed()
vel = 10
player = pygame.Rect(450, 250, 50, 50)
def draw_window():
WIN.fill((255, 255, 255))
pygame.draw.rect(WIN, (255, 0, 0), (player))
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if keys_pressed[pygame.K_w]:
player.y += vel
draw_window()
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
I tried using multiple different methods like functions, putting the key press statement elsewhere, but nothing worked and nothing changed.

Related

I'm trying to move a sprite while animating it in pygame, the x value isn't changing though [duplicate]

This question already has answers here:
How can I make a sprite move when key is held down
(6 answers)
How to get keyboard input in pygame?
(11 answers)
Closed last month.
I'm trying to create a game in pygame where the character moves while animating, I tired using sprites but got the same results, however I did notice that pygame wasn't detecting that I pressed the right arrow key, I tried to get it to work but alas.
here's my code
:
import pygame
from sys import exit
def player_animation():
global player_current_sprite, player_frames, player_image
player_current_sprite += 0.01
player_image = player_frames[int(player_current_sprite)]
if player_current_sprite >= 1.3:
player_current_sprite = 0.7
pygame.display.set_caption('pausing has consequences')
pygame.init()
screen = pygame.display.set_mode((1000, 700))
clock = pygame.time.Clock()
player_frame_1 = pygame.image.load('graphics/player_frame_1.png')
player_frame_2 = pygame.image.load('graphics/player_frame_2.png')
player_current_sprite = 0
player_frames = [player_frame_1, player_frame_2]
player_image = player_frames[player_current_sprite]
player_rect = player_image.get_rect(center=(450, 250))
move = False
background_surface = pygame.image.load('graphics/background.png')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
move = True
if event.type == pygame.K_RIGHT and move == True:
player_rect.x += 10
if event.type == pygame.KEYUP:
move = False
screen.blit(background_surface, (0, 0))
player_animation()
screen.blit(player_image, player_rect)
pygame.display.flip()
clock.tick(60)
The x value does not change when I pressed the button, I did notice that pygame did not detect my button press

convert_alpha does not work. Can you help me please? [duplicate]

This question already has answers here:
How do I blit a PNG with some transparency onto a surface in Pygame?
(4 answers)
Pygame image transparency confusion
(1 answer)
Closed 8 months ago.
My code don't work, I tried to display a png image with convert_alpha in pygame 2.0 (python 3.10.1).
Here is my code:
import pygame
import sys
pygame.init()
background_colour = (255, 255, 255)
(width, height) = (1300, 700)
screen = pygame.display.set_mode((width, height))
bg = pygame.image.load("images/background.png")
rock = pygame.image.load("images/rock.png").convert_alpha()
pygame.display.set_caption('flying thing')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("Key up has been pressed, go up")
if event.key == pygame.K_DOWN:
print("Key down has been pressed, go down")
if event.key == pygame.K_SPACE:
print("Key space has been pressed, game stopped")
screen.blit(bg, (0, 0))
screen.blit(rock, (0, 0))
pygame.display.update()
pygame.quit()
quit()
here is my image:
here is my output:

pygame image collsion trouble [duplicate]

This question already has an answer here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I have tried to make a Zelda clone, and now I'm wondering how to calculate collision, can anyone tell me how to do that? I have tried colliderct and it simply won't work here is my code:
import pygame
pygame.init()
display = pygame.display.set_mode((800,600))
white=(255,255,255)
black=(0,0,0)
x=50
y=50
width = 40
height = 60
vel = 5
playerimg= pygame.image.load('link1.jpg').convert()
def player(x,y):
display.blit(playerimg,(x,y))
while True:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x -= vel
if keys[pygame.K_d]:
x += vel
if keys[pygame.K_w]:
y -= vel
if keys[pygame.K_s]:
y += vel
display.fill(white)
player(x,y)
pygame.draw.rect(display, (255,0,0), hitbox,2)
pygame.display.update()
pygame.quit()
you can use 'hitboxes' to do this, one you must know the dimensions of your image
now that you got them, you can do
hitbox=(x,y, 102,131)
hitbox1=pygame.draw.rect(display, (255,0,0), hitbox,2)
if the_thing_it_hits.colliderect(hitbox) == True:
print('ouch')
put this in the while True: loop and it should be good
You can do a collision test by using pygame.Rect and colliderect(). For instance you can define an obstacle and get the rectangle from playerimg by get_rect(). Test if the 2 rectangles are colliding:
while True:
# [...]
hitbox = pygame.Rect(100, 100, 100, 100)
player_rect = playerimg.get_rect(topleft = (x, y))
if player_rect.colliderect(hitbox):
print("hit")
display.fill(white)
player(x,y)
pygame.draw.rect(display, (255,0,0), hitbox,2)
pygame.display.update()
Anyway, I recommend to use pygame.sprite.Sprite, pygame.sprite.Group and pygame.sprite.spritecollide().
Furthermore, your implementation of the QUIT event will not quit the game
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
because the break statement will just break the event loop, but not the application loop.
Use a variable instead:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

Pygame used in VS Code with Pygame Snippets [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN
(1 answer)
Closed 2 years ago.
I have installed vs code and added pygame snippets to use pygame library. My big problem is, every time I try to use any key option of pygame, like pygame.KEYDOWN or pygame.QUIT it tells me that QUIT is not a function of pygame. Can someone help me?
Everything else seems to work, like display or surface
even pygame.key.get_pressed() don’t make problems.
import pygame, random, sys
from pygame.locals import *
from pygame.key import *
def set_Background():
screen = pygame.display.set_mode((500,500))
surface = pygame.image.load('Background.png')
surface = pygame.transform.scale(surface, (500, 500))
screen.blit(surface, (0,0))
pygame.display.update()
return screen
def set_Enemy():
enemy = pygame.image.load('Enemy.png')
enemy = pygame.transform.scale(enemy, (50, 50))
return enemy
def set_Player():
player = pygame.image.load('Player.png')
player = pygame.transform.scale(player, (70, 70))
return player
RUNNING = True
while RUNNING:
background = set_Background()
enemy = set_Enemy()
player = set_Player()
enemy_rect = enemy.get_rect()
player_rect = player.get_rect()
e_x = random.randint(10,450)
e_y = random.randint(10,450)
background.blit(enemy, (e_x, e_y))
pygame.display.update()
for event in pygame.event.get():
key = pygame.key.get_pressed()
if event.type == key[pygame.K_ESCAPE]:
#module pygame has no K_ESCAPE member
sys.exit()
if event.type == pygame.QUIT:
#says module pygame has no QUIT member
sys.exit()
pygame.key.get_pressed() shouldn't be in the event loop, but in the main while loop. In the event loop you need to check if the event type is pygame.QUIT and then set the running flag to False.
Here's a fixed version:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
running = True # Uppercase names are for constants not variables.
while running:
# The event loop.
for event in pygame.event.get():
# If a pygame.QUIT event is in the queue.
if event.type == pygame.QUIT:
running = False
# To check if it was a `KEYDOWN` event.
elif event.type == pygame.KEYDOWN:
# If the escape key was pressed.
if event.key == pygame.K_ESCAPE:
running = False
# Use pygame.key.get_pressed to see if a key is held down.
# This should not be in the event loop.
key = pygame.key.get_pressed()
if key[pygame.K_UP]:
print('up arrow pressed')
screen.fill((30, 30, 30))
pygame.display.flip()
clock.tick(60)
Add from pygame.locals import * at the top of your code.
You are mixing two types of key presses in one go. You should instead either
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SOMEKEY:
do_something()
or
keys = pygame.key.get_pressed()
if keys[pygame.K_somekey]:
do_something()
so the code above with the pygame.key.get_pressed() should not be in the event loop

clearing the screen with a key press in pygame [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Pygame level/menu states
(2 answers)
Closed 2 years ago.
this code is supposed to go to a different function which clears the screen when you hit z, and it does do that, but whenever I let go of z it switches back to the title function and redraws everything.
I'm trying to use it as a "hit this key to go to a new window" type thing and the screenfill is just a fill-in for now, so you can see why that's a problem. for the record I'm using python 3.6.
I've also already tried putting global start in the top of the title function, but then the z key does nothing. please and thanks!
import pygame
from pygame.locals import *
import random
pygame.init()
LOGO = pygame.image.load("kelogo.png")
savannah = pygame.image.load("savannah.png")
hitkey = pygame.image.load("hitkey.png")
display_width = 800
display_height = 600
white = (255, 255, 255)
game_display = pygame.display.set_mode((display_width, display_height))
start = False
def main_game():
while start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.fill(white)
pygame.display.update()
def title():
intro = False
while not intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.blit(savannah, ((0),(0)))
game_display.blit(LOGO, ((0),(100)))
game_display.blit(hitkey, ((130),(100)))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z:
start = True
main_game()
pygame.display.update()
title()
main_game()
pygame.quit()
quit()

Categories

Resources