1

I Was creating another flappy bird clone and i was checking for mouse click for flapping, But it sometimes registers the click and sometimes not. I Even tried checking for key press but that did not work either. Can someone help? Here is the code, The checking is in the middle section of the code:

from time import sleep
import pygame
from random import randint

#constant variables
SCREEN_WIDTH, SCREEN_HEIGHT = 288, 512
SPEED = 2

PLAYER_SPEED = 0.1
PLAYER_JUMP_FORCE = 3

#setup window
WIN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

#setup clock
clock = pygame.time.Clock()

#load in pictures
background = pygame.image.load('background.png')
ground = pygame.image.load('base.png')
bird = pygame.image.load('yellowbird.png')
pipe_down = pygame.image.load('pipe.png')
pipe_up = pygame.transform.rotate(pipe_down, 180)



def draw(background, ground, ground_x, player, player_y, pipe_down, pipe_up, pipe_x, pipe_x2, pipe_y_offset,pipe_y_offset2):
    WIN.blit(background, (0, 0))
    WIN.blit(player, (50, player_y))
    WIN.blit(pipe_down, (pipe_x, 350 + pipe_y_offset))
    WIN.blit(pipe_up, (pipe_x, -100 + pipe_y_offset))
    WIN.blit(pipe_down, (pipe_x2, 350 + pipe_y_offset2))
    WIN.blit(pipe_up, (pipe_x2, -100 + pipe_y_offset2))
    WIN.blit(ground, (ground_x, SCREEN_HEIGHT - 112))



def main():
    #variables
    ground_x = 0

    player_y = SCREEN_HEIGHT / 3
    velocity = 0

    pipe_x = SCREEN_WIDTH + 52
    pipe_x2 = SCREEN_WIDTH + 252
    pipe_y_offset = randint(-100, 0)
    pipe_y_offset2 = randint(-100, 0)

    #game loop
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        #set fps
        dt = clock.tick(60)
        #code here
        #move ground
        ground_x -= SPEED
        if ground_x <= -48:
            ground_x = 0
        #move player
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                velocity = PLAYER_JUMP_FORCE
        velocity -= PLAYER_SPEED
        player_y -= velocity
        
        #player collision
        if player_y >= SCREEN_HEIGHT - 112:
            run = False

        #pipe move
        pipe_x -= SPEED
        pipe_x2 -= SPEED

        if pipe_x < -52:
            pipe_y_offset = randint(-100, 0)
            pipe_x = SCREEN_WIDTH + 52

        if pipe_x2 < -52:
            pipe_y_offset2 = randint(-100, 0)
            pipe_x2 = SCREEN_WIDTH + 52

        #other
        draw(background, ground, ground_x, bird, player_y, pipe_down, pipe_up, pipe_x, pipe_x2, pipe_y_offset, pipe_y_offset2)
        pygame.display.update()
    
main()

The checking for mouse click in the code above: this code is included in the code above

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        velocity = PLAYER_JUMP_FORCE

i tried to do it with the help of event.type and i expected that it would register the click every time.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0