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实现图像几何变换
Jul 06 Python
python通过文件头判断文件类型
Oct 30 Python
通过Python爬虫代理IP快速增加博客阅读量
Dec 14 Python
python基本语法练习实例
Sep 19 Python
python实现一个简单的udp通信的示例代码
Feb 01 Python
Python调用钉钉自定义机器人的实现
Jan 03 Python
PyTorch 普通卷积和空洞卷积实例
Jan 07 Python
使用python批量转换文件编码为UTF-8的实现
Apr 03 Python
使用PyQt5实现图片查看器的示例代码
Apr 21 Python
如何写python的配置文件
Jun 07 Python
Python Matplotlib绘图基础知识代码解析
Aug 31 Python
python 机器学习的标准化、归一化、正则化、离散化和白化
Apr 16 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中使用Oracle数据库(1)
2006/10/09 PHP
php封装一个异常的处理类
2017/06/08 PHP
php简单读取.vcf格式文件的方法示例
2017/09/02 PHP
PHP支付宝当面付2.0代码
2018/12/21 PHP
laravel框架模板之公共模板、继承、包含实现方法分析
2019/08/30 PHP
css图片自适应大小
2007/11/28 Javascript
Javascript的时间戳和php的时间戳转换注意事项
2013/04/12 Javascript
json格式的时间显示为正常年月日的方法
2013/09/08 Javascript
JavaScript采用递归算法计算阶乘实例
2015/08/04 Javascript
js滚动条平滑移动示例代码
2016/03/29 Javascript
JavaScript简单实现弹出拖拽窗口(一)
2016/06/17 Javascript
web前端开发upload上传头像js示例代码
2016/10/22 Javascript
使用jquery实现的循环连续可停顿滚动实例
2016/11/23 Javascript
jQuery实现百度登录框的动态切换效果
2017/04/21 jQuery
JavaScript实现图片无缝滚动效果
2017/07/07 Javascript
webpack4+react多页面架构的实现
2018/10/25 Javascript
vue路由传参的基本实现方式小结【三种方式】
2020/02/05 Javascript
基于Angular 8和Bootstrap 4实现动态主题切换的示例代码
2020/02/11 Javascript
基于Electron实现桌面应用开发代码实例
2020/07/07 Javascript
Python lambda和Python def区别分析
2014/11/30 Python
matlab中实现矩阵删除一行或一列的方法
2018/04/04 Python
使用python读取txt文件的内容,并删除重复的行数方法
2018/04/18 Python
Sanic框架Cookies操作示例
2018/07/17 Python
Python的PIL库中getpixel方法的使用
2020/04/09 Python
python实现126邮箱发送邮件
2020/05/20 Python
详解python模块pychartdir安装及导入问题
2020/10/22 Python
python 发送get请求接口详解
2020/11/17 Python
Web前端绘制0.5像素的几种方法
2017/08/11 HTML / CSS
移动Web—CSS为Retina屏幕替换更高质量的图片
2012/12/24 HTML / CSS
Linux内核产生并发的原因
2012/07/13 面试题
高中毕业生生活的自我评价
2013/12/08 职场文书
职务聘任书范文
2014/03/29 职场文书
老乡会致辞
2015/07/28 职场文书
《法国号》教学反思
2016/02/22 职场文书
python基础之停用词过滤详解
2021/04/21 Python
JavaScript中document.activeELement焦点元素介绍
2021/11/27 Javascript