pygame绘图应用-Pie Game

结合上一章应用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()
            
            
            

效果:

屏幕快照 2017-07-01 上午1.47.13.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pygame图形接口 使用pygame.image模块,可以对图像进行读取和保存。 使用pygame.image....
    sssally92阅读 17,692评论 1 25
  • 大部分人提起儿童编程,就会想到Scratch,然而当儿童升入中学,学习什么语言比较合适呢?我认为,Python是未...
    少儿创客阅读 4,790评论 2 6
  • 系统基础库Category/Util 一套Category类型的库,附带很多自定义控件sstoolkit将Bloc...
    DDY阅读 690评论 0 0
  • 4月份开启了对自我的探索,感到成长很快,灰常开心。(4月份的复盘见这篇文章哈2017年4月复盘:对自己,更坦诚) ...
    职场妈妈关系教练_文静阅读 206评论 0 2
  • 每个人都有一个梦想,但不是每个人都能实现梦想,都说梦想和现实相距太远,似乎梦想就是遥不可及,就是雾里看花水中捞月,...
    243李楠阅读 132评论 0 0