1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: ss
4
5 importpygame6 importsys7
8 #初始化
9 pygame.init()10
11 size = width,height = 1000,600 #设置屏幕尺寸
12 BLUE = 0,0,255
13 WHITE = 255,255,255
14 BLACK =0,0,015 RED = 255,0,016 GREEN = 0,255,017
18 screen = pygame.display.set_mode(size) #创建surface对象
19 pygame.display.set_caption('画圆及拖拽') #创建标题
20
21 #圆心位置定义
22 position = size[0] // 2 , size[1] // 2
23
24 moving =False25
26 whileTrue:27 for event inpygame.event.get():28 if event.type ==pygame.QUIT:29 sys.exit()30 if event.type == pygame.MOUSEBUTTONDOWN: #获取点击鼠标事件
31 if event.button == 1: #点击鼠标左键
32 moving =True33 if event.type == pygame.MOUSEBUTTONUP: #获取松开鼠标事件
34 if event.button == 1: #松开鼠标左键
35 moving =False36 ifmoving:37 position = pygame.mouse.get_pos() #更新圆心位置为鼠标当前位置
38
39
40
41 screen.fill(WHITE) #填充屏幕
42 #画各种尺寸颜色的圆
43 pygame.draw.circle(screen,BLUE,position,30,1)44 pygame.draw.circle(screen, BLACK, position, 50, 1)45 pygame.draw.circle(screen, RED, position, 80, 1)46 pygame.draw.circle(screen, GREEN, position, 120, 1)47 #刷新屏幕
48 pygame.display.flip()