python实现简单反弹球游戏


Posted in Python onApril 12, 2021

python简单游戏-反弹球,供大家参考,具体内容如下

tkinter实现,直接贴上代码

from tkinter import*
import time
import random

class Ball:
    def __init__(self,canvas,paddle,color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10,10,25,25,fill=color)
        self.canvas.move(self.id,245,100)
        starts = [-3,-2,-1,1,2,3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False

    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]:
                return True
        return False

    def draw(self):
        self.canvas.move(self.id,self.x,self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 4
        if pos[3] >= self.canvas_height:
            self.hit_bottom=True
        if self.hit_paddle(pos)==True:
            self.y=-4
        if pos[0] <= 0:
            self.x = 4
        if pos[2] >= self.canvas_width:
            self.x = -4

class Paddle:
    def __init__(self,canvas,color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,0,100,10,fill=color)
        self.canvas.move(self.id,200,400)
        self.x=0
        self.canvas_width = self.canvas.winfo_width()
        canvas.bind_all('<KeyPress-Left>',self.turn_left)
        canvas.bind_all('<KeyPress-Right>',self.turn_right)
        self.hit_bottom = False

    def draw(self):
        self.canvas.move(self.id,self.x,0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0

    def turn_left(self,evt):
        self.x=-7

    def turn_right(self,evt):
        self.x=7 
        
tk = Tk()
tk.title("反弹吧!球球")
#tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas = Canvas(tk,width=650,height=600,bd=0,highlightthickness=0)
canvas.pack()
tk.update()

paddle=Paddle(canvas,'blue')
ball = Ball(canvas,paddle,'red')

while 1:
    if ball.hit_bottom==False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

效果:

python实现简单反弹球游戏

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

Python 相关文章推荐
学习python (1)
Oct 31 Python
Python时区设置方法与pytz查询时区教程
Nov 27 Python
python正则表达式re模块详细介绍
May 29 Python
在Heroku云平台上部署Python的Django框架的教程
Apr 20 Python
Python的装饰器使用详解
Jun 26 Python
pandas object格式转float64格式的方法
Apr 10 Python
python3转换code128条形码的方法
Apr 17 Python
ZABBIX3.2使用python脚本实现监控报表的方法
Jul 02 Python
python实现键盘输入的实操方法
Jul 16 Python
Python如何访问字符串中的值
Feb 09 Python
Pytorch数据拼接与拆分操作实现图解
Apr 30 Python
运行Python编写的程序方法实例
Oct 21 Python
python中Tkinter 窗口之输入框和文本框的实现
Apr 12 #Python
python opencv常用图形绘制方法(线段、矩形、圆形、椭圆、文本)
python 利用 PIL 将数组值转成图片的实现
python实现网络五子棋
python实现简易名片管理系统
Apr 11 #Python
python 自动化偷懒的四个实用操作
python Tkinter的简单入门教程
You might like
PHP判断变量是否为0的方法
2014/02/08 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
2015/04/21 PHP
java解析json方法总结
2019/05/16 PHP
laravel config文件配置全局变量的例子
2019/10/13 PHP
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
JavaScript 用Node.js写Shell脚本[译]
2012/09/20 Javascript
jQuery实现仿微软首页感应鼠标变化滑动窗口效果
2015/10/08 Javascript
bootstrap和jQuery.Gantt的css冲突 如何解决
2016/05/29 Javascript
深入浅析knockout源码分析之订阅
2016/07/12 Javascript
Vue.js在使用中的一些注意知识点
2017/04/29 Javascript
vue2的todolist入门小项目的详细解析
2017/05/11 Javascript
jquery+css3实现熊猫tv导航代码分享
2018/02/12 jQuery
Webpack中publicPath路径问题详解
2018/05/03 Javascript
详解Vue CLI3 多页应用实践和源码设计
2018/08/30 Javascript
element-ui tooltip修改背景颜色和箭头颜色的实现
2019/12/16 Javascript
jQuery实现日历效果
2020/09/11 jQuery
Python统计python文件中代码,注释及空白对应的行数示例【测试可用】
2018/07/25 Python
Python 通过调用接口获取公交信息的实例
2018/12/17 Python
Django中的用户身份验证示例详解
2019/08/07 Python
Django Admin中增加导出Excel功能过程解析
2019/09/04 Python
pandas的相关系数与协方差实例
2019/12/27 Python
Python的赋值、深拷贝与浅拷贝的区别详解
2020/02/12 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2020/02/26 Python
python爬虫中抓取指数的实例讲解
2020/12/01 Python
Python利用socket模块开发简单的端口扫描工具的实现
2021/01/27 Python
canvas进阶之如何画出平滑的曲线
2018/10/15 HTML / CSS
美的官方商城:Midea
2016/09/14 全球购物
官方授权图形T恤和服装:Fifth Sun
2019/06/12 全球购物
英国领先的在线鱼贩:The Fish Society
2020/08/12 全球购物
求职信范文怎么写
2014/01/29 职场文书
党政领导班子民主生活会整改措施
2014/09/18 职场文书
党员干部作风建设思想汇报范文
2014/10/25 职场文书
婚庆主持词大全
2015/06/30 职场文书
Python利器openpyxl之操作excel表格
2021/04/17 Python
redis不能访问本机真实ip地址的解决方案
2021/07/07 Redis
webpack的移动端适配方案小结
2021/07/25 Javascript