#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游戏源码的更多相关文章

  1. 随手用python写一个下载jdk源码爬虫

    最近在研读jdk源码,网上找了下资源,发现都不完整. 后来新发现了一个有完整源码的地方,主要包括了java,c,c++的东西,装逼需要,就想拿来玩玩.但是,找了好多种下载打开的方式,发现都不对.于是, ...

  2. 比较不错的一个ios找茬游戏源码

    找茬游戏源码 ,这个是一款非常不错的ios找茬游戏源码,该游戏的兼容性非常好的,并且还可以支持ipad和iphone,UI界面设计得也很漂亮,游戏源码真的是一款非常完美,而且又很完整的一款休闲类的游戏 ...

  3. html5 canvas简易版捕鱼达人游戏源码

    插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...

  4. ios跑酷游戏源码完整版

    今天在网上看到了一个很流行的ios游戏源码,酷跑游戏源码,个人下载感觉非常不错,运行起来非常不错的,大家可以研究一下吧,由于源码文件较大,没有上传,请大家见谅. 由于文件较大,没有上传了,大家可以到这 ...

  5. ios源码-ios游戏源码-ios源码下载

    游戏源码   一款休闲类的音乐小游戏源码 该源码实现了一款休闲类的音乐小游戏源码,该游戏的源码很简单,而且游戏的玩法也很容易学会,只要我们点击视图中的grid,就可以 人气:2943运行环境:/Xco ...

  6. android版猜拳游戏源码分享

    android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...

  7. 飞翔的圆(Flappy Bird)游戏源码

    这个源码是一个不错的休闲类的游戏源码,飞翔的圆(Flappy Bird)游戏源码V1.0,本项目是一个仿Flappy Bird的小游戏,只不过是把Flappy Bird里面的鸟替换成了简单的圆.感兴趣 ...

  8. ios水果风暴游戏源码下载

    游戏源码是从那个IOS教程网IOS.662p.com分享给大家的. 这是一款ios水果风暴游戏源码下载,介绍给大家一下,喜欢的朋友可以下载学习一下吧.应用介绍:这是一个以获得高分和挑战更高难度为目的的 ...

  9. Android版的疯狂猜图游戏源码完整版分享

    这个游戏源码是在安装教程网那么分享过来的,Android版的疯狂猜图游戏源码完整版分享,也是本人之前很早以前发的一款游戏源码的,大家如果想了解一下,可以看看吧,不说多了,上一个图先吧.   > ...

随机推荐

  1. js面向对象轮播图写法

    ;;}         ,,,;}         ];     ].].;     ){             ;             ;         }) } Banner.protot ...

  2. hibernate中配置单向多对一关联,和双向一对多,双向多对多

    什么是一对多,多对一? 一对多,比如你去找一个父亲的所有孩子,孩子可能有两个,三个甚至四个孩子. 这就是一对多 父亲是1 孩子是多 多对一,比如你到了两个孩子,它们都是有一个共同的父亲. 此时孩子就是 ...

  3. JDK6 新特性

    JDK6新特性目录导航: Desktop类和SystemTray类 JAXB2实现对象与XML之间的映射 StAX Compiler API 轻量级 Http Server API 插入式注解处理AP ...

  4. centOS下更新yum源

    CentOS下更新yum源 1.使用如下命令,备份/etc/yum.repos.d/CentOS-Base.repo. cp /etc/yum.repos.d/CentOS-Base.repo /et ...

  5. Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素

    /* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...

  6. java web项目使用ant编译将不同的功能代码打包成jar,进而分局点将项目打包成不同的tar.gz包进而部署

    使用ant可以轻松的将一个项目分离代码,直接打包成不同需求的tar.gz包使用 1.build.properties (属性) version.num=1.0 #版本信息 2.build.xml (a ...

  7. 图表制作工具之ECharts

    简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...

  8. WCF入门四[WCF的通信模式]

    一.概述 WCF的通信模式有三种:请求/响应模式.单向模式和双工通信. 二.请求/响应模式 请求/响应模式就是WCF的默认模式,前面几篇随笔中的示例都是这种模式,当客户端发送请求后(非异步状态下),即 ...

  9. Lambda表达式详解【转】

    前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸夸lambda吧:简化了匿名委托的使用,让你让代码更加简洁,优雅.据说它是微软自c#1.0后新增的最重要的功能之一 ...

  10. C++11中decltype的使用

    The decltype type specifier yields the type of a specified expression. The decltype type specifier, ...