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

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

在游戏中,有很多地方值得我们参考,如加载图片,声音和异常处理等
=========================================
代码部分:
=========================================
#python elephant #Import Modules
import os, pygame
from pygame.locals import * __author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/hongten',
'QQ' : '',
'Version' : '1.0'} if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled') #functions to create our resources
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', fullname)
raise (SystemExit, message)
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect() def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join('data', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error as message:
print('Cannot load sound:', fullname)
raise (SystemExit, message)
return sound #classes for our game objects
class Spear(pygame.sprite.Sprite):
"""moves a clenched spear on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image, self.rect = load_image('spear-w.png', -1)
self.punching = 0 def update(self):
"move the spear based on the mouse position"
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10) def punch(self, target):
"returns true if the spear collides with the target"
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect) def unpunch(self):
"called to pull the spear back"
self.punching = 0 class Elephant(pygame.sprite.Sprite):
"""moves a elephant critter across the screen. it can spin the
monkey when it is punched."""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite intializer
self.image, self.rect = load_image('elephant-nw.png', -1)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 10, 10
self.move = 9
self.dizzy = 0 def update(self):
"walk or spin, depending on the monkeys state"
if self.dizzy:
self._spin()
else:
self._walk() def _walk(self):
"move the monkey across the screen, and turn at the ends"
newpos = self.rect.move((self.move, 0))
if self.rect.left < self.area.left or \
self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newpos def _spin(self):
"spin the monkey image"
center = self.rect.center
self.dizzy = self.dizzy + 12
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else:
rotate = pygame.transform.rotate
self.image = rotate(self.original, self.dizzy)
self.rect = self.image.get_rect(center=center) def punched(self):
"this will cause the monkey to start spinning"
if not self.dizzy:
self.dizzy = 1
self.original = self.image def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
#Initialize Everything
pygame.init()
screen = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Monkey Fever')
pygame.mouse.set_visible(0) #Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250)) #Put Text On The Background, Centered
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pummel The Elephant, And Win $$$", 1, (10, 10, 10))
textpos = text.get_rect(centerx=background.get_width()/2)
background.blit(text, textpos) #Display The Background
screen.blit(background, (0, 0))
pygame.display.flip() #Prepare Game Objects
clock = pygame.time.Clock()
whiff_sound = load_sound('elephant-jump.wav')
punch_sound = load_sound('elephant-mmove.wav')
elephant = Elephant()
spear = Spear()
allsprites = pygame.sprite.RenderPlain((spear, elephant)) #Main Loop
while 1:
clock.tick(60) #Handle Input Events
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == MOUSEBUTTONDOWN:
if spear.punch(elephant):
punch_sound.play() #punch
elephant.punched()
else:
whiff_sound.play() #miss
elif event.type is MOUSEBUTTONUP:
spear.unpunch() allsprites.update() #Draw Everything
screen.blit(background, (0, 0))
allsprites.draw(screen)
pygame.display.flip() #Game Over #this calls the 'main' function when this script is executed
if __name__ == '__main__': main()
源码下载:http://files.cnblogs.com/hongten/spear_elephant.zip
========================================================
More reading,and english is important.
I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。
E | hongtenzone@foxmail.com B | http://www.cnblogs.com/hongten
========================================================
pygame系列_箭刺Elephant游戏_源码下载的更多相关文章
- pygame系列_箭刺Elephant游戏
这个游戏原名为:Chimp,我们可以到: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html 获取到源码和详细的源码讲解 下面是我对游戏 ...
- openlayers4 入门开发系列结合 echarts4 实现散点图(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- leaflet-webpack 入门开发系列三地图分屏对比(附源码下载)
前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...
- cesium 入门开发系列矢量瓦片加载展示(附源码下载)
前言 cesium 入门开发系列环境知识点了解:cesium api文档介绍,详细介绍 cesium 每个类的函数以及属性等等cesium 在线例子 内容概览 cesium 实现矢量瓦片加载效果 源代 ...
- [Java] SSH框架笔记_框架分析+环境搭建+实例源码下载
首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...
- openlayers4 入门开发系列之聚合图篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- openlayers4 入门开发系列之迁徙图篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- openlayers4 入门开发系列之地图工具栏篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- openlayers4 入门开发系列之地图切换篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
随机推荐
- thinkphp报错Call to undefined method app\index\controller\Index::fetch()
因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...
- flask基础之请求处理核心机制(五)
前言 总结一下flask框架的请求处理流程. 系列文章 flask基础之安装和使用入门(一) flask基础之jijia2模板使用基础(二) flask基础之jijia2模板语言进阶(三) flask ...
- 嵌入式linux/android alsa_aplay alsa_amixer命令行用法
几天在嵌入式linux上用到alsa command,网上查的资料多不给力,只有动手一点点查,终于可以用了,将这个使用方法告诉大家,以免大家少走弯路. 0.先查看系统支持哪几个alsa cmd: ll ...
- iBt(001-004)原文与试译
Unit 001 Basic building materials include: timber, mud, stone, marble, brick, tile, steel, and cemen ...
- 005_MAC下的VMware fushion快捷键(折中)
由于MAC和VMware Fushion虚拟机之间有一些快捷键的映射,所以Windows虚拟机就找了一个折中的方案.现总结MAC下的win常用快捷键==> <1>最小化窗口(Alt ...
- An overview of gradient descent optimization algorithms (更新到Adam)
Momentum:解快了收敛速度,同时也减弱了SGD的波动 NAG: 减速了Momentum更新参数太快 Adagrad: 出现频率较低参数采用较大的更新,对于出现频率较高的参数采用较小的,不共用一个 ...
- 在SQL2008和2012里面怎么让显示全部行和编辑 全部而不是200和1000
在sql server2008里面,可能微软考虑到数据量比较大,如果直接返回所有行,可能造成耗费时间过多.所有默认为"编辑前200行"和"返回前1000行".这 ...
- 20165203《Java程序设计》第八周学习总结
20165203<Java程序设计>第八周学习总结 教材学习内容总结 第12章 进程与线程 进程的完成过程:代码加载.执行至执行完毕 线程:一个进程由多个线程组成. 线程的完成过程:自身的 ...
- 【PAT】1015 德才论 (25)(25 分)
1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得 ...
- C++11线程池的实现
什么是线程池 处理大量并发任务,一个请求一个线程来处理请求任务,大量的线程创建和销毁将过多的消耗系统资源,还增加了线程上下文切换开销. 线程池通过在系统中预先创建一定数量的线程,当任务请求到来时从线程 ...