python游戲pygame模塊畫圓及鼠標拖拽移動方法介紹


 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*- 
 3 #Author: ss
 4 
 5 import pygame
 6 import sys
 7 
 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,0
15 RED = 255,0,0
16 GREEN = 0,255,0
17 
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 = False
25 
26 while True:
27     for event in pygame.event.get():
28         if event.type == pygame.QUIT:
29             sys.exit()
30         if event.type == pygame.MOUSEBUTTONDOWN: # 獲取點擊鼠標事件
31             if event.button == 1: # 點擊鼠標左鍵
32                 moving = True
33         if event.type == pygame.MOUSEBUTTONUP: # 獲取松開鼠標事件
34             if event.button == 1: # 松開鼠標左鍵
35                 moving = False
36     if moving:
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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM