1

I tried to draw the circle following my cursor through pygame.draw.circle() . However, when I ran the following code, I can only see a window with black screen with nothing following my cursor.

import pygame
from pygame.locals import *

# initialize pygame
pygame.init()

# set up the display
flags = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.OPENGL
resolution = (800, 600)
win = pygame.display.set_mode(resolution, flags)

# pygame event loop
while True:
    for event in pygame.event.get():
        if not event == None: # do not print anything if there is no action taking place
            print(event)
        if event.type == QUIT: # quit the program
            pygame.quit()
        if event.type == MOUSEMOTION:
            pygame.draw.circle(win, (255, 0, 0), event.pos, 5)
        if event.type == KEYDOWN:
            print(event.key, event.mod, pygame.key.name(event.key))
    pygame.display.flip() 
           

Can someone explain what's the issue? And let the pygame.draw.circle() function work as it is supposed to be.

1 Answer 1

1

You must draw the circle in the application loop. Set the position in the event loop, but draw the circle at that position in the application loop. The event loop is executed only when an event occurs, so everything that is drawn there is draw only at that very moment. The application loop is executed continuously and each pass corresponds to one frame. Usually, the scene is redrawn in each frame. This means that the display must be cleared in each frame, the scene must be drawn, and finally the display must be updated.
Unfortunately, this will not solve your problem because you cannot use Pygame's draw module with an OpenGL window. You need to create a default (none OpenGL) window (or you need to draw everything with the OpenGL API).

Working example:

import pygame
from pygame.locals import *

# initialize pygame
pygame.init()

# set up the display
#flags = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.OPENGL
resolution = (800, 600)
#win = pygame.display.set_mode(resolution, flags)
win = pygame.display.set_mode(resolution)
clock = pygame.time.Clock()

center = (400, 300)

# pygame event loop
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
        if event.type == MOUSEMOTION:
            center = event.pos
            
    win.fill(0)
    pygame.draw.circle(win, (255, 0, 0), center, 5)    
    pygame.display.flip() 

pygame.quit()
exit()

If you want to draw a continuous line, you must draw line segments from one mouse position to the next:

import pygame
from pygame.locals import *

pygame.init()

resolution = (400, 400)
win = pygame.display.set_mode(resolution)
clock = pygame.time.Clock()

prevpos = None

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
        if event.type == MOUSEMOTION:
            if prevpos:
                pygame.draw.line(win, (255, 0, 0), prevpos, event.pos, 10)
            prevpos = event.pos
             
    pygame.display.flip() 

pygame.quit()
exit()

Since the quality of line segments is very poor, you can improve that by drawing individual circles along the line from one mouse position to the next:

import pygame
from pygame.locals import *

pygame.init()

resolution = (800, 600)
win = pygame.display.set_mode(resolution)
clock = pygame.time.Clock()

prevpos = None

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
        if event.type == MOUSEMOTION:
            if prevpos:
                x0, x1 = prevpos[0], event.pos[0]
                y0, y1 = prevpos[1], event.pos[1]
                if abs(x1 - x0) > abs(y1 - y0):
                    for x in range(x0, x1, 1 if x1 > x0 else -1):
                        y = int(y0 + (y1 - y0) * (x - x0)  / (x1 - x0))
                        pygame.draw.circle(win, (255, 0, 0), (x, y), 5)
                else:
                    for y in range(y0, y1, 1 if y1 > y0 else -1):
                        x = int(x0 + (x1 - x0) * (y - y0) / (y1 - y0))
                        pygame.draw.circle(win, (255, 0, 0), (x, y), 5)
            prevpos = event.pos
             
    pygame.display.flip() 

pygame.quit()
exit()
1
  • Thanks so much. After deleting the parameter: pygame.OPENGL, the codes run just fine! Really appreciate that~
    – art
    Jun 4 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.