pygame实现弹球游戏


Posted in Python onApril 14, 2020

本文实例为大家分享了pygame实现弹球游戏的具体代码,供大家参考,具体内容如下

pygame弹球游戏

写的很简陋
pip install pygame 安装pygame模块

代码,复制运行即可

import pygame
import random

pygame.init()

win = pygame.display.set_mode((600, 600)) # 画布窗口的大小
pygame.display.set_caption("弹球游戏") # 窗口标题

x, y = 300, 0 # 方块的起点
width, height = 10, 10 # 方块的宽,高
speed = 1 # 速度


def _randomOK():
  return random.randint(0, 1)


stop = False
_random = _randomOK()

str1 = "暂停中"
baffle = 250
status = 0

count = 0
top = 0
while True:
  # 刷新频率, 小球移动速度
  pygame.time.Clock().tick(1000)

  for event in pygame.event.get():
    # 窗口x事件
    if event.type == pygame.QUIT:
      exit(0)
    elif event.type == pygame.KEYDOWN:
      # 回车事件
      if event.key == 13:
        str1 = "暂停中"
        stop = not stop
        if status == 1:
          x, y = 300, 0

  keys = pygame.key.get_pressed()
  if stop:
    pygame.display.set_caption(str1) # 窗口标题
    continue
  if y >= 590:
    status = 1
    stop = not stop
    str1 = "游戏结束,回车重新开始,反弹次数" + str(count)
    count = 0

  pygame.display.set_caption("弹球游戏") # 窗口标题
  if y == 0:
    top = 0
  if top == 0:
    if _random == 0: # 向下左弹
      x -= speed
      y += speed
    elif _random == 1:
      x += speed
      y += speed
  else:
    if _random == 0: # 向上左弹
      x -= speed
      y -= speed
    elif _random == 1: # 向上右弹
      x += speed
      y -= speed
  # 方向箭头响应
  if keys[pygame.K_LEFT]:
    baffle -= speed
    if baffle < 0:
      baffle = 0

  if keys[pygame.K_RIGHT]:
    baffle += speed
    if baffle > 500:
      baffle = 500
  # 碰撞逻辑
  if 500 <= y <= 520:
    print(x, y)
    print(baffle)
    # y 高度坐标 200 x 宽度坐标 200
    # x坐标加300 大于 宽度初始坐标, 小于 宽度+300
    if baffle <= x <= baffle + 100:
      count += 1
      top = 1

  # 防止跑出边界
  if x > win.get_size()[0] - width:
    _random = _randomOK()
    x = win.get_size()[0] - width

  if x < 0:
    _random = _randomOK()
    x = 0

  if y > win.get_size()[1] - height:
    _random = _randomOK()
    y = win.get_size()[1] - height

  if y < 0:
    _random = _randomOK()
    y = 0

  # 将每一帧的底色先填充成黑色
  win.fill((64, 158, 255))
  # 画方块
  pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
  # 挡板设置,
  pygame.draw.rect(win, (255, 255, 255), (baffle, 500, 100, 20))
  # 更新画布
  pygame.display.update()
pygame.quit()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用Python从零实现贝叶斯分类器的机器学习的教程
Mar 31 Python
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
May 06 Python
python中使用正则表达式的后向搜索肯定模式(推荐)
Nov 11 Python
Python+tkinter模拟“记住我”自动登录实例代码
Jan 16 Python
python爬虫爬取网页表格数据
Mar 07 Python
Python处理中文标点符号大集合
May 14 Python
PyTorch里面的torch.nn.Parameter()详解
Jan 03 Python
Python随机数函数代码实例解析
Feb 09 Python
Python socket处理client连接过程解析
Mar 18 Python
python构造IP报文实例
May 05 Python
k-means 聚类算法与Python实现代码
Jun 01 Python
pytorch __init__、forward与__call__的用法小结
Feb 27 Python
python DES加密与解密及hex输出和bs64格式输出的实现代码
Apr 13 #Python
Python request操作步骤及代码实例
Apr 13 #Python
jupyter notebook插入本地图片的实现
Apr 13 #Python
Python BeautifulReport可视化报告代码实例
Apr 13 #Python
解决jupyter notebook 出现In[*]的问题
Apr 13 #Python
超全Python图像处理讲解(多模块实现)
Apr 13 #Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
Apr 13 #Python
You might like
写php分页时出现的Fatal error的解决方法
2011/04/18 PHP
php获取后台Job管理的实现代码
2011/06/10 PHP
php中实现进程锁与多进程的方法
2016/09/18 PHP
php获取ajax的headers方法与内容实例
2017/12/27 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
2020/09/17 PHP
Laravel统一错误处理为JSON的方法介绍
2020/10/18 PHP
基于jquery实现的可以编辑选择的下拉框的代码
2010/11/19 Javascript
js获取时间(本周、本季度、本月..)
2013/11/22 Javascript
关于onchange事件在IE和FF下的表现及解决方法
2014/03/08 Javascript
JavaScript处理解析JSON数据过程详解
2015/09/11 Javascript
jQuery简单验证上传文件大小及类型的方法
2016/06/02 Javascript
javascript中使用未定义变量或值的情况分析
2016/07/19 Javascript
基于Vue实现页面切换左右滑动效果
2020/06/29 Javascript
详解js常用分割取字符串的方法
2019/05/15 Javascript
极简的Python入门指引
2015/04/01 Python
Python的Django框架中的表单处理示例
2015/07/17 Python
python删除特定文件的方法
2015/07/30 Python
浅谈Python 对象内存占用
2016/07/15 Python
Python进阶_关于命名空间与作用域(详解)
2017/05/29 Python
python实现对文件中图片生成带标签的txt文件方法
2018/04/27 Python
基于python中theano库的线性回归
2018/08/31 Python
Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】
2018/12/05 Python
python创建学生管理系统
2019/11/22 Python
python线程里哪种模块比较适合
2020/08/02 Python
详解HTML5 window.postMessage与跨域
2017/05/11 HTML / CSS
有原因的手表:Flex Watches
2019/03/23 全球购物
荷兰音乐会和音乐剧门票订购网站:Topticketshop
2019/08/27 全球购物
如何实现一个自定义类的序列化
2012/05/22 面试题
英语专业学生个人求职信
2014/01/28 职场文书
军校大学生个人的自我评价
2014/02/17 职场文书
中层干部培训方案
2014/06/16 职场文书
宾馆仓管员岗位职责
2014/07/27 职场文书
租房安全协议书
2014/08/20 职场文书
2014年惩防体系建设工作总结
2014/12/01 职场文书
单位委托函范文
2015/01/29 职场文书
淡雅古典唯美少女娇媚宁静迷人写真
2022/03/21 杂记