python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏
学了一天pygame,用python和pygame写一个简单的挡板弹球游戏
GitHub:
# -*- coding:utf-8 -*-
from sys import exit
import pygame
from pygame.locals import *
pygame.init()
# 创建窗口
ScreenWidth = 500
ScreenHright = 720
ScreenSize = (ScreenWidth, ScreenHright)
Screen = pygame.display.set_mode(ScreenSize, 0, 32)
pygame.display.set_caption("Ly's Easy Ball Game")
# 背景音乐
pygame.mixer.music.load('Sugar.mp3')
pygame.mixer.music.play(-1, 0.0)
# 碰撞音效
CollisionMusic = pygame.mixer.Sound('collision.wav')
# 重新开始按钮音效
ButtonMusic = pygame.mixer.Sound('button.wav')
# 游戏结束音效
GameOverMusic = pygame.mixer.Sound('over.wav')
def GameStart():
# 游戏背景Surface对象
Background = pygame.image.load('GameBackground.jpg').convert()
# 挡板Surface对象
Baffle = pygame.image.load('Baffle.png').convert_alpha()
# 球Surface对象
Ball = pygame.image.load('Ball.png').convert_alpha()
# 挡板位置信息
BaffleX = 140
BaffleY = 600
BaffleSpeed = 1000
BaffleXSpeed = BaffleSpeed
BaffleYSpeed = BaffleSpeed
BaffleMove = {K_LEFT: 0, K_RIGHT: 0, K_UP: 0, K_DOWN: 0}
# 球位置信息
BallX = 235
BallY = 0
BallSpeed = 1000.
BallXSpeed = BallSpeed
BallYSpeed = BallSpeed
# 帧率控制Clock对象
FPSClock = pygame.time.Clock()
# 时间显示Clock对象
ProgramRunClock = pygame.time.get_ticks()
# 时间显示Font对象
RunTimeFont = pygame.font.Font('Jura-DemiBold.ttf', 24)
# 游戏结果
GameResult = ''
while True:
# 接收信息处理
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYDOWN:
if event.key in BaffleMove:
BaffleMove[event.key] = 1
elif event.type == KEYUP:
if event.key in BaffleMove:
BaffleMove[event.key] = 0
# 绘制背景
Screen.blit(Background, (0, 0))
RunTimeStr = str((pygame.time.get_ticks() - ProgramRunClock) / 1000.0)
# print(RunTimeStr)
# 使用render方法显示时间字体
RunTimeSurface = RunTimeFont.render(RunTimeStr, True, (255, 52, 179))
# 显示时间
Screen.blit(RunTimeSurface, (0, 0))
# 距上次调用clock对象时间
SecondTimePassed = FPSClock.tick(60) / 1000.0
# 绘制球
Screen.blit(Ball, (BallX, BallY))
BallX += BallXSpeed * SecondTimePassed
BallY += BallYSpeed * SecondTimePassed
# 判断球边界条件
if BallX > 500 - Ball.get_width():
BallXSpeed = -BallXSpeed
BallX = 500 - Ball.get_width()
elif BallX < 0:
BallXSpeed = -BallXSpeed
BallX = 0
if BallY > 720 - Ball.get_width():
BallYSpeed = -BallYSpeed
BallY = 720 - Ball.get_width()
elif BallY < 0:
BallYSpeed = -BallYSpeed
BallY = 0
# 定位挡板移动后坐标
BaffleX -= BaffleMove[K_LEFT] * BaffleXSpeed * SecondTimePassed
BaffleX += BaffleMove[K_RIGHT] * BaffleXSpeed * SecondTimePassed
BaffleY -= BaffleMove[K_UP] * BaffleYSpeed * SecondTimePassed
BaffleY += BaffleMove[K_DOWN] * BaffleYSpeed * SecondTimePassed
# 判断挡板边界条件
if BaffleX > 500 - Baffle.get_width():
BaffleX = 500 - Baffle.get_width()
elif BaffleX < 0:
BaffleX = 0
if BaffleY > 720 - 45 - Baffle.get_height():
BaffleY = 720 - 45 - Baffle.get_height()
elif BaffleY < 720 - Baffle.get_height() * 3:
BaffleY = 720 - Baffle.get_height() * 3
# 绘制挡板
Screen.blit(Baffle, (BaffleX, BaffleY))
# 判断球碰撞挡板条件
# 挡板左上角
if BallX == BaffleX - Ball.get_width() and BallY == BaffleY - Ball.get_height():
BallXSpeed = -BallXSpeed
BallYSpeed = -BallYSpeed
CollisionMusic.play()
# 挡板左下角
elif BallX == BaffleX - Ball.get_width() and BallY == BaffleY + Baffle.get_height():
BallXSpeed = -BallXSpeed
BallYSpeed = -BallYSpeed
CollisionMusic.play()
# 挡板右上角
elif BallX == BaffleX + Baffle.get_width() and BallY == BaffleY - Ball.get_height():
BallXSpeed = -BallXSpeed
BallYSpeed = -BallYSpeed
CollisionMusic.play()
# 挡板右下角
elif BallX == BaffleX + Baffle.get_width() and BallY == BaffleY + Baffle.get_height():
BallXSpeed = -BallXSpeed
BallYSpeed = -BallYSpeed
CollisionMusic.play()
# 挡板上表面
elif BallX > BaffleX and BallX < BaffleX + Baffle.get_width() and BallY > BaffleY - Ball.get_height() and BallY < BaffleY:
BallYSpeed = -BallYSpeed
BallY = BaffleY - Ball.get_height()
CollisionMusic.play()
# 挡板下表面
elif BallX > BaffleX and BallX < BaffleX + Baffle.get_width() and BallY < BaffleY + Baffle.get_height() and BallY > BaffleY:
BallYSpeed = -BallYSpeed
BallY = BaffleY + Baffle.get_height()
CollisionMusic.play()
# 挡板左侧面
elif BallY > BaffleY and BallY < BaffleY + Baffle.get_height() and BallX > BaffleX - Ball.get_width() and BallX < BaffleX:
BallXSpeed = -BallXSpeed
BallX = BaffleX
CollisionMusic.play()
# 挡板右侧面
elif BallY > BaffleY and BallY < BaffleY + Baffle.get_height() and BallX > BaffleX + Baffle.get_width() - Ball.get_width() and BallX < BaffleX + Baffle.get_width():
BallXSpeed = -BallXSpeed
BallX = BaffleX + Baffle.get_width()
CollisionMusic.play()
if BallY > 720 - 45:
GameResult = RunTimeStr
GameOverMusic.play()
return GameResult
# 刷新显示
pygame.display.update()
def GameResult(GameResult):
# 游戏结果背景Surface对象
GameResultBackground = pygame.image.load('GameResultBackground.png').convert()
# 游戏结果引导
ResultHint = pygame.image.load('ResultFont.png').convert_alpha()
# 游戏结果Font对象
GameResultFont = pygame.font.Font('EuroBold.ttf', 100)
# 重新开始按钮
ReStartButton = pygame.image.load('ReStartButton.png').convert_alpha()
# 重新开始Hover按钮
ReStartButtonHover = pygame.image.load('ReStartButtonHover.png').convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN and 150 <= event.pos[
0] <= 150 + ReStartButton.get_width() and 450 <= event.pos[1] <= 450 + ReStartButton.get_height():
ButtonMusic.play()
return True
# 游戏结果背景
Screen.blit(GameResultBackground, (0, 0))
# 游戏结果引导
Screen.blit(ResultHint, (45, 200))
RunTimeSurface = GameResultFont.render(GameResult, True, (255, 69, 0))
Screen.blit(RunTimeSurface, (90, 270))
# 重新开始游戏按钮
MouseX, MouseY = pygame.mouse.get_pos()
if 150 <= MouseX <= 150 + ReStartButton.get_width() and 450 <= MouseY <= 450 + ReStartButton.get_height():
Screen.blit(ReStartButtonHover, (150, 450))
else:
Screen.blit(ReStartButton, (150, 450))
# 游戏结果
pygame.display.update()
if __name__ == '__main__':
flag = True
while flag:
GameResultStr = GameStart()
if GameResultStr != '':
flag = GameResult(GameResultStr)
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏相关推荐
- python编写2048游戏代码_python pygame实现2048游戏
实现2048相对来说比较简单,用4*4的二维数组保存地图,pygame.key.get_pressed()获取键盘操作,详见代码. 效果图 代码 # -*- coding: utf-8 -*- #!/ ...
- python塔防小游戏代码_Python制作塔防小游戏
开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 原理介绍 游戏规则简介: 玩家通过建造箭塔抵御敌人的进攻. 每隔一段时间,将会有一波敌人从 ...
- python之穿越火线游戏代码_Python 大作业之五子棋游戏(附代码)
Python 大作业--五子棋游戏 姓名:吴欣 学号: 姓名:张雨清 学号: 一 游戏介绍: 我们设计的是五子棋游戏,支持两人一个鼠标对下,黑方用左 键单击,白方用右键单击,谁先下均可,落子无悔,下过 ...
- python中从键盘输入的代码_python如何从键盘获取输入实例
python中使用input()函数来获取用户输入 函数 input() 让程序暂停运行,等待用户输入一些文本,获取用户的输入后,Python将其存储到一个变量中,以方便后期使用. name = in ...
- python十点半游戏代码_Python实现Pig Latin小游戏实例代码
前言: 本文研究的主要是Python实现pig Latin小游戏的简单代码,具体介绍如下. Pig Latin是一个语言游戏. 步骤: 1.让用户输入一个英文单词 2.确保用户输入一个有效单词 3.将 ...
- python接水果游戏代码_Python开发接水果小游戏编程
我研发的Python游戏引擎Pylash已经更新到1.4了.现在我们就来使用它完成一个极其简单的小游戏:接水果.以下是游戏截图: vc/yvPy/2NbGyMvO79LGtq+jrMq5yMvO79P ...
- python井字棋游戏代码_python实现井字棋游戏
python实现井字棋游戏 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: python实现井字棋游戏.txt ] (友情提示:右键点上行txt文档名->目标 ...
- python成语游戏代码_Python基础,猜成语小游戏
猜成语 闲的无事,无聊的写bug,突然觉得可以随便写个猜成语小游戏,正好可以解闷 Python随机库,random random是Python的随机库,有这样几个简单的用法 在使用random前要用i ...
- python中从键盘输入的代码_Python读取键盘输入的2种方法
Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘.如下: 1.raw_input 2.input raw_input函数 raw_input() 函数从标准输入读取一个行,并 ...
最新文章
- python中math.ceil是什么意思_python中的数字取整(ceil,floor,round)概念和用法
- 深入理解session过期机制
- $HOME/$user/.权限导致用户无法登陆图形界面
- 【VBS】网页脚本的放置位置与载入时机
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
- 技术迭代快速。PyTorch 真的优于Tensorflow吗?
- 程序员的绩效到底是应该衡量项目,还是改 Bug 量?
- 标准模板库(STL)之无序容器列传
- 解决苹果mac新建txt文档在Windows下不换行的方法
- shell 截取文件名及扩展名
- 文件上传之multer
- 推荐三款U盘烧写工具
- CSS Sprite雪碧图应用
- 地图,GPS位置地图坐标系:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图),OpenGIS
- 向量交点坐标公式_直线的交点坐标与距离公式
- 在Redhat9上安装Oracle 9.2
- 小车自动往返工作原理_自动往返小汽车
- VMware教程(一):设置 CentOS 7 共享文件夹
- PS|002自制夸张表情包
- 战争与征服服务器维护,《战争与征服》12月5日停机维护公告
热门文章
- 随机论---生命起源随想
- 如何使用AKShare
- 【EdgeX】基于sdk-c随机数设备服务发布数据到MQTT消息总线上,并在MQTTX上订阅
- 07.第八章、质量管理
- muduo C++网络库的学习笔记
- 考虑风光火储的微电网优化调度 考虑风电、光伏、热电机组和储能优化调度,其中负荷考虑冬季或夏季两种场景,并且考虑晴天、多云、雨天、多风和少风场景
- 【香蕉OI】GCD 和 LCM (莫比乌斯反演)
- 51nod 1607 卷积和(枚举搜索)
- Spring Boot中使用Convert接口实现类型转换器
- es父子结构查询_探索ES-嵌套对象和父子对象(四)