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

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()

Related

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

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.

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

How to draw a player sprite on the screen [duplicate]

This question already has answers here:
How to draw images and sprites in pygame?
(4 answers)
Closed 7 days ago.
The code sample below does not work as intended.
import pygame as pg
import time as t
import math as m
import random as r
import os # this efficiently imports the necessary modules
pg.init()
screen = pg.display.set_mode((500, 500))
pg.display.set_caption("bullet hell")
clock = pg.time.Clock()
clock.tick(60)
player = pg.image.load("player.png")
def loopkill():
if event.type == pg.QUIT:
running = False
def loop(): # this is the mainloop for pygame
running = True
while running:
for event in pg.event.get():
t.sleep(100)
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
print("ready")
pg.surface.blit(player, (250, 250))
g.draw(screen)
g.update()
pg.display.flip()
# Quit Pygame
pg.quit()
loop()
Expected a player sprite, however got a blank screen instead.
I'm suspicious of the t.sleep(100) that would make your program stop for 100 seconds every time there is an event. Also, the functions which draw the screen are within the event loop, so they are only called when there are events.

Quit Algorithm pygame Not working properly [duplicate]

This question already has an answer here:
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
(1 answer)
Closed 2 years ago.
In this I want the program to close when they hit the quit button but if I enable my Check_Key_Press() function, the Close() function does not work however If I comment out the Check_Key_Press() function then it works again.
import pygame
pygame.init()
width, height = 500,500
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tic Tac Toe(GUI)")
clock = pygame.time.Clock()
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
Tile_dime = 59
Diff = (Tile_dime+1)/2
board_det = [['-',(width/2-3*Diff,height/2-3*Diff)],['-',(width/2-Diff,height/2-3*Diff)],['-',(width/2+Diff,height/2-3*Diff)],
['-',(width/2-3*Diff,height/2-Diff)],['-',(width/2-Diff,height/2-Diff)],['-',(width/2+Diff,height/2-Diff)],
['-',(width/2-3*Diff,height/2+Diff)],['-',(width/2-Diff,height/2+Diff)],['-',(width/2+Diff,height/2+Diff)]]
def draw_board():
for i in range(len(board_det)):
pygame.draw.rect(win, white, [board_det[i][1], (Tile_dime, Tile_dime)])
def Close():
global run
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
def Check_Key_Press():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
pass
if event.key == pygame.K_LEFT:
pass
run = True
while run:
clock.tick(60)
draw_board()
Check_Key_Press()
Close()
pygame.display.update()
pygame.event.get() get all the messages and remove them from the queue. If pygame.event.get () is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once and use them in multiple loops or pass the list or events to functions and methods where they are handled:
def Close(event_list):
global run
for event in event_list:
if event.type == pygame.QUIT:
run = False
def Check_Key_Press(event_list):
for event in event_list:
if event.type == pygame.KEYDOWN:
pass
if event.key == pygame.K_LEFT:
pass
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
draw_board()
Check_Key_Press(event_list)
Close(event_list)
pygame.display.update()

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

Categories

Resources