python pygame实现挡板弹球游戏


Posted in Python onNovember 25, 2019

学了一天pygame,用python和pygame写一个简单的挡板弹球游戏

GitHub:

EasyBaffleBallGame

# -*- 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 相关文章推荐
python多线程编程方式分析示例详解
Dec 06 Python
python实现删除文件与目录的方法
Nov 10 Python
Python的爬虫程序编写框架Scrapy入门学习教程
Jul 02 Python
python正则表达式爬取猫眼电影top100
Feb 24 Python
tensorflow获取变量维度信息
Mar 10 Python
python实现扫描日志关键字的示例
Apr 28 Python
使用Python向C语言的链接库传递数组、结构体、指针类型的数据
Jan 29 Python
python 列表推导式使用详解
Aug 29 Python
Pytorch实现LSTM和GRU示例
Jan 14 Python
python datetime处理时间小结
Apr 16 Python
python如何求100以内的素数
May 27 Python
五分钟学会怎么用python做一个简单的贪吃蛇
Jan 12 Python
numpy 返回函数的上三角矩阵实例
Nov 25 #Python
如何基于Python获取图片的物理尺寸
Nov 25 #Python
Python:slice与indices的用法
Nov 25 #Python
python科学计算之narray对象用法
Nov 25 #Python
python运用pygame库实现双人弹球小游戏
Nov 25 #Python
python科学计算之scipy——optimize用法
Nov 25 #Python
基于python中__add__函数的用法
Nov 25 #Python
You might like
PHP的几个常用数字判断函数代码
2012/04/24 PHP
浅谈php安全性需要注意的几点事项
2014/07/17 PHP
typecho插件编写教程(六):调用接口
2015/05/28 PHP
PHP异常处理定义与使用方法分析
2017/07/25 PHP
由document.body和document.documentElement想到的
2009/04/13 Javascript
event对象的方法 兼容多浏览器
2009/06/27 Javascript
js 鼠标拖动对象 可让任何div实现拖动效果
2009/11/09 Javascript
ASP.NET jQuery 实例13 原创jQuery文本框字符限制插件-TextArea Counter
2012/02/03 Javascript
js生成的验证码的实现与技术分析
2014/09/17 Javascript
jQuery无刷新切换主题皮肤实例讲解
2015/10/21 Javascript
js格式化输入框内金额、银行卡号
2016/02/01 Javascript
原生JavaScript实现Ajax的方法
2016/04/07 Javascript
BootStrapValidator校验方式
2016/12/19 Javascript
简单实现nodejs上传功能
2017/01/14 NodeJs
Vue 2.5 Level E 发布了: 新功能特性一览
2017/10/24 Javascript
JavaScript常用工具方法封装
2019/02/12 Javascript
在vue中使用Echarts利用watch做动态数据渲染操作
2020/07/20 Javascript
[37:23]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#2Secret VS EG第二局
2016/03/04 DOTA
Python编程中用close()方法关闭文件的教程
2015/05/24 Python
Python使用bs4获取58同城城市分类的方法
2015/07/08 Python
Python为何不能用可变对象作为默认参数的值
2019/07/01 Python
Python中模块(Module)和包(Package)的区别详解
2019/08/07 Python
pandas的to_datetime时间转换使用及学习心得
2019/08/11 Python
python 多进程队列数据处理详解
2019/12/23 Python
浅谈Python中的异常和JSON读写数据的实现
2020/02/27 Python
PyCharm最新激活码(2020/10/27全网最新)
2020/10/27 Python
新东网科技Java笔试题
2012/07/13 面试题
应届毕业生的个人自我鉴定
2013/10/24 职场文书
高中的职业生涯规划书
2013/12/28 职场文书
小学语文教学经验交流材料
2014/06/02 职场文书
幼儿园秋季开学寄语
2014/08/02 职场文书
2014光棍节单身联谊活动策划书
2014/10/10 职场文书
优秀小学生事迹材料
2014/12/26 职场文书
质量负责人岗位职责
2015/02/15 职场文书
Python编程super应用场景及示例解析
2021/10/05 Python
解析探秘fescar分布式事务实现原理
2022/02/28 Java/Android