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 enumerate遍历数组示例应用
Sep 06 Python
用Python计算三角函数之acos()方法的使用
May 15 Python
Django框架中数据的连锁查询和限制返回数据的方法
Jul 17 Python
在Python的Flask框架中验证注册用户的Email的方法
Sep 02 Python
python中常用的九种预处理方法分享
Sep 11 Python
python+django+sql学生信息管理后台开发
Jan 11 Python
windows下添加Python环境变量的方法汇总
May 14 Python
解决Tensorflow安装成功,但在导入时报错的问题
Jun 13 Python
python在html中插入简单的代码并加上时间戳的方法
Oct 16 Python
python双向链表原理与实现方法详解
Dec 03 Python
python新式类和经典类的区别实例分析
Mar 23 Python
python openCV实现摄像头获取人脸图片
Aug 20 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
相对路径转化成绝对路径
2007/04/10 PHP
php利用cookie实现自动登录的方法
2014/12/10 PHP
php实现过滤字符串中的中文和数字实例
2015/07/29 PHP
CL vs ForZe BO5 第一场 2.13
2021/03/10 DOTA
JavaScript 字符串与数组转换函数[不用split与join]
2009/12/13 Javascript
jquery插件如何使用 jQuery操作Cookie插件使用介绍
2012/12/15 Javascript
JS实现图片预加载无需等待
2012/12/21 Javascript
JQuery实现表格中相同单元格合并示例代码
2013/06/26 Javascript
js onclick事件传参讲解
2013/11/06 Javascript
jquery 表格排序、实时搜索表格内容(附图)
2014/05/19 Javascript
html文档中的location对象属性理解及常见的用法
2014/08/13 Javascript
JavaScript更改原始对象valueOf的方法
2015/03/19 Javascript
使用postMesssage()实现iframe跨域页面间的信息传递
2016/03/29 Javascript
checkbox 选中一个另一个checkbox也会选中的实现代码
2016/07/09 Javascript
Angular2 环境配置详细介绍
2016/09/21 Javascript
web前端开发upload上传头像js示例代码
2016/10/22 Javascript
vue组件实例解析
2017/01/10 Javascript
vue router 源码概览案例分析
2018/10/09 Javascript
jQuery实现的模仿雨滴下落动画效果
2018/12/11 jQuery
js实现图片推拉门效果代码实例
2019/05/18 Javascript
Vue 子组件与数据传递问题及注意事项
2019/07/11 Javascript
vue使用自定义指令实现拖拽
2021/01/29 Javascript
微信小程序与公众号实现数据互通的方法
2019/07/25 Javascript
js实现AI五子棋人机大战
2020/05/28 Javascript
[57:22]完美世界DOTA2联赛PWL S2 FTD vs PXG 第二场 11.27
2020/12/01 DOTA
python3抓取中文网页的方法
2015/07/28 Python
浅析Python中else语句块的使用技巧
2016/06/16 Python
Django restframework 源码分析之认证详解
2019/02/22 Python
python 如何对logging日志封装
2020/12/02 Python
打造经典复古风格的品牌:Alice + Olivia(爱丽丝+奥利维亚)
2016/09/07 全球购物
美国最佳选择产品网站:Best Choice Products
2019/05/27 全球购物
机关搬迁方案
2014/05/18 职场文书
生物技术专业求职信
2014/06/10 职场文书
2015年护士工作总结范文
2015/03/31 职场文书
观后感开头
2015/06/19 职场文书
2019最新劳动仲裁申请书!
2019/07/08 职场文书