pygame系列_箭刺Elephant游戏
这个游戏原名为:Chimp,我们可以到:
http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html
获取到源码和详细的源码讲解
下面是我对游戏的改编:
运行效果:

当箭刺到大象的时候,大象的身体就会翻转,并且发出声音,当然没有刺到的时候,也会发出另外的声音。

在游戏中,有很多地方值得我们参考,如加载图片,声音和异常处理等
=========================================
代码部分:
=========================================

1 #python elephant
2
3 #Import Modules
4 import os, pygame
5 from pygame.locals import *
6
7 __author__ = {'name' : 'Hongten',
8 'mail' : 'hongtenzone@foxmail.com',
9 'blog' : 'http://www.cnblogs.com/hongten',
10 'QQ' : '648719819',
11 'Version' : '1.0'}
12
13 if not pygame.font: print('Warning, fonts disabled')
14 if not pygame.mixer: print('Warning, sound disabled')
15
16
17 #functions to create our resources
18 def load_image(name, colorkey=None):
19 fullname = os.path.join('data', name)
20 try:
21 image = pygame.image.load(fullname)
22 except pygame.error as message:
23 print('Cannot load image:', fullname)
24 raise (SystemExit, message)
25 image = image.convert()
26 if colorkey is not None:
27 if colorkey is -1:
28 colorkey = image.get_at((0,0))
29 image.set_colorkey(colorkey, RLEACCEL)
30 return image, image.get_rect()
31
32 def load_sound(name):
33 class NoneSound:
34 def play(self): pass
35 if not pygame.mixer or not pygame.mixer.get_init():
36 return NoneSound()
37 fullname = os.path.join('data', name)
38 try:
39 sound = pygame.mixer.Sound(fullname)
40 except pygame.error as message:
41 print('Cannot load sound:', fullname)
42 raise (SystemExit, message)
43 return sound
44
45
46 #classes for our game objects
47 class Spear(pygame.sprite.Sprite):
48 """moves a clenched spear on the screen, following the mouse"""
49 def __init__(self):
50 pygame.sprite.Sprite.__init__(self) #call Sprite initializer
51 self.image, self.rect = load_image('spear-w.png', -1)
52 self.punching = 0
53
54 def update(self):
55 "move the spear based on the mouse position"
56 pos = pygame.mouse.get_pos()
57 self.rect.midtop = pos
58 if self.punching:
59 self.rect.move_ip(5, 10)
60
61 def punch(self, target):
62 "returns true if the spear collides with the target"
63 if not self.punching:
64 self.punching = 1
65 hitbox = self.rect.inflate(-5, -5)
66 return hitbox.colliderect(target.rect)
67
68 def unpunch(self):
69 "called to pull the spear back"
70 self.punching = 0
71
72
73 class Elephant(pygame.sprite.Sprite):
74 """moves a elephant critter across the screen. it can spin the
75 monkey when it is punched."""
76 def __init__(self):
77 pygame.sprite.Sprite.__init__(self) #call Sprite intializer
78 self.image, self.rect = load_image('elephant-nw.png', -1)
79 screen = pygame.display.get_surface()
80 self.area = screen.get_rect()
81 self.rect.topleft = 10, 10
82 self.move = 9
83 self.dizzy = 0
84
85 def update(self):
86 "walk or spin, depending on the monkeys state"
87 if self.dizzy:
88 self._spin()
89 else:
90 self._walk()
91
92 def _walk(self):
93 "move the monkey across the screen, and turn at the ends"
94 newpos = self.rect.move((self.move, 0))
95 if self.rect.left < self.area.left or \
96 self.rect.right > self.area.right:
97 self.move = -self.move
98 newpos = self.rect.move((self.move, 0))
99 self.image = pygame.transform.flip(self.image, 1, 0)
100 self.rect = newpos
101
102 def _spin(self):
103 "spin the monkey image"
104 center = self.rect.center
105 self.dizzy = self.dizzy + 12
106 if self.dizzy >= 360:
107 self.dizzy = 0
108 self.image = self.original
109 else:
110 rotate = pygame.transform.rotate
111 self.image = rotate(self.original, self.dizzy)
112 self.rect = self.image.get_rect(center=center)
113
114 def punched(self):
115 "this will cause the monkey to start spinning"
116 if not self.dizzy:
117 self.dizzy = 1
118 self.original = self.image
119
120
121 def main():
122 """this function is called when the program starts.
123 it initializes everything it needs, then runs in
124 a loop until the function returns."""
125 #Initialize Everything
126 pygame.init()
127 screen = pygame.display.set_mode((468, 60))
128 pygame.display.set_caption('Monkey Fever')
129 pygame.mouse.set_visible(0)
130
131 #Create The Backgound
132 background = pygame.Surface(screen.get_size())
133 background = background.convert()
134 background.fill((250, 250, 250))
135
136 #Put Text On The Background, Centered
137 if pygame.font:
138 font = pygame.font.Font(None, 36)
139 text = font.render("Pummel The Elephant, And Win $$$", 1, (10, 10, 10))
140 textpos = text.get_rect(centerx=background.get_width()/2)
141 background.blit(text, textpos)
142
143 #Display The Background
144 screen.blit(background, (0, 0))
145 pygame.display.flip()
146
147 #Prepare Game Objects
148 clock = pygame.time.Clock()
149 whiff_sound = load_sound('elephant-jump.wav')
150 punch_sound = load_sound('elephant-mmove.wav')
151 elephant = Elephant()
152 spear = Spear()
153 allsprites = pygame.sprite.RenderPlain((spear, elephant))
154
155 #Main Loop
156 while 1:
157 clock.tick(60)
158
159 #Handle Input Events
160 for event in pygame.event.get():
161 if event.type == QUIT:
162 return
163 elif event.type == KEYDOWN and event.key == K_ESCAPE:
164 return
165 elif event.type == MOUSEBUTTONDOWN:
166 if spear.punch(elephant):
167 punch_sound.play() #punch
168 elephant.punched()
169 else:
170 whiff_sound.play() #miss
171 elif event.type is MOUSEBUTTONUP:
172 spear.unpunch()
173
174 allsprites.update()
175
176 #Draw Everything
177 screen.blit(background, (0, 0))
178 allsprites.draw(screen)
179 pygame.display.flip()
180
181 #Game Over
182
183
184 #this calls the 'main' function when this script is executed
185 if __name__ == '__main__': main()

源码下载:http://files.cnblogs.com/liuzhi/spear_elephant.zip
pygame系列_箭刺Elephant游戏的更多相关文章
- pygame系列_箭刺Elephant游戏_源码下载
这个游戏原名为:Chimp,我们可以到: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html 获取到源码和详细的源码讲解 下面是我对游戏 ...
- pygame系列_小球完全弹性碰撞游戏_源码下载
之前做了一个基于python的tkinter的小球完全碰撞游戏: python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名 ...
- pygame系列_小球完全弹性碰撞游戏
之前做了一个基于python的tkinter的小球完全碰撞游戏: 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名称: 小球完全弹性碰撞游戏规则: 1.游戏初始化的时候,有5个不同 ...
- pygame系列_原创百度随心听音乐播放器_完整版
程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ...
- pygame系列_游戏中的事件
先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ...
- pygame系列_游戏窗口显示策略
在这篇blog中,我将给出一个demo演示: 当我们按下键盘的‘f’键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式 并且在后台我们可以看到相关的信息输出: 上面给出了一个简单的例子,当然在 ...
- pygame系列_弹力球
这是pygame写的弹力球 运行效果: ======================================================== 代码部分: ================= ...
- pygame系列_第一个程序_图片代替鼠标移动
想想现在学校pygame有几个钟了,就写了一个小程序:图片代替鼠标移动 程序的运行效果: 当鼠标移动到窗口内,鼠标不见了,取而代之的是图片..... ========================= ...
- pygame系列_百度随心听_完美的UI设计
这个程序的灵感来自于百度随心听 下面是我的程序截图: 说明: 动作按钮全部是画出来的,没有用到任何图片 用到图片的只有:背景,歌手图片,作者图片 代码正在调试中.... 如果你鼠标移动到黄色小圆里面, ...
随机推荐
- HTML5 Canvas阴影用法演示
HTML5 Canvas阴影用法演示 HTML5 Canvas中提供了设置阴影的四个属性值分别为: context.shadowColor = “red” 表示设置阴影颜色为红色 context.sh ...
- Oracle安装配置流程
Oracle安装流程 第一次自己动手安装oracle,之前对oracle安装配置一窍不通,最后最终弄好.总结下. 1. 安装oracle10gserver端 2. 安装oracle10gclien ...
- ym——安卓巴士总结了近百个Android优秀开源项
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 1.Android团队提供的演示样例项目 假设不是从学习Android SDK中提供的 ...
- linux exec和文件描述符妙用技巧(转)
最近在看<精通unix shell脚本编程>时,看到exec<$1 exec 1>$OUTFILE,一下看的我就蒙了.网上看了大半天,终于搞定,记录如下.对于 Linux 而言 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- Delphi的没落有三个原因(比较贴切)
Delphi的没落有三个原因:1,Object pascal并没有像C/C++那样成为业内标准. 如果你使用C或C++写程序,可以在任何地方进行编译,例如Windows.Linux.手机,甚至是单片机 ...
- 《转载》值得学习!Google的编程样式指南
原网址:http://www.csdn.net/article/2012-10-12/2810689-Google-styleguide 本文分享了Google众多编程语言的样式指南,其中包括C语言. ...
- 让你提前知道软件开发(22):shell脚本文件操作
文章1部分 再了解C语言 shell脚本中的文件操作 [文章摘要] 编写shell脚本时,经常会涉及到对文件的操作,比方从文件里读取一行数据.向文件追加一行数据等. 完毕文件读写操作的方法有非常多,了 ...
- 表达式树动态拼接lambda
动态拼接lambda表达式树 前言 最近在优化同事写的代码(我们的框架用的是dapperLambda),其中有一个这样很普通的场景——界面上提供了一些查询条件框供用户来进行过滤数据.由于dappe ...
- 智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一 ...