结合上一章应用pygame绘图的知识点,做一个简单的小游戏开发。Pie游戏,是一款非常简单的游戏,没有什么难度,但是又有几本的游戏逻辑,玩家任意顺序按下的键盘1、2、3、4随着按下去的键来绘制或隐藏相应的饼块。
#pie game
import pygame
from pygame.locals import *
import sys
import math
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Pie Game")
#color && font
font_color = 255,255,255
bg_color = 0,0,200
line_color = 255,255,200
myfont = pygame.font.Font(None,60)
#position && radius
pos_x = 300
pos_y = 250
radius = 200
position = pos_x - radius,pos_y - radius,radius*2,radius*2
line_width = 4
arc_width= 4
piece1 = False
piece2 = False
piece3 = False
piece4 = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYUP:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_1:
if piece1:
piece1 = False
else:
piece1 = True
elif event.key == pygame.K_2:
if piece2:
piece2 = False
else:
piece2 = True
elif event.key == pygame.K_3:
if piece3:
piece3 = False
else:
piece3 = True
elif event.key == pygame.K_4:
if piece4:
piece4 = False
else:
piece4 = True
screen.fill(bg_color)
# draw font
#draw Arc
if piece1:
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x,pos_y-radius),line_width)
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x+radius,pos_y),line_width)
pygame.draw.arc(screen,line_color,position,math.radians(0),math.radians(90),arc_width)
textImage1 = myfont.render("1",True,font_color)
screen.blit(textImage1,(pos_x+radius/2,pos_y-radius/2))
if piece2:
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x,pos_y-radius),line_width)
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x - radius,pos_y),line_width)
pygame.draw.arc(screen,line_color,position,math.radians(90),math.radians(180),arc_width)
textImage2 = myfont.render("2",True,font_color)
screen.blit(textImage2,(pos_x - radius/2,pos_y-radius/2))
if piece3:
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x-radius,pos_y),line_width)
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x,pos_y+radius),line_width)
pygame.draw.arc(screen,line_color,position,math.radians(180),math.radians(270),arc_width)
textImage3 = myfont.render("3",True,font_color)
screen.blit(textImage3,(pos_x - radius/2,pos_y+radius/2))
if piece4:
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x,pos_y+radius),line_width)
pygame.draw.line(screen,line_color,(pos_x,pos_y),(pos_x+radius,pos_y),line_width)
pygame.draw.arc(screen,line_color,position,math.radians(270),math.radians(360),arc_width)
textImage4 = myfont.render("4",True,font_color)
screen.blit(textImage4,(pos_x+radius/2,pos_y+radius/2))
#refresh
pygame.display.update()
效果: