首页 > 解决方案 > 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.

标签: pythonpygame

解决方案


这里的问题是您没有绘制背景。当您在下一帧绘制新圆圈时,不会覆盖旧圆圈。只需添加gameDisplay.fill(black)到循环的开头。如果您愿意,当然可以更改颜色。


推荐阅读