一个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:输入: ...
- 用户交互input
input() 函数 接收到的都是str,如果输入为数字,打印结果想进行运算,此时需要转义.语法:内容=input("提示信息")这里可以直接获取到用户输入的内容. a = inp ...
- bootstrap-daterangepicker插件运用
引入:daterangepicker.css.daterangepicker.js.moment.js.moment.min.js 链接:https://files.cnblogs.com/files ...
- JDK8 新特性
JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...
- 神级编辑器 sublime text 和 神级插件 emmet
h1{foo}和a[href=#] 生成如下代码 <h1>foo</h1> <a href="#"></a> 嵌套的使用 > ...
- CentOS下禁止防火墙
CentOS下禁止防火墙 1.使用如下命令安装iptables-services. yum install -y iptables-services 2.关闭防火墙. service iptables ...
- python爬虫 爬取steam热销游戏
好久没更新了啊...最近超忙 这学期学了学python 感觉很有趣 就写着玩~~~ 爬取的页面是:https://store.steampowered.com/search/?filter=globa ...
- (数据科学学习手札01)Python与R基本数据结构之异同
Python 1.列表(list) list1 = [i for i in range(10)] list1[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 特点:可遍历,可索引,可切片 ...
- 按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分
SELECT S# as 学生ID ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库 ,(SELECT score FROM S ...
- C++11中std::move的使用
std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...