简单的大饼游戏,掌握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()
效果图:
绘制椭圆: