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读写文件操作示例程序
Dec 02 Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 Python
python中尾递归用法实例详解
Apr 28 Python
Python中random模块用法实例分析
May 19 Python
Python用模块pytz来转换时区
Aug 19 Python
对Python中gensim库word2vec的使用详解
May 08 Python
pandas 对series和dataframe进行排序的实例
Jun 09 Python
Pytorch 实现自定义参数层的例子
Aug 17 Python
解决django后台管理界面添加中文内容乱码问题
Nov 15 Python
Python中url标签使用知识点总结
Jan 16 Python
Django Model中字段(field)的各种选项说明
May 19 Python
Python下划线5种含义代码实例解析
Jul 10 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
关于页面优化和伪静态
2009/10/11 PHP
用Php编写注册后Email激活验证的实例代码
2013/03/11 PHP
php中explode函数用法分析
2014/11/15 PHP
IE中createElement需要注意的一个问题
2010/07/13 Javascript
jQuery Ajax提交表单查询获得数据实例代码
2012/09/19 Javascript
Javascript小技巧之生成html元素
2014/05/15 Javascript
JavaScript常用的弹出广告及背投广告实现方法
2015/02/06 Javascript
javascript中sort() 方法使用详解
2015/08/30 Javascript
jQuery Validate表单验证插件 添加class属性形式的校验
2016/01/18 Javascript
javascript鼠标右键菜单自定义效果
2020/12/08 Javascript
利用纯js + transition动画实现移动端web轮播图详解
2017/09/10 Javascript
Vue.js 2.5新特性介绍(推荐)
2017/10/24 Javascript
JavaScript封装的常用工具类库bee.js用法详解【经典类库】
2018/09/03 Javascript
vue 实现移动端键盘搜索事件监听
2019/11/06 Javascript
jQuery实现手风琴效果(蒙版)
2020/01/11 jQuery
Javascript类型判断相关例题及解析
2020/08/26 Javascript
[01:21:36]CHAOS vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
pycharm安装图文教程
2017/05/02 Python
教你使用python画一朵花送女朋友
2018/03/29 Python
Django跨域请求CSRF的方法示例
2018/11/11 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
2019/06/13 Python
python实现最大优先队列
2019/08/29 Python
Python cookie的保存与读取、SSL讲解
2020/02/17 Python
Python多进程编程multiprocessing代码实例
2020/03/12 Python
python3+openCV 获取图片中文本区域的最小外接矩形实例
2020/06/02 Python
python如何对链表操作
2020/10/10 Python
python 进制转换 int、bin、oct、hex的原理
2021/01/13 Python
CSS3 Notes: -webkit-box-reflect实现倒影的实例
2016/12/08 HTML / CSS
Canvas系列之滤镜效果
2019/02/12 HTML / CSS
香港唯港荟酒店预订:Hotel ICON
2018/03/27 全球购物
都柏林通行卡/城市通票:The Dublin Pass
2020/02/16 全球购物
开办化妆品公司创业计划书
2013/12/26 职场文书
婚庆司仪开场白
2015/05/29 职场文书
解决pytorch-gpu 安装失败的记录
2021/05/24 Python
Lombok的详细使用及优缺点总结
2021/07/15 Java/Android
python编程项目中线上问题排查与解决
2021/11/01 Python