PY交易(二)使用Pygame寫一個小遊戲——Pie
Pie遊戲怎麼玩呢。相信我,超級簡單,你玩過就不會再玩了。但是可以通過寫代碼的過程來熟悉Pygame的概念,了解Python概念的基本邏輯,還有用戶交互的環節。
不多說了,直接上代碼,
import math,sysnimport pygamenfrom pygame.locals import *npygame.init()nnscreen = pygame.display.set_mode((600,500))npygame.display.set_caption("The Pie Game -- Press 1,2,3,4")nmyfont = pygame.font.Font(None,60)nncolor = 200,80,60nwidth = 4nx = 300ny = 250nradius = 200nposition = x-radius,y-radius,radius*2,radius*2nnpiece1 = Falsenpiece2 = Falsenpiece3 = Falsenpiece4 = Falsennwhile True:n for event in pygame.event.get():n if event.type == QUIT:n sys.exit()n elif event.type == KEYUP:n if event.key == pygame.K_ESCAPE:n sys.exit()n elif event.key == pygame.K_1:n piece1 = Truen elif event.key == pygame.K_2:n piece2 = Truen elif event.key == pygame.K_3:n piece3 = Truen elif event.key == pygame.K_4:n piece4 = Truen screen.fill((0,0,200))nn textImg1 = myfont.render("1",True,color)n screen.blit(textImg1, (x + radius / 2 - 20, y - radius / 2))n textImg2 = myfont.render("2", True, color)n screen.blit(textImg2, (x - radius / 2, y - radius / 2))n textImg3 = myfont.render("3", True, color)n screen.blit(textImg3, (x - radius / 2, y + radius / 2 - 20))n textImg4 = myfont.render("4", True, color)n screen.blit(textImg4, (x + radius / 2 - 20, y + radius / 2 - 20))nn if piece1:n start_angle = math.radians(0)n end_angle = math.radians(90)n pygame.draw.arc(screen,color,position,start_angle,end_angle,width)n pygame.draw.line(screen,color,(x,y),(x,y-radius),width)n pygame.draw.line(screen,color,(x,y),(x+radius,y),width)n if piece2:n start_angle = math.radians(90)n end_angle = math.radians(180)n pygame.draw.arc(screen,color,position,start_angle,end_angle,width)n pygame.draw.line(screen,color,(x,y),(x,y-radius),width)n pygame.draw.line(screen,color,(x,y),(x-radius,y),width)n if piece3:n start_angle = math.radians(18)n end_angle = math.radians(270)n pygame.draw.arc(screen,color,position,start_angle,end_angle,width)n pygame.draw.line(screen,color,(x,y),(x-radius,y),width)n pygame.draw.line(screen,color,(x,y),(x,y+radius),width)n if piece4:n start_angle = math.radians(270)n end_angle = math.radians(360)n pygame.draw.arc(screen,color,position,start_angle,end_angle,width)n pygame.draw.line(screen,color,(x,y),(x,y+radius),width)n pygame.draw.line(screen,color,(x,y),(x+radius,y),width)nn if piece1 and piece2 and piece3 and piece4:n color = 0,255,0nn pygame.display.update()n
規則很簡單,依次按下1,2,3,4就可以了,怎麼樣,自己動手試試看吧。
以後還會繼續寫,更深入的
推薦閱讀:
※如何學python-第十二課 邏輯運算符-成員運算符
※利用python進行數據分析之準備(一)
※有沒有什麼軟體能夠在Win10系統下將電腦重複的工作自動實現?
※為什麼 Basic 能長期盤踞編程語言排行榜第 6 名,佔有率比 PHP、Python 還高?
※某測試模擬器性能優化-從系統角度思考問題