码迷,mamicode.com
首页 > 其他好文 > 详细

pygame 练习之 PIE game (以及简单图形训练)

时间:2016-05-05 22:37:04      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

简单的大饼游戏,掌握pygame中直线以及圆弧的画法,以及对输入的响应。

 

技术分享
 1 import math
 2 import pygame, sys
 3 from pygame.locals import *
 4 pygame.init()
 5 screen = pygame.display.set_mode((600, 500))
 6 pygame.display.set_caption("The Pie Game -- Press 1 2 3 4")
 7 myfont = pygame.font.Font(None, 60)
 8 
 9 color = 200, 80, 60
10 width = 5
11 x = 300
12 y = 250
13 radius = 200
14 position = x - radius, y - radius, radius*2,  radius*2 
15 
16 piece1 = False
17 piece2 = False
18 piece3 = False
19 piece4 = False
20 
21 # main game loop
22 while True: 
23     for event in pygame.event.get():
24         if event.type == pygame.QUIT:
25             pygame.quit()
26             sys.exit()
27         elif event.type == KEYUP:
28             if event.key == pygame.K_ESCAPE:
29                 pygame.quit()
30                 sys.exit()
31             elif event.key == pygame.K_1:
32                 piece1 = True
33             elif event.key == pygame.K_2:
34                 piece2 = True
35             elif event.key == pygame.K_3:
36                 piece3 = True
37             elif event.key == pygame.K_4:
38                 piece4 = True
39     #clear the screen    
40     screen.fill((0,0,200))
41 
42     textImg1 = myfont.render("1", True, color)
43     screen.blit(textImg1, (x+radius/2, y-radius/2))
44 
45     textImg2 = myfont.render("2", True, color)
46     screen.blit(textImg2, (x-radius/2, y-radius/2))
47 
48     textImg3 = myfont.render("3", True, color)
49     screen.blit(textImg3, (x-radius/2, y+radius/2))
50 
51     textImg4 = myfont.render("4", True, color)
52     screen.blit(textImg4, (x+radius/2, y+radius/2))
53 
54     #should the pieces be drawn?
55     if piece1:
56         start_angle = math.radians(0)
57         end_angle   = math.radians(90)
58         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
59         pygame.draw.line(screen, color, (x, y), (x, y-radius), width)
60         pygame.draw.line(screen, color, (x, y), (x+radius, y), width)
61         
62     if piece2:
63         start_angle = math.radians(90)
64         end_angle   = math.radians(180)
65         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
66         pygame.draw.line(screen, color, (x, y), (x, y-radius), width)
67         pygame.draw.line(screen, color, (x, y), (x-radius, y), width)
68 
69     if piece3:
70         start_angle = math.radians(180)
71         end_angle   = math.radians(270)
72         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
73         pygame.draw.line(screen, color, (x, y), (x-radius, y), width)
74         pygame.draw.line(screen, color, (x, y), (x, y+radius), width)
75 
76     if piece4:
77         start_angle = math.radians(270)
78         end_angle   = math.radians(360)
79         pygame.draw.arc(screen, color, position, start_angle, end_angle, width)
80         pygame.draw.line(screen, color, (x, y), (x, y+radius), width)
81         pygame.draw.line(screen, color, (x, y), (x+radius, y), width)
82 
83     #finished?
84     if piece1 and piece2 and piece3 and piece4:
85         color = 0, 255, 0
86         
87     pygame.display.update()
View Code

 

效果图:

技术分享

 

绘制椭圆:

 

pygame 练习之 PIE game (以及简单图形训练)

标签:

原文地址:http://www.cnblogs.com/yoyo-sincerely/p/5463361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!