0
\$\begingroup\$

I am creating a game with Pygame but my collisions are registering too soon. I'm using this code to end the game when the player collides with a pipe:

if player_rect.colliderect(pipe_rect): pygame.QUIT exit() 

It works, but it triggers too soon. If the player comes close to the pipe the game ends, even before they collide.

Here is the whole code:

import pygame pygame.init() gravity = 0 clock = pygame.time.Clock() jumping = False fall = 0 screen = pygame.display.set_mode((600,368)) game_background = pygame.image.load("cloud.png").convert_alpha() player = pygame.image.load("bird.png").convert_alpha() player_rect = player.get_rect(midbottom = (170,200)) #PIPE IMAGE pipe_1 = pygame.image.load("pipe1.png").convert_alpha() pipe_rect = pipe_1.get_rect(midbottom = (500,600)) pipe_2 = pygame.image.load("pipe2.png").convert_alpha() pipe2_rect = pipe_2.get_rect(midbottom = (500,200)) #set 2 pipe_3 = pygame.image.load("pipe3.png").convert_alpha() pipe3_rect = pipe_3.get_rect(midbottom = (720,100)) pipe_4 = pygame.image.load("pipe4.png").convert_alpha() pipe4_rect = pipe_4.get_rect(midbottom = (720,500)) #set 3 pipe_5 = pygame.image.load("pipe5.png").convert_alpha() pipe5_rect = pipe_5.get_rect(midbottom = (920,600)) pipe_6 = pygame.image.load("pipe6.png").convert_alpha() pipe6_rect = pipe_6.get_rect(midbottom = (920,200)) #set 4 pipe_7 = pygame.image.load("pipe7.png").convert_alpha() pipe7_rect = pipe_7.get_rect(midbottom = (1120,500)) pipe_8 = pygame.image.load("pipe8.png").convert_alpha() pipe8_rect = pipe_8.get_rect(midbottom = (1120,100)) #set 5 pipe_9 = pygame.image.load("pipe9.png").convert_alpha() pipe9_rect = pipe_9.get_rect(midbottom = (1320,200)) pipe_10 = pygame.image.load("pipe10.png").convert_alpha() pipe10_rect = pipe_10.get_rect(midbottom = (1320,600)) #set 6 pipe_11 = pygame.image.load("pipe11.png").convert_alpha() pipe11_rect = pipe_11.get_rect(midbottom = (1520,450 )) pipe_12 = pygame.image.load("pipe12.png").convert_alpha() pipe12_rect = pipe_12.get_rect(midbottom = (1520,50)) #set 7 pipe_13 = pygame.image.load("pipe13.png").convert_alpha() pipe13_rect = pipe_13.get_rect(midbottom = (1720,550 )) pipe_14 = pygame.image.load("pipe14.png").convert_alpha() pipe14_rect = pipe_14.get_rect(midbottom = (1720,150)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.blit(game_background,(0,0)) keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_SPACE] and player_rect.bottom >= 0: jump = True gravity = -7 gravity += 1 player_rect.y += gravity if player_rect.bottom >= 366: player_rect.bottom = 366 screen.blit(player,player_rect) #pipe 1 screen.blit(pipe_1,pipe_rect) pipe_rect.x -=3 if pipe_rect.right <= 0: pipe_rect.left = 1200 #pipe 2 screen.blit(pipe_2,pipe2_rect) pipe2_rect.x -=3 if pipe2_rect.right <= 0: pipe2_rect.left = 1200 #pipe 3 screen.blit(pipe_3,pipe3_rect) pipe3_rect.x -=3 if pipe3_rect.right <= 0: pipe3_rect.left = 1200 #pipe 4 screen.blit(pipe_4,pipe4_rect) pipe4_rect.x -=3 if pipe4_rect.right <= 0: pipe4_rect.left = 1200 #pipe 5 screen.blit(pipe_5,pipe5_rect) pipe5_rect.x -=3 if pipe5_rect.right <= 0: pipe5_rect.left = 1200 #pipe 6 screen.blit(pipe_6,pipe6_rect) pipe6_rect.x -=3 if pipe6_rect.right <= 0: pipe6_rect.left = 1200 #pipe 7 screen.blit(pipe_7,pipe7_rect) pipe7_rect.x -=3 if pipe7_rect.right <= 0: pipe7_rect.left = 1200 #pipe 8 screen.blit(pipe_8,pipe8_rect) pipe8_rect.x -=3 if pipe8_rect.right <= 0: pipe8_rect.left = 1200 #pipe 9 screen.blit(pipe_9,pipe9_rect) pipe9_rect.x -=3 if pipe9_rect.right <= 0: pipe9_rect.left = 1200 #pipe 10 screen.blit(pipe_10,pipe10_rect) pipe10_rect.x -=3 if pipe10_rect.right <= 0: pipe10_rect.left = 1200 #pipe 11 screen.blit(pipe_11,pipe11_rect) pipe11_rect.x -=3 if pipe11_rect.right <= 0: pipe11_rect.left = 1200 #pipe 12 screen.blit(pipe_12,pipe12_rect) pipe12_rect.x -=3 if pipe12_rect.right <= 0: pipe12_rect.left = 1200 #pipe 13 screen.blit(pipe_13,pipe13_rect) pipe13_rect.x -=3 if pipe13_rect.right <= 0: pipe13_rect.left = 1200 #pipe 14 screen.blit(pipe_14,pipe14_rect) pipe14_rect.x -=3 if pipe14_rect.right <= 0: pipe14_rect.left = 1200 if pipe_rect.colliderect(player_rect): pygame.QUIT exit() # gravity += 1 #player_rect.y += gravity #if player_rect.bottom >= 366: player_rect.bottom = 366 #screen.blit(player,player_rect) clock.tick(40) pygame.display.update()` 
\$\endgroup\$
1
  • \$\begingroup\$ I don't have the resources to test this now, but using coordinates like this player_rect = player.get_rect(midbottom = (170,200)) and pipe_rect = pipe_1.get_rect(midbottom = (500,600)), where you use midbottom as a reference, may offset the actual rect you test and the visual. Instead of using midbottom, try using something like topleft to see if it works. Then you should try and position your object using topleft instead before using a more sophisticated way to position your objects based on midbottom. \$\endgroup\$ Commented Jan 22, 2023 at 15:20

1 Answer 1

0
\$\begingroup\$

The problem

The line gravity += 1 will make that variable so great it'll slam your player immediately to your base pipe_rect prematurely ending your game.

To elaborate a bit more ...

Let's look at your framerate, clook.tick(40) which means this will loop through 40 times per second. With gravity set at 0, then each loop you apply gravity += 1, it'll quickly become 0 to 40 to 80 to etc ... This creates the problem bug, because your player_rect.y += gravity line will increasingly pull the player_rect faster than it can resist gravity, without repeatedly pressing the spacebar.

Suggested Solution

* Adding a if statement to limit gravity variable

Below the line gravity += 1, add

if gravity >= 10: gravity = 10 

This will give you player a constant pull to the bottom of the screen, but not to fast. The faster the player presses the spacebar key, the easier the bird can resist the gravity effect. 10 is a arbitrary number, it could be any limit you set with an int.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.