python3实现弹弹球小游戏


Posted in Python onNovember 25, 2019

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

from tkinter import *
from tkinter import messagebox
import random
import time
from PIL import Image, ImageTk
import sys
 
 
class Game:
  def __init__(self):
    self.tk = Tk()
    self.times = 0
    sw = self.tk.winfo_screenwidth()
    sh = self.tk.winfo_screenheight()
    image = Image.open(r'02.jpg')
    background_image = ImageTk.PhotoImage(image)
    ww = background_image.width()
    wh = background_image.height()
    x = (sw-ww)/2
    y = (sh-wh)/2
    self.tk.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
    self.tk.title('欢迎进入弹弹弹')
    background_label = Label(self.tk, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    self.tk.resizable(False, False)
    self.tk.wm_attributes("-topmost", 1) # at top
    btn1 = Button(self.tk, text='减少难度', background='#FFFF67',
           activebackground='#3EC23B', command=self.test1)
    btn2 = Button(self.tk, text='增加难度', background='#FFFF67',
           activebackground='#3EC23B', command=self.test2)
 
    btn1.place(x=380, y=10, width=80, height=30)
    btn2.place(x=500, y=10, width=80, height=30)
    self.canvas = Canvas(self.tk, width=500, height=400,
               bd=0, highlightthickness=0, background='#FFFFFF')
    self.canvas.place(x=190, y=90, width=500, height=400)
 
    self.tk.update()
    self.xunhuan()
 
  def xunhuan(self):
    try:
      paddle = Paddle(self.canvas, 'green', 100)
      ball = Ball(self.canvas, paddle, '#D11C43', 0)
      while True:
        if ball.hit_bottom == False:
          ball.draw()
          paddle.draw()
        else:
          b = messagebox.askyesno(
            '失败', message="您的分数为:" + str(ball.score)+"\n是否重新开始游戏?")
          if bool(b) is True:
            paddle.canvas.delete(paddle.id)
            self.sever()
          else:
            sys.exit()
        self.tk.update_idletasks()
        self.tk.update()
        time.sleep(0.01+self.times)
    except:
      sys.exit('游戏已退出!')
 
  def sever(self):
    self.xunhuan()
 
  def test1(self):
    self.times += 0.001
 
  def test2(self):
    self.times -= 0.001
 
 
class Ball:
  def __init__(self, canvas, paddle, color, score):
    self.score = 0
    self.canvas = canvas
    self.paddle = paddle
    self.id = canvas.create_oval(
      20, 20, 35, 35, fill=color, outline='green')
    self.canvas.move(self.id, 245, 100)
    startx = [-3, -2, -1, 1, 2, 3]
    random.shuffle(startx)
    self.x = startx[0]
    self.y = -3
    self.canvas_height = self.canvas.winfo_height()
    self.canvas_width = self.canvas.winfo_width()
    self.hit_bottom = False
    b = messagebox.askyesno('game', '游戏是否开始?')
    if bool(b) is True:
      self.draw()
    else:
      sys.exit()
 
  def draw(self):
    self.canvas.move(self.id, self.x, self.y)
    pos = self.canvas.coords(self.id)
    if pos[1] <= 0 or self.hit_paddle(pos) == True:
      self.y = -self.y
    if pos[0] <= 0 or pos[2] >= self.canvas_width:
      self.x = -self.x
    if pos[3] >= self.canvas_height:
      self.hit_bottom = True
 
  def hit_paddle(self, pos):
    paddle_pos = self.canvas.coords(self.paddle.id)
    if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
      if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
        self.score += 1
        return True
    return False
 
 
class Paddle:
  def __init__(self, canvas, color, width):
    self.canvas = canvas
    self.id = canvas.create_rectangle(0, 0, width, 10, fill=color)
    self.x = 0
    self.y = 0
    self.canvas.move(self.id, 200, 300)
    self.canvas_width = self.canvas.winfo_width()
    self.canvas.bind_all("<Key-Left>", self.turn_left)
    self.canvas.bind_all("<Key-Right>", self.turn_right)
 
  def draw(self):
    pos = self.canvas.coords(self.id)
    if pos[0] + self.x >= 0 and pos[2] + self.x <= self.canvas_width:
      self.canvas.move(self.id, self.x, 0)
 
  def turn_left(self, event):
    self.x = -4
 
  def turn_right(self, event):
    self.x = 4
 
 
def main():
  game = Game()
 
 
if __name__ == '__main__':
  main()

python3实现弹弹球小游戏

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

Python 相关文章推荐
Python批量重命名同一文件夹下文件的方法
May 25 Python
浅谈Python中列表生成式和生成器的区别
Aug 03 Python
Python学习小技巧之利用字典的默认行为
May 20 Python
使用实现pandas读取csv文件指定的前几行
Apr 20 Python
如何使用Python进行OCR识别图片中的文字
Apr 01 Python
Python异步操作MySQL示例【使用aiomysql】
May 16 Python
python f-string式格式化听语音流程讲解
Jun 18 Python
使用python实现ftp的文件读写方法
Jul 02 Python
python-视频分帧&amp;多帧合成视频实例
Dec 10 Python
浅谈Python 参数与变量
Jun 20 Python
Python如何实现线程间通信
Jul 30 Python
python 模拟登陆github的示例
Dec 04 Python
python数据化运营的重要意义
Nov 25 #Python
python实现拉普拉斯特征图降维示例
Nov 25 #Python
python模块hashlib(加密服务)知识点讲解
Nov 25 #Python
Python3如何对urllib和urllib2进行重构
Nov 25 #Python
python pygame实现球球大作战
Nov 25 #Python
Python内置加密模块用法解析
Nov 25 #Python
python使用pygame实现笑脸乒乓球弹珠球游戏
Nov 25 #Python
You might like
php实现斐波那契数列的简单写法
2014/07/19 PHP
yii2实现 &quot;上一篇,下一篇&quot; 功能的代码实例
2017/02/04 PHP
PHP redis实现超迷你全文检索
2017/03/04 PHP
Laravel框架源码解析之反射的使用详解
2020/05/14 PHP
js propertychange和oninput事件
2014/09/28 Javascript
Javascript显示和隐藏ul列表的方法
2015/07/15 Javascript
总结在前端排序中遇到的问题
2016/07/19 Javascript
javascript容错处理代码(屏蔽js错误)
2017/01/20 Javascript
Node.js中的child_process模块详解
2018/06/08 Javascript
Python 变量类型及命名规则介绍
2013/06/08 Python
python使用wmi模块获取windows下的系统信息 监控系统
2015/10/27 Python
python3编码问题汇总
2016/09/06 Python
tensorflow 使用flags定义命令行参数的方法
2018/04/23 Python
python递归函数绘制分形树的方法
2018/06/22 Python
在Pycharm中对代码进行注释和缩进的方法详解
2019/01/20 Python
python 读取dicom文件,生成info.txt和raw文件的方法
2019/01/24 Python
记一次pyinstaller打包pygame项目为exe的过程(带图片)
2020/03/02 Python
Pycharm插件(Grep Console)自定义规则输出颜色日志的方法
2020/05/27 Python
澳大利亚领先的优质葡萄酒拍卖会:Langton’s Fine Wines
2019/03/24 全球购物
俄罗斯一家时尚女装商店:Charuel
2019/12/04 全球购物
C语言基础笔试题
2013/04/27 面试题
函数只定义了一次, 调用了一次, 但编译器提示非法重定义了-什么问题?
2014/10/03 面试题
java程序员面试交流
2012/11/29 面试题
本科生职业生涯规划书范文
2014/01/21 职场文书
临床护士自荐信
2014/01/31 职场文书
优秀老员工获奖感言
2014/02/15 职场文书
《理想的风筝》教学反思
2014/04/11 职场文书
教师节演讲稿
2014/05/06 职场文书
保护环境倡议书100字
2014/05/19 职场文书
党的群众路线教育实践活动个人整改方案
2014/09/21 职场文书
稽核岗位职责范本
2015/04/13 职场文书
简单的辞职信范文(2016最新版)
2015/05/12 职场文书
千手观音观后感
2015/06/03 职场文书
单身证明格式样本
2015/06/15 职场文书
身份证丢失证明
2015/06/19 职场文书
Angular性能优化之第三方组件和懒加载技术
2021/05/10 Javascript