Speed of an object in pygame?
pygame, python
Solution
You should put the following bit of code in your "while True:" loop somewhere:
clock.tick([insert fps here])
and put this somewhere before the loop:
clock=pygame.time.Clock()
This will not allow the loop to run more than the number of times you enter per second, and hopefully slow the cube down.
Problem
I am writing a simple pygame program that only consists of moving a box around the screen. The box moves very fast and I want to know how to control the speed. In my code the updated position is moved by 1 and not smaller because if the number is not an integer it makes things more complicated. ``` import os, sys import pygame from pygame.locals import * pygame.init() mainClock = pygame.time.Clock() WINDOWWIDTH = 400 WINDOWHEIGHT = 400 windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption("Box") BLACK = (0, 0, 0) RED = (255, 0, 0) WHITE = (255, 255, 255) size1 = 20 size2 = 2 #character = pygame.Rect(30, 30, 20, 30) player = pygame.Surface((40,40)) pos1 = 100 pos2 = 100 MOVESPEED = 6 x = 1 while True: if pos1 == WINDOWWIDTH - 40 and pos1 > 0: pos1 -= 1 x += 1 elif pos1 < WINDOWWIDTH - 40 and x == 1: pos1 += 1 elif x ==2: pos1 -= 1 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_LEFT: pos1 -= 5 if event.key == K_RIGHT: pos1 += 4 windowSurface.fill(WHITE) #screen.blit(character) windowSurface.blit(player, (pos1, pos2)) pygame.display.update() ```