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()