Umseizure
Umseizure

Reputation: 51

pygame square not moving

I cant seem to understand the clock function in pygame as much i search and code it if it is possible can you help me with this code that is trying to simply make the square move with the up down left and right arrows and if possible and if you have time simply help me understand the clock system.

import pygame
import sys
pygame.init()
fps = 30
fpsclock=pygame.time.Clock()
window = pygame.display.set_mode((600, 600))


# main application loop
run = True
while run:
    # limit frames per second
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # clear the display
    window.fill(0)

    # draw the scene   
    color = (255,0,0)
    x = 275
    y = 275
    key_input = pygame.key.get_pressed() #key imputs
    pygame.draw.rect(window, color, pygame.Rect(x,y,60,60))
    pygame.display.flip()
    if key_input[pygame.K_LEFT]:
        x - 1
    if key_input[pygame.K_RIGHT]:
        x + 1
    if key_input[pygame.K_DOWN]:
        y + 1
    if key_input[pygame.K_UP]:
        y - 1
    pygame.display.update()
    fpsclock.tick(fps)


    # update the display
    pygame.display.flip()

pygame.quit()
exit()

Upvotes: 1

Views: 79

Answers (2)

Sarper Makas
Sarper Makas

Reputation: 99

There are some problems about it:

Change x - 1 to x -= 1

And put x = 275 and y = 275 after defining window

Finally:

import pygame
import sys
pygame.init()
fps = 30
fpsclock=pygame.time.Clock()
window = pygame.display.set_mode((600, 600))

x = 275
y = 275
# main application loop
run = True
while run:
    # limit frames per second
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # clear the display
    window.fill(0)

    # draw the scene   
    color = (255,0,0)
    key_input = pygame.key.get_pressed() #key imputs
    
    pygame.draw.rect(window, color, pygame.Rect(x,y,60,60))
    pygame.display.flip()
    if key_input[pygame.K_LEFT]:
        x -= 1
    if key_input[pygame.K_RIGHT]:
        x += 1
    if key_input[pygame.K_DOWN]:
        y += 1
    if key_input[pygame.K_UP]:
        y -= 1

    pygame.display.update()
    fpsclock.tick(fps)


    # update the display
    pygame.display.flip()

pygame.quit()
exit()

Upvotes: 1

Rabbid76
Rabbid76

Reputation: 210878

x -= 1 instead of x - 1 and x += 1 instead of x + 1. Do the same for y. x and y needs to be initialized before the application instead of in the loop. So you have to do x = 275 and y = 275 before the application loop:

import pygame
import sys
pygame.init()
fps = 30
fpsclock=pygame.time.Clock()
window = pygame.display.set_mode((600, 600))

x = 275
y = 275
color = (255,0,0)

# main application loop
run = True
while run:
    # limit frames per second
    fpsclock.tick(fps)

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    key_input = pygame.key.get_pressed() #key imputs
    if key_input[pygame.K_LEFT]:
        x -= 1
    if key_input[pygame.K_RIGHT]:
        x += 1
    if key_input[pygame.K_DOWN]:
        y += 1
    if key_input[pygame.K_UP]:
        y -= 1

    # clear the display
    window.fill(0)

    # draw the scene 
    pygame.draw.rect(window, color, pygame.Rect(x,y,60,60))  

    # update the display
    pygame.display.flip()

pygame.quit()
exit()

if you have time simply help me understand the clock system.

Use pygame.time.Clock to control the frames per second and thus the game speed. The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

fps = 30
fpsclock=pygame.time.Clock()
while run:
    fpsclock.tick(fps)

runs 30 times per second.

Upvotes: 1

Related Questions