用pygame做一個簡單的python小游戲—七彩同心圓
這個小游戲原是我同學python課的課后作業,并不是很難,就簡單實作了一下,順便加強一下pygame庫的學習,
玩法:每次點擊滑鼠時,會以滑鼠為圓心,不斷向外擴展圓(類似于水波浪的擴散),從而形成一個同心圓,并達到隨機大小后停止擴展,其中每個同心圓的顏色都是隨機的,
效果圖:


代碼實作:
import pygame, random, sys, time
pygame.init()
screen = pygame.display.set_mode([600, 400])
screen.fill((255, 255, 255))
radiusr = 0
arrradiusr = [0] * 10 # 圓的半徑
arraddradiusr = [0] * 10 # 圓的半徑增量
arrradiusbool = [False] * 10 # 圓是否存在 False代表該索引值下的圓不存在,True代表存在
arrradiusx = [0] * 10 # 圓的坐標x軸
arrradiusy = [0] * 10 # 圓的坐標y軸
RGBx = [0] * 10 # 顏色RGB值第一個值
RGBy = [0] * 10 # 顏色RGB值第二個值
RGBz = [0] * 10 # 顏色RGB值第三個值
while True:
time.sleep(0.1) # 0.1秒
for event in pygame.event.get(): # 監聽器
if event.type == pygame.MOUSEBUTTONDOWN: # 滑鼠按下
num = arrradiusbool.index(False) #獲取圓不存在的索引值
arrradiusbool[num] = True #將該索引值的圓設定為存在
arrradiusr[num] = 0 #該圓的半徑設定為0
arrradiusx[num], arrradiusy[num] = pygame.mouse.get_pos() #獲取滑鼠坐標
RGBx[num] = random.randint(0, 255) #獲取顏色值
RGBy[num] = random.randint(0, 255) #獲取顏色值
RGBz[num] = random.randint(0, 255) #獲取顏色值
pygame.draw.circle(screen, pygame.Color(RGBx[num], RGBy[num], RGBz[num]), #畫圓
(arrradiusx[num], arrradiusy[num]), arrradiusr[num], 1)
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for i in range(10):
if arrradiusbool[i] == False: #如果圓不存在則跳過回圈
pass
else:
if (arrradiusr[i] < random.randint(10, 50)): #隨機圓的大小
arraddradiusr[i] = random.randint(0, 5) #圓的隨機半徑增量
arrradiusr[i] += arraddradiusr[i]
pygame.draw.circle(screen, pygame.Color(RGBx[i], RGBy[i], RGBz[i]), #畫圓
(arrradiusx[i], arrradiusy[i]), arrradiusr[i], 1)
else:
arrradiusbool[i] = False #若圓已達到最大,這將該索引值的圓設定為不存在
pygame.display.update()
END!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/234333.html
標籤:其他
上一篇:HarmonyOS(鴻蒙)運動手表—從零實作投骰子小游戲
下一篇:用Java開發貪食蛇小游戲
