简体   繁体   中英

Pygame is changing the size of rect when moving

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM