Pygame can't create if statment for restart screen? [duplicate] - python
This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
Closed 29 days ago.
Im trying to create a code for creating restart section. I created a variable and set to True.
And said if that variable true do everything in game if not press space and restart the game.
Now space bar now working and games not restarting. What is wrong? And every time I ask something on this site I get bullied over here so take it easy Im pretty new.
Code:
if game_active:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player_rect.bottom >= 300:
player_gravity = -20
else:
if event.type == pygame.KEYDOWN and pygame.K_SPACE:
game_active = True
Code for setting false to game_active when player and enemy collapse:
if snail_rect.colliderect(player_rect):
game_active = False
else:
screen.fill('Yellow')
You need to do
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
You missed the event.key part.
Related
How do I solve "AttributeError: 'Event' object has no attribute 'key'"? [duplicate]
This question already has answers here: How to get keyboard input in pygame? (11 answers) Closed last year. My problem appeared when I tried to make my character walk by changing one variable and thus starting an animation.The problem however is that i get an AttributeError where "'Event' object has no attribute 'key'". I find it very strange as I have written this exact code before put this problem has never appeared in previous uses. I have tried rewriting the code many times, putting it outside of the event check and inside but nothing have worked. while turnoff == False: screen.fill(white) # event checks for event in pygame.event.get(): if event.type == pygame.QUIT: turnoff = True elif event.type == pygame.MOUSEBUTTONDOWN: #supposed to identify which key but can't make it work if event.key == pygame.K_DOWN: action -= 1 frame = 0 if event.key == pygame.K_UP: action -= 3 frame = 0 The error I get is if event.key == pygame.K_DOWN: AttributeError: 'Event' object has no attribute 'key'
The MOUSEBUTTONDOWN event has no key attribute. However, the KEDOWN event has a key attribute: for event in pygame.event.get(): if event.type == pygame.QUIT: turnoff = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: action -= 1 frame = 0 if event.key == pygame.K_UP: action -= 3 frame = 0
The error is that you are asking for the event.key when the event is a pygame.MOUSEBUTTONDOWN. You only want to check for a keypress when there is a pygame.KEYDOWN. Your code should look like this: for event in pygame.event.get(): if event.type == pygame.QUIT: turnoff = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: action -= 1 frame = 0 if event.key == pygame.K_UP: action -= 3 frame = 0
There is no such key attribute for MOUSEBUTTONDOWN: Read-only. The event type specific attributes of an event. The dict attribute is a synonym for backward compatibility. For example, the attributes of a KEYDOWN event would be unicode, key, and mod (source) However, according to the event.c's source there might be a button member. That is, if you want to check the mouse button. Otherwise check for event.type == pygame.KEYDOWN.
Player not moving in pygame [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 years ago. I created a program with pygame. The background and player are appearing but the player is not moving. The program is not giving any errors, can you help me? I am using python 3.8.6. Here is some of my code. # Game Loop running = True while running: 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 # Close the game if event.type == pygame.QUIT: running = False
Your code is just displaying the player, not moving him. To move the player, you have, in a first step, to define a speed variable. Then you have to get the rectangle of your player. That will allow you to modify player position by using the speed variable you defined. Also, if you want to move your player, you have to draw the background before drawing the player. Otherwise, every player you draw will not disappear. And don't forget to define the game speed. Code #!/usr/bin/python3 import pygame # set the game speed delay = 10 screen = pygame.display.set_mode((800, 600)) # loading player image and get pos player = pygame.image.load('pixel_ship_yellow.png') player_pos = player.get_rect() # define speed variable speed = [1, 1] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: break # drawing background screen.fill((0,0,0)) # apply speed (position is automatically updated) player_pos = player_pos.move(speed) # drawing player screen.blit(player, player_pos) pygame.display.flip() # set the game speed pygame.time.delay(delay)
You need to check for the event when a user pressed a button to move the character and then update the players position. For example, here's checking if the player pressed the right arrow: while running: for event in pygame.event.get(): player(playerX, playerY) pygame.display.update() # checking if right arrow is being pressed if events.type == pygame.KEYDOWN: if events.key == pygame.K_RIGHT # update players x position here to move right # for example, player.x += 2 # Close the game if event.type == pygame.QUIT: running = False
Displaying images for only a specific period of time in pygame
So I just came to know about pygame and thought it would be cool to use it to make a simple space invaders game.So in my code https://pastebin.com/eksFCbYr I want the image "bullet.png" to be displayed on the screen only during the duration of the spacebar being pressed.But it doesnt seem to work. I first thought that it may be because the image is being drawn below another image.So I changed the positions of the images concerned such that they dont overlap.But that seems to have had no effect,indicating that is not the problem. Could someone identify what mistake i have made in my code.Also please tell me if there are any better ways to do this. Thanks` m = 2 bull = pygame.image.load("bullet.png") t=True while t: screen.fill((50, 50, 50)) for event in pygame.event.get(): if event.type == pygame.QUIT: t = False #keydown statement if event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: n = 1 if event.key == pygame.K_LEFT: p = -1 if event.key == pygame.K_SPACE: m = 0 #keyup statement if event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT or pygame.K_LEFT: n = 0 p = 0 if event.key == pygame.K_SPACE: m = 1 if m == 0: screen.blit(bull, (ab, 100)) background() player1(ab, bc) ab += n+p if ab <= 0: ab = 0 if ab >= 468: ab = 468 pygame.display.update() {This is my first ever post and also i am a complete newbie to python(and even programming for that matter).So please forgive me if my question is not formatted in the right way.}
The reason the bullet is not showing up on screen is because of the keyup statement. Because as it says in your code, the moment the Space key is unpressed, the value 'm' is back to 1 again (m = 1) and the if statement wont be executed. Then if what I said is the problem, I suggest you define a shoot function that blits the bullet on screen and moves it too. It's also better to use the following code instead using keyup or keydown statements: keys = pygame.key.get_pressed() if keys[pygame.K_Space]: player.shoot() This might help with the problem. shoot() is the function I mentioned but you can use some other method that you're comfortable with.
My python character only moves 1 pixel at a time
I am making a small little Parkour Minigame where the character jumps from platform to platform, but I can't seem to get the character to move properly. I made another game and it worked correctly, but this one doesn't. When I hold down the left/right arrow key it only moves 1 pixel at a time. Here are the V Parkour_MoveLeft=Parkour_MoveRight=Parkour_Jump='no' Parkour_Speed=1 Parkour_X=0 Parkour_Y=0 Parkour_Rows=0 Parkour_Col=0 Now here is my code for the part of the game I am having trouble with : if location=='Parkour': Window.fill(Black) WindowW = 700 WindowH = 700 Window=pygame.display.set_mode((WindowW, WindowH),0, 32) pygame.draw.rect(Window, Blue, Parkour_Character) num=0 for point in Parkour_Grids: mat=Parkour_Lvl_1[num] num+=1 if mat=='a': point['collide']='no' if mat=='p': pygame.draw.rect(Window, Green, point['rect']) point['collide']='yes' for point in Parkour_Grids: if point['collide']=='yes': if Parkour_Character.colliderect(point['left']): Parkour_MoveRight='no' if Parkour_Character.colliderect(point['right']): Parkour_MoveLeft='no' if Parkour_Character.colliderect(point['bottom']): Parkour_MoveUp='no' if Parkour_Character.colliderect(point['top']): Parkour_MoveDown='no' Parkour Movement for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_RIGHT: Parkour_MoveRight='yes' if event.key == K_LEFT: Parkour_MoveLeft='yes' if event.type == KEYUP: if event.key == K_RIGHT: Parkour_MoveRight='no' if event.key == K_LEFT: Parkour_MoveLeft='no' if Parkour_MoveLeft=='yes': Parkour_Character.right-=Parkour_Speed if Parkour_MoveRight=='yes': Parkour_Character.right+=Parkour_Speed Level Map Parkour_Lvl_1=['a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','p','p','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','p','p','p','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a', 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a'] while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() Parkour_Grids.append({'rect':pygame.Rect(Parkour_X, Parkour_Y, 80, 30),'right':1,'left':1,'top':1,'bottom':1,'type':'air','collide':'yes'}) Parkour_X+=80 Parkour_Col+=1 if Parkour_Col==40: Parkour_Col=0 Parkour_X=0 Parkour_Y+=70 Parkour_Rows+=1 if Parkour_Rows==10: break for point in Parkour_Grids: point['right']=pygame.Rect(point['rect'].left+70,point['rect'].top , 6, 70) point['left']=pygame.Rect(point['rect'].right-76,point['rect'].top , 6, 70) point['top']=pygame.Rect(point['rect'].left+6,point['rect'].top-15 , 58, 6) point['bottom']=pygame.Rect(point['rect'].left+6,point['rect'].bottom+6 , 58,6) Anyone have any help on what I can do? It's the exact same code at another game I made (with different variables), but this one doesn't seem to work.
You need to raise Parkour_Speed. It is set to one, and because it does not look like you have ever multiplied it or implemented a movement twice, that is exactly how many pixels it will move. Upping this number will make it move more pixels at one time. However, while it isn't explicitly clear if this is your issue, it is also possible that it is not moving continuously when you press the button. If the event finder is not being run constantly, this will definitely happen, however because checking events is implemented at the same time, this is probably not the case (with more code it would be easier to know). Another thing that you must repeatedly implement to continuously move your sprite across the screen is where you sync Parkour_X and Y with the screen. I do not see where you do this in the code, but I think it should be easiest to use the move_ip function to solve your problems. Provided that the screen is being updated, this modification to your code should solve your issues: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_RIGHT: Parkour_MoveRight='yes' if event.key == K_LEFT: Parkour_MoveLeft='yes' if event.type == KEYUP: if event.key == K_RIGHT: Parkour_MoveRight='no' if event.key == K_LEFT: Parkour_MoveLeft='no' if Parkour_MoveLeft=='yes': Parkour_Character.move_ip(-Parkour_Speed,0) if Parkour_MoveRight=='yes': Parkour_Character.move_ip(Parkour_Speed,0) If this also does not solve your problem, everything else seems fine, so it may be useful to include even more code. I know you have a lot of code up already, but simple features in graphical programs such as this one tend to be very interconnected.
Python - Pygame - Learning get_pressed
I am learning python as well as pygame. I am struggling a bit with finding the issue in my code. When I hold down a key and press another key at the same time, and then let go of them in the same order I press them, it sometimes makes my little character move backwards in the opposite direction. Here is my code that I am struggling with: while running: for event in pygame.event.get(): if event.type == KEYDOWN: keystate = pygame.key.get_pressed() if event.type == pygame.QUIT: # if user clicked close running = False # flag done to exit this loop # all game logic / controls should go below this comment elif event.type == KEYDOWN: if keystate[K_SPACE]: print ('pressed space') player.move_up = True elif keystate[K_RIGHT]: print ('pressed right') player.move_right = True elif keystate[K_LEFT]: print ('pressed left') player.move_left = True elif event.type == KEYUP: if keystate[K_SPACE]: print ('released space') player.move_up = False elif keystate[K_RIGHT]: print ('released right') player.move_right = False elif keystate[K_LEFT]: print ('released left') player.move_left = False if player.move_up: player.pos[1] -= 3 if player.move_up == False: if player.pos[1] < ground_level: player.pos[1] +=3 if player.move_right: player.pos[0] += 5 if player.move_left: player.pos[0] -= 5` So far I don't have any actual physics, and he just flies around. But that's okay, I just want to iron out the issues that I am already having.
You never update keystate's value on a KEYUP event. Thus, it still has whatever values it had whenever the last KEYDOWN event fired. To fix this, change this... if event.type == KEYDOWN: keystate = pygame.key.get_pressed() to this: if event.type in (KEYDOWN, KEYUP): keystate = pygame.key.get_pressed()