本系列博客介绍以python+pygame库进行小游戏的开发。有写的不对之处还望各位海涵。
我们一同在前几期的博客中已经学到了很多pygame的基本知识了,现在该做个小游戏实战一下了。
前几期博客链接:
第一篇:初识pygame
第二篇:pygame中的IO、数据
第三篇:pygame事件与设备轮询
本次我们要做一个很简单的小游戏:从天上会随机的掉下鱼,你需要使用鼠标操纵猫去接住鱼,丢失一条鱼损失一条命,一共有10条命,同时还要避免接到炸弹。接住鱼会有积分。
游戏效果图如下:
游戏下载地址:http://pan.baidu.com/s/1qWA4xZ2
源代码下载地址:http://pan.baidu.com/s/1i3is15j
还是先上一下完整的源代码吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
1 # -*-coding:utf-8-*- s = ' 2 #AoDaMiao Like Eating Fish 3 import sys, random, time, pygame 4 from pygame.locals import * 5 6 def print_text(font, x, y, text, color=(255,255,255)): 7 imgText = font.render(text, True, color) 8 screen.blit(imgText, (x,y)) 9 10 11 #main program begins 12 pygame.init() 13 screen = pygame.display.set_mode((600,500)) 14 pygame.display.set_caption("嗷大喵爱吃鱼!") 15 font1 = pygame.font.Font(None, 24) 16 font2 = pygame.font.Font(None, 18) 17 font3 = pygame.font.Font(None, 34) 18 pygame.mouse.set_visible(False) 19 white = 255,255,255 20 red = 220, 50, 50 21 yellow = 230,230,50 22 black = 0,0,0 23 cat=pygame.image.load("aodamiao_2.png") 24 width,height=cat.get_size() 25 pic=pygame.transform.scale(cat,(width,height)) 26 fish=pygame.image.load("fish.png") 27 width,height=fish.get_size() 28 fish=pygame.transform.smoothscale(fish,(width//3,height//3)) 29 init=pygame.image.load("init.png") 30 lives = 10 31 score = 0 32 clock_start = 0 33 game_over = 1 34 mouse_x = mouse_y = 0 35 Round =1 36 mine=0 37 mine_png=pygame.image.load("mine.png") 38 cat2=pygame.image.load("aodamiao_3.png") 39 flag=0 40 41 pos_x = 300 42 pos_y = 410-40 43 44 bomb_x = random.randint(0,500) 45 mine_x=random.randint(0,500) 46 bomb_y = -50 47 vel_y = 0.4 48 vel_yy=0.6 49 mine_y=-100 50 51 #repeating loop 52 while True: 53 for event in pygame.event.get(): 54 if event.type == pygame.QUIT: 55 #sys.exit() 56 pygame.quit() 57 exit() 58 elif event.type == MOUSEMOTION: 59 mouse_x,mouse_y = event.pos 60 move_x,move_y = event.rel 61 elif event.type == MOUSEBUTTONUP: 62 if game_over: 63 game_over = False 64 lives = 10 65 score = 0 66 Round =1 67 vel_y=0.4 68 mine=0 69 flag=0 70 pic=cat 71 bomb_y = -50 72 73 keys = pygame.key.get_pressed() 74 if keys[K_ESCAPE]: 75 sys.exit() 76 77 screen.fill((0,0,100)) 78 79 if game_over: 80 screen.blit(init,(60, 60)) 81 print_text(font3, 200, 400,"Clicked To Play!") 82 print_text(font2, 310, 480,"Copyright<a href="http://www.jobbole.com/members/shiyajun2015">@2015</a> developed by xiaoxiami") 83 else: 84 #Round setting 85 if score >300 and score : 86 Round=2 87 elif score >600 and score : 88 Round =3 89 elif score >900 and score : 90 Round=4 91 elif score >1200 and score : 92 Round =5 93 elif score >=1500: 94 Round =6 95 #draw the Round 96 print_text(font1, 280, 0, "Round: " + str(Round)) 97 #speed setting 98 if Round ==1: 99 vel_y=0.4 100 elif Round ==2: 101 vel_y=0.6 102 elif Round ==3: 103 vel_y=0.8 104 elif Round ==4: 105 vel_y=1.0 |