2
votes

pygame is changing the size of my rectangle instead of moving the rectangle and it is not what I want. I tried to make the bottom go up as well but it stayed in one area and I don't know how to change this.

import pygame , sys

pygame.init()
clock = pygame.time.Clock()
width = 600
height= 500

red = (255,0,0)
blue = (0,0,255)
green = 0,255,0
screen = pygame.display.set_mode((width, height))

opponent = pygame.Rect((width- 50, height/2,30,30))
opponent2 = pygame.Rect((width- 570, height/2,30,30))

speed1 = 10
speed2 = 10

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()
    opponent.top += speed1
    pygame.draw.rect ( screen, red, opponent )
    pygame.draw.rect ( screen, blue, opponent2 )
    pygame.display.flip()
    clock.tick(144)
1
You do top += X, why would you think that it would move instead of resize? - Cireo
what do you mean - ram kador
I would expect something like move_ip(x, y) instead - Cireo

1 Answers

2
votes

Inside your while loop, add screen.fill((0, 0, 0)) before drawing other things:

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()
    opponent.top += speed1
    screen.fill((0, 0, 0))
    pygame.draw.rect ( screen, red, opponent )
    pygame.draw.rect ( screen, blue, opponent2 )
    pygame.display.flip()
    clock.tick(144)