一个python游戏源码
#finalPyPong.py import pygame,sys class MyBallClass(pygame.sprite.Sprite):
def __init__(self,image_file,speed,location=[0,0]):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = location
self.speed = speed def move(self):
global points,score_text self.rect = self.rect.move(self.speed) if self.rect.left < 0 or self.rect.right > screen.get_width():
self.speed[0] = -self.speed[0] if self.rect.top < screen.get_height():
hit_wall.play() if self.rect.top <=0 : self.speed[1] = -self.speed[1]
points = points + 1
score_text = font.render(str(points),1,(0,0,0))
get_point.play() class MyPaddleClass(pygame.sprite.Sprite):
def __init__(self,location=[0,0]):
pygame.sprite.Sprite.__init__(self)
image_surface = pygame.surface.Surface([100,20])
image_surface.fill([0,0,0])
self.image = image_surface.convert()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = location pygame.init()
pygame.mixer.init() pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1) hit = pygame.mixer.Sound("hit_paddle.wav")
hit.set_volume(0.4) new_life = pygame.mixer.Sound("new_life.wav")
new_life.set_volume(0.5) splat = pygame.mixer.Sound("splat.wav")
splat.set_volume(0.6) hit_wall = pygame.mixer.Sound("hit_wall.wav")
hit_wall.set_volume(0.4) get_point = pygame.mixer.Sound("get_point.wav")
get_point.set_volume(0.2) bye = pygame.mixer.Sound("game_over.wav")
bye.set_volume(0.6) screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock() myBall = MyBallClass("wackyball.bmp",[12,6],[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
lives = 3
points = 0 font = pygame.font.Font(None,50)
score_text = font.render(str(points),1,(0,0,0))
textpos = [10,10]
done = False while 1:
clock.tick(30)
screen.fill([255,255,255]) for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
paddle.rect.centerx = event.pos[0] if pygame.sprite.spritecollide(paddle,ballGroup,False):
hit.play() myBall.speed[1] = -myBall.speed[1] myBall.move() if not done:
screen.blit(myBall.image,myBall.rect)
screen.blit(paddle.image,paddle.rect)
screen.blit(score_text,textpos) for i in range(lives):
width = screen.get_width()
screen.blit(myBall.image,[width-40*i,20])
pygame.display.flip() if myBall.rect.top >= screen.get_rect().bottom:
if not done:
splat.play()
lives = lives - 1 if lives <= 0:
if not done:
pygame.time.delay(1000)
bye.play() final_text1 = "Game Over"
final_text2 = "Your final socre is: " + str(points) ft1_font = pygame.font.Font(None,70)
ft1_surf = font.render(final_text1,1,(0,0,0)) ft2_font = pygame.font.Font(None,50)
ft2_surf = font.render(final_text2,1,(0,0,0)) screen.blit(ft1_surf,[screen.get_width()/2 - ft1_surf.get_width()/2,100])
screen.blit(ft2_surf,[screen.get_width()/2 - ft2_surf.get_width()/2,200]) pygame.display.flip()
done = True pygame.mixer.music.fadeout(2000)
else:
pygame.time.delay(1000)
new_life.play() myBall.rect.topleft = [50,50]
screen.blit(myBall.image,myBall.rect)
pygame.display.flip() pygame.time.delay(1000)
一个python游戏源码的更多相关文章
- 随手用python写一个下载jdk源码爬虫
最近在研读jdk源码,网上找了下资源,发现都不完整. 后来新发现了一个有完整源码的地方,主要包括了java,c,c++的东西,装逼需要,就想拿来玩玩.但是,找了好多种下载打开的方式,发现都不对.于是, ...
- 比较不错的一个ios找茬游戏源码
找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...
- html5 canvas简易版捕鱼达人游戏源码
插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...
- ios跑酷游戏源码完整版
今天在网上看到了一个很流行的ios游戏源码,酷跑游戏源码,个人下载感觉非常不错,运行起来非常不错的,大家可以研究一下吧,由于源码文件较大,没有上传,请大家见谅. 由于文件较大,没有上传了,大家可以到这 ...
- ios源码-ios游戏源码-ios源码下载
游戏源码 一款休闲类的音乐小游戏源码 该源码实现了一款休闲类的音乐小游戏源码,该游戏的源码很简单,而且游戏的玩法也很容易学会,只要我们点击视图中的grid,就可以 人气:2943运行环境:/Xco ...
- android版猜拳游戏源码分享
android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...
- 飞翔的圆(Flappy Bird)游戏源码
这个源码是一个不错的休闲类的游戏源码,飞翔的圆(Flappy Bird)游戏源码V1.0,本项目是一个仿Flappy Bird的小游戏,只不过是把Flappy Bird里面的鸟替换成了简单的圆.感兴趣 ...
- ios水果风暴游戏源码下载
游戏源码是从那个IOS教程网IOS.662p.com分享给大家的. 这是一款ios水果风暴游戏源码下载,介绍给大家一下,喜欢的朋友可以下载学习一下吧.应用介绍:这是一个以获得高分和挑战更高难度为目的的 ...
- Android版的疯狂猜图游戏源码完整版分享
这个游戏源码是在安装教程网那么分享过来的,Android版的疯狂猜图游戏源码完整版分享,也是本人之前很早以前发的一款游戏源码的,大家如果想了解一下,可以看看吧,不说多了,上一个图先吧. > ...
随机推荐
- #leetcode刷题之路29- 两数相除
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- C++创建一个名为Ellipse的椭圆类--练习
题目描述: /*设计名为Ellipse的椭圆类*/ /* 其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类. */ 代码如下: #include<iostream& ...
- Java中Lambda表达式的简单使用
Lambda表达式是Java SE 8中一个重要的新特性.你可以把 Lambda表达式 理解为是一段可以传递的代码 (将代码像数据一样进行传递).可以写出更简洁.更灵活的代码.作为一种更紧凑的代码风格 ...
- Co. - Microsoft - Windows - 快捷键
[F1-F12]键 [Win]键 Windows键就是电脑键盘上是Windows图标的键,一般在Ctrl键和Alt键之间.搜索看整个键盘你会发现键盘上有两个Windows键,主要是左右手设计方便按各种 ...
- python pandas库——pivot使用心得
python pandas库——pivot使用心得 2017年12月14日 17:07:06 阅读数:364 最近在做基于python的数据分析工作,引用第三方数据分析库——pandas(versio ...
- 吐血分享:QQ群霸屏技术教程2017(维护篇)
排名上去,并不是终极稳定,日常维护相当重要. 群排名做上去了,如果不去维护,排名很可能会下去,尤其是咱们做了很多群的时候,完全不会留意到. 为什么不稳定? 1.活跃度下去了,排名当然不稳定,这个需要日 ...
- 一些android的日常笔记
1.textview文本:如果内容多的话,设置下面的一行代码,可以实现滑动. text.setMovementMethod(ScrollingMovementMethod.getInstance()) ...
- JavaScript 转载
JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...
- maven中settings文件的配置
鉴于上一个博客写的maven打包,读者老爷们可能找不到settings文件的配置,这里专门附上settings文件的配置: 有的settings文件只含有一些空标签,需要手动去配置. <?xml ...
- xshell怎样打印
Xshell提供用本地打印机打印终端窗口文本的功能.在Xshell打印时可以沿用终端窗口使用的字体及颜色.且在页面设置对话框可以设置打印纸的边距. 如何设置打印纸的大小和方向: 1.打开xshell ...