简体   繁体   中英

Modify ellipse coordinates in pygame

I have been trying to move an ellipse in pygame.

This is my code:

import pygame

import pygame

pygame.init()
oW = 400
oH = 500

black = (0,0,0)
red = (255,0,0)
blue = (0,0,255)

gameDisplay = pygame.display.set_mode((800,800))
gameDisplay.fill(black)


Sun = pygame.draw.circle(gameDisplay, red, (400,400), 60)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        
        #Key input over here
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                oW = oW + 10
            if event.key == pygame.K_d:
                oH = oH + 10
        Orbit = pygame.draw.ellipse(gameDisplay, blue, (200, 250, oW, oH), 1)
    pygame.display.update()

How could I make it so that rather than the ellipse constantly duplicating itself and making copies, it's coordinates would just move the original(first one)? Currently, if you run the program below, the ellipse will make copies of itself in the new coordinates rather than modifying the original so there will be a lot of ellipses if you press a or d. After researching online, some people were suggesting putting a black shape over everything and then redrawing it but that's not really what I'm going for here, I just want the shape to update without constantly duplicating itself.

The problem here is that you aren't drawing a background. When you draw a new circle the next frame, the old circle isn't overwritten. Simply add gameDisplay.fill(black) to the start of your loop. You can of course change the color if you like.

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