Home > OS >  pygame square not moving
pygame square not moving

Time:08-14

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()

CodePudding user response:

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.

CodePudding user response:

pygame.time.Clock().tick(fps) helps the program to get no more than the given number of fps (frame per second).

pygame.display.flip() is the same thing as pygame.display.update(), but if you pass to it one or more pygame.Rect(), it will refresh only the given area of the screen instead of all window. For what you are trying to achieve, any solution is fine. Just remember that if you refresh parts of the screen multiple times at the same time, it may cause them to flicker.

Pay attention to define your x,y variables outside the while loop, or it will reset every progress, and to save the current progress, you need to use -= and = for bot x and y.

The code would be:

import pygame

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

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

# 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.update()

pygame.quit()
exit()

As an example to take advantage of pygame.display.update(), since we know the square will only move one pixel per frame, in each direction, we can just the square two pixel wider with pygame.display.update(pygame.Rect(x-1,y-1,62,62))

  • Related