7abc778
7abc778

Reputation: 43

Pygame isn't moving a rectangle that i drew on screen

Here is the code for the game i'm trying to make (a flappy bird remake)

    def move():

        global bird_x
        global bird_y

        pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
        pygame.draw.rect(win,(255,0,0),(bird_x, bird_y, 30, 30))
        pygame.display.update()
    
while True:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        bird_y += 60
    fb.move()
    time.sleep(0.005)

When i try to run this code, the 'bird' (red square) doesn't move, i've changed the keys used, i've taken the movement in and out of functions, and nothing's working. (this is trimmed code, so the variables probably wont exist)

full code:

import time
import random
pygame.init()

win = pygame.display.set_mode((400,600))
pygame.display.set_caption('Flappy Bird')

pipex = 345
pipe1y = -270
pipe2y = 420
width = 65
height1 = 400
height2 = 3000

vel = 5
bird_x = 20
bird_y = 300
isJump = False
jumpCount = 10

class fb:
    def move():
        global pipex
        global yy
        global pipe1y
        global pipe2y
        global bird_x
        global bird_y
        pipex -= 1
        if pipex < -60:
            pipex = 345
            yy = random.randint(-350,0)
            pipe1y = yy
            pipe2y = pipe1y + 555
        pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
        pygame.draw.rect(win,(0, 255, 0), (pipex, pipe1y, width, height1))
        pygame.draw.rect(win,(0, 255, 100), (pipex, pipe2y, width, height2))
        pygame.draw.rect(win,(255,0,0),(bird_x, bird_y, 30, 30))
        pygame.display.update()
    
while True:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        bird_y += 60
    fb.move()
    time.sleep(0.005)

Upvotes: 2

Views: 61

Answers (1)

user14249883
user14249883

Reputation:

Change your main loop to this:

while True:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        bird_y -= 1
    if keys[pygame.K_DOWN]:
        bird_y += 1
    fb.move()
    time.sleep(0.005)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            break

you didn't test for events you need to handle events

PS: I added a down movement

Upvotes: 3

Related Questions