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中 ? : 三元表达式的使用介绍
Oct 09 Python
Python读取图片EXIF信息类库介绍和使用实例
Jul 10 Python
Python中编写ORM框架的入门指引
Apr 29 Python
详解Python3操作Mongodb简明易懂教程
May 25 Python
Python+matplotlib实现计算两个信号的交叉谱密度实例
Jan 08 Python
win10系统下Anaconda3安装配置方法图文教程
Sep 19 Python
对python 命令的-u参数详解
Dec 03 Python
python引用(import)某个模块提示没找到对应模块的解决方法
Jan 19 Python
如何通过python的fabric包完成代码上传部署
Jul 29 Python
python 爬虫 实现增量去重和定时爬取实例
Feb 28 Python
Django ORM实现按天获取数据去重求和例子
May 18 Python
Django使用rest_framework写出API
May 21 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
整理的一些实用WordPress后台MySQL操作命令
2013/01/07 PHP
推荐几个开源的微信开发项目
2014/12/28 PHP
分享20多个很棒的jQuery 文件上传插件或教程
2011/09/04 Javascript
jquery(hide方法)隐藏指定元素实例
2013/11/11 Javascript
JavaScript获取当前网页最后修改时间的方法
2015/04/03 Javascript
对JavaScript客户端应用编程的一些建议
2015/06/24 Javascript
jQuery定义插件的方法
2015/12/18 Javascript
使用OpenLayers3 添加地图鼠标右键菜单
2015/12/29 Javascript
javascript实现倒计时跳转页面
2016/01/17 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
彻底学会Angular.js中的transclusion
2017/03/12 Javascript
详解React-Router中Url参数改变页面不刷新的解决办法
2018/05/08 Javascript
vue 关闭浏览器窗口的时候,清空localStorage的数据示例
2019/11/06 Javascript
jQuery实时统计输入框字数及限制
2020/06/24 jQuery
JavaScript实现鼠标经过表格某行时此行变色
2020/11/20 Javascript
将Python的Django框架与认证系统整合的方法
2015/07/24 Python
python 数据清洗之数据合并、转换、过滤、排序
2017/02/12 Python
Python 使用类写装饰器的小技巧
2018/09/30 Python
解决Python下imread,imwrite不支持中文的问题
2018/12/05 Python
python进阶之自定义可迭代的类
2019/08/20 Python
Python Django实现layui风格+django分页功能的例子
2019/08/29 Python
python+django+selenium搭建简易自动化测试
2020/08/19 Python
马德里竞技官方网上商店:Atletico Madrid Shop
2019/03/31 全球购物
市场营销专业毕业生自荐信
2013/11/02 职场文书
银行批评与自我批评
2014/02/10 职场文书
会计核算科岗位职责
2014/03/19 职场文书
2014年冬季防火方案
2014/05/21 职场文书
校运会口号
2014/06/18 职场文书
物业总经理助理岗位职责
2014/06/29 职场文书
带刀到教室的检讨书
2014/10/04 职场文书
2014年办公室个人工作总结
2014/11/12 职场文书
2014年医院科室工作总结
2014/12/20 职场文书
实习介绍信模板
2015/01/30 职场文书
毕业生个人自荐书
2015/03/05 职场文书
SQLServer权限之只开启创建表权限
2022/04/12 SQL Server
索尼ICF-36收音机评测
2022/04/30 无线电