import pygame
import random
pygame.init()
width,height=900,500
screen=pygame.display.set_mode((width,height))
vel=0.1
def main():
player1=pygame.Rect(5,height//2-20,10,40)
player2=pygame.Rect(0,height//2-20,10,40)
player2=pygame.Rect(width-5-player2.width,height//2-20,10,40)
go=random.randint(0,1)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
screen.fill((0,0,0))
pygame.draw.rect(screen,(255,255,255),player1)
pygame.draw.rect(screen,(255,255,255),player2)
key=pygame.key.get_pressed()
if key[pygame.K_UP] and player2.y-vel>0:
player2.y-=vel
if key[pygame.K_DOWN] and player2.y+vel+player2.height<height:
player2.y+=vel
pygame.display.flip()
if __name__=="__main__":
main()
here is my code. you can see velocity is 1 and min, but it’s run too fast, I don’t know how to make it slower although pygame will round player2.y so I can use float
I tried float but it didn’t work. pygame will round it. If you use 0.5 you will only go down not up
0