[pyGame] Key Event not Working !... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [pyGame] Key Event not Working !... (/thread-5350.html) |
[pyGame] Key Event not Working !... - JamieVanCadsand - Sep-29-2017 Hey Python Programmers... I get an script to change the colour of the screen into an other color (screen.fill), but it doesn't works. This my script: import pygame import sys screen = pygame.display.set_mode((800,600)) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True screen.fill((255,255,255)) pygame.display.update() pygame.quit() sys.exit() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: screen.fill((255,000,000)) pygame.display.update()The seccond for loop must be change the screen to an red colour whenether you press K_SPACE. I try to work with events and keys... but pyGame don't change the screen display of my simple test... Can anyone help me to correctivate my code, just i get an example how i can activate an events, use keys in pyGame ?... Can anyone help me to correctivate my code ?... The screen settup is good, but the screen will not change the color... my display screen remains white, but i want activate an event thad makes the screen red, when you press "SPACE" as key... can anyone give my an code and diffrence examples how i can activate pyGame Key Events ?... Thanks for help, Jamie. RE: [pyGame] Key Event not Working !... - nilamo - Sep-29-2017 The second for loop never runs, because you exit the program first. RE: [pyGame] Key Event not Working !... - JamieVanCadsand - Sep-29-2017 Now i get this code... but it still don't work... My Code: import pygame import sys screen = pygame.display.set_mode((800,600)) done = False # Change Colors in my Screen while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key.get_pressed(K_r): screen.fill((255,0,0)) if event.key.get_pressed(K_g): screen.fill((0,255,0)) if event.key.get_pressed(K_b): screen.fill((0,0,255)) pygame.display.update() # Quit my Game while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True screen.fill((0,0,0)) pygame.display.update() pygame.quit() sys.exit()I get problems with quit my game or program in pyGame and my key_inputs don't works... What's wrong with my code, i try to use events to make the display colors of my screen in pyGame changeble when you press R/G/B, but pyGame does'nt... Can anyone help me to write an example and corrivate my code ?... i am an noob in python and pyGame... but i want to learn write software with e.g. pyGame and OpenGL... Can enyone corrivate my code ?... Thanks, Jamie. RE: [pyGame] Key Event not Working !... - nilamo - Sep-29-2017 (Sep-29-2017, 04:52 PM)JamieVanCadsand Wrote: Now i get this code... but it still don't work... Yes it does. There's no errors, it works exactly as you told it to. You have two infinite loops. The second one is where you check if the window should close... but you never get there because you never break out of the first loop. RE: [pyGame] Key Event not Working !... - metulburr - Sep-29-2017 i amswered some question regarding this by reading your other post, but this is what you want. import pygame import sys screen = pygame.display.set_mode((800,600)) done = False #img = pygame.image.load(os.path.join('C:/Users/Gebruiker/Desktop/Renders', 'Render.png')).convert() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: done = True elif event.key == pygame.K_r: screen.fill((255,0,0)) elif event.key == pygame.K_g: screen.fill((0,255,0)) elif event.key == pygame.K_b: screen.fill((0,0,255)) pygame.display.update() pygame.quit() sys.exit() RE: [pyGame] Key Event not Working !... - Mekire - Sep-29-2017 On an unrelated note of style, switch on a dictionary rather than a big if/elif/else block: import sys import pygame as pg COLORS = {pg.K_r : pg.Color("red"), pg.K_g : pg.Color("green"), pg.K_b : pg.Color("blue")} screen = pg.display.set_mode((800,600)) done = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True elif event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: done = True elif event.key in COLORS: screen.fill(COLORS[event.key]) pg.display.update() pg.quit() sys.exit() RE: [pyGame] Key Event not Working !... - nilamo - Sep-29-2017 On an unrelated note, also of style, get rid of sys.exit(). There's no reason to import the sys module for this, and calling exit() at the end of your program is redundant. RE: [pyGame] Key Event not Working !... - Mekire - Sep-29-2017 (Sep-29-2017, 08:31 PM)nilamo Wrote: On an unrelated note, also of style, get rid of sys.exit(). There's no reason to import the sys module for this, and calling exit() at the end of your program is redundant.The sys.exit call is a holdover from running pygame programs with IDLE. IDLE would often hang if it wasn't included. It is strictly not necessary, but often included just for safety.
|