一个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版的疯狂猜图游戏源码完整版分享,也是本人之前很早以前发的一款游戏源码的,大家如果想了解一下,可以看看吧,不说多了,上一个图先吧. > ...
随机推荐
- jwplayer
将JW Player嵌入到网页中非常的简单,只需要进行如下3个步骤: 1.解压mediaplayer-viral.zip文件,将jwplayer.js和player.swf文件拷贝到工程中: 2.在页 ...
- C/C++获取CPU等硬件信息&&屏幕截图
打算练习Socket的时候用用,最近有点小事情,没时间继续完善,先把写的这些代码贴上来,有空了再完善一下. HardwareInfo.h #include <stdio.h> #inclu ...
- 在mac上显示网速的软件——iStat Menus 5:
在mac上显示网速的软件——iStat Menus 5: https://bjango.com/mac/istatmenus/ 注册码: Email: 982092332@qq.com SN: GAW ...
- 使用php+gmail 发送邮件
<?php namespace app\index\controller; use think\Controller; use PHPMailer\PHPMailer; class Test e ...
- linux命令之文件系统权限操作常用命令
1. umask:设置权限掩码 语法:umask [参数] 命令说明:umask可以单独使用,可以设置目录与文件的默认权限,默认权限掩码是022,所以默认目录权限是777-022=755,读权限是 ...
- grafana使用Prometheus数据源监控mongo数据库
数据库改用mongo后,监控需求就需要整合进grafana里,由于一直在坚持docker化部署,那么此次也不例外. 1. 安装Prometheus: What is Prometheus? Prome ...
- Hadoop(20)-MapReduce框架原理-OutputFormat
1.outputFormat接口实现类 2.自定义outputFormat 步骤: 1). 定义一个类继承FileOutputFormat 2). 定义一个类继承RecordWrite,重写write ...
- VSCode插件整理
VSCode插件整理 VSCode插件整理 官网地址 vscode常用配置(User Settings文件) 基本插件 前端插件 VUE部分 python MarkDown部分 连接Linux 本地与 ...
- DSP+ARM多核异构开发环境SYSLINK搭建OMAPL138
DSP+ARM多核异构开发环境搭建OMAPL138 注意: 环境为Ubuntu 12.04 只能是这个环境.我甚至在Ubuntu16.04上面安装了VMware,然后,在装了一个Ubuntu 12.0 ...
- 怎么修复网站漏洞之metinfo远程SQL注入漏洞修补
2018年11月23日SINE网站安全检测平台,检测到MetInfo最新版本爆出高危漏洞,危害性较大,影响目前MetInfo 5.3版本到最新的 MetInfo 6.1.3版本,该网站漏洞产生的主要原 ...