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 相关文章推荐
Pyramid添加Middleware的方法实例
Nov 27 Python
python实现的DES加密算法和3DES加密算法实例
Jun 03 Python
Python单例模式实例详解
Mar 01 Python
python使用arcpy.mapping模块批量出图
Mar 06 Python
python中Pycharm 输出中文或打印中文乱码现象的解决办法
Jun 16 Python
Python3.4实现从HTTP代理网站批量获取代理并筛选的方法示例
Sep 26 Python
pyspark.sql.DataFrame与pandas.DataFrame之间的相互转换实例
Aug 02 Python
python 找出list中最大或者最小几个数的索引方法
Oct 30 Python
elasticsearch python 查询的两种方法
Aug 04 Python
浅谈Python3 numpy.ptp()最大值与最小值的差
Aug 24 Python
python 遍历pd.Series的index和value
Nov 26 Python
理解python中装饰器的作用
Jul 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
提升PHP执行速度全攻略
2006/10/09 PHP
分享PHP入门的学习方法
2007/01/02 PHP
PHPUnit PHP测试框架安装方法
2011/03/23 PHP
PHP 删除文件与文件夹操作 unlink()与rmdir()这两个函数的使用
2011/07/17 PHP
一个简单的php加密解密函数(动态加密)
2013/06/19 PHP
计算php页面运行时间的函数介绍
2013/07/01 PHP
详解PHP对象的串行化与反串行化
2016/01/24 PHP
php curl上传、下载、https登陆实现代码
2017/07/23 PHP
Mootools 1.2教程 滚动条(Slider)
2009/09/15 Javascript
js简单的弹出框有关闭按钮
2014/05/05 Javascript
jQuery页面刷新(局部、全部)问题分析
2016/01/09 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
2016/09/29 Javascript
Vue.js第一天学习笔记(数据的双向绑定、常用指令)
2016/12/01 Javascript
JS打印彩色菱形的实例代码
2018/08/15 Javascript
jQuery实现每日秒杀商品倒计时功能
2019/09/06 jQuery
[23:21]Ti4 冒泡赛第二轮DK vs C9 2
2014/07/14 DOTA
python刷投票的脚本实现代码
2014/11/08 Python
Python解惑之整数比较详解
2017/04/24 Python
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
2018/02/05 Python
Python创建普通菜单示例【基于win32ui模块】
2018/05/09 Python
python读取csv和txt数据转换成向量的实例
2019/02/12 Python
django框架防止XSS注入的方法分析
2019/06/21 Python
python多线程http压力测试脚本
2019/06/25 Python
Django文件存储 自己定制存储系统解析
2019/08/02 Python
Python3实现将一维数组按标准长度分隔为二维数组
2019/11/29 Python
Python3.7.0 Shell添加清屏快捷键的实现示例
2020/03/23 Python
如何在Python对Excel进行读取
2020/06/04 Python
canvas学习笔记之绘制简单路径
2019/01/28 HTML / CSS
几个Shell Script面试题
2012/08/31 面试题
DIY蛋糕店的创业计划书范文
2013/12/26 职场文书
小学教育见习报告
2014/10/31 职场文书
客户经理岗位职责
2015/01/31 职场文书
项目备案申请报告
2015/05/15 职场文书
大学毕业谢师宴致辞
2015/07/27 职场文书
2016教师校本研修心得体会
2016/01/08 职场文书
2016年法制宣传月活动总结
2016/04/01 职场文书