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中关键字nonlocal和global的声明与解析
Mar 12 Python
Python基于回溯法子集树模板解决m着色问题示例
Sep 07 Python
python入门教程 python入门神图一张
Mar 05 Python
python 巧用正则寻找字符串中的特定字符的位置方法
May 02 Python
Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例
May 16 Python
python实现猜数字小游戏
Mar 24 Python
python 判断矩阵中每行非零个数的方法
Jan 26 Python
python requests库爬取豆瓣电视剧数据并保存到本地详解
Aug 10 Python
Python中注释(多行注释和单行注释)的用法实例
Aug 28 Python
Tensorflow的梯度异步更新示例
Jan 23 Python
使用 pytorch 创建神经网络拟合sin函数的实现
Feb 24 Python
python函数map()和partial()的知识点总结
May 26 Python
python中Tkinter 窗口之输入框和文本框的实现
Apr 12 #Python
python opencv常用图形绘制方法(线段、矩形、圆形、椭圆、文本)
python 利用 PIL 将数组值转成图片的实现
python实现网络五子棋
python实现简易名片管理系统
Apr 11 #Python
python 自动化偷懒的四个实用操作
python Tkinter的简单入门教程
You might like
解析PHP生成静态html文件的三种方法
2013/06/18 PHP
Codeigniter生成Excel文档的简单方法
2014/06/12 PHP
PHP获取短链接跳转后的真实地址和响应头信息的方法
2014/07/25 PHP
PHP函数实现分页含文本分页和数字分页
2014/10/23 PHP
PHP超牛逼无限极分类生成树方法
2015/05/11 PHP
php使用curl代理实现抓取数据的方法
2017/02/03 PHP
PHP获取文件扩展名的方法实例总结
2017/06/10 PHP
如何通过View::first使用Laravel Blade的动态模板详解
2017/09/21 PHP
关于laravel 数据库迁移中integer类型是无法指定长度的问题
2019/10/09 PHP
Ajax,UTF-8还是GB2312 eval 还是execScript
2008/11/13 Javascript
把html页面的部分内容保存成新的html文件的jquery代码
2009/11/12 Javascript
基于JS实现EOS隐藏错误提示层代码
2016/04/25 Javascript
BootStrap学习系列之Bootstrap Typeahead 组件实现百度下拉效果(续)
2016/07/07 Javascript
JavaScript数组push方法使用注意事项
2017/10/30 Javascript
浅谈angular4.0中路由传递参数、获取参数最nice的写法
2018/03/12 Javascript
JavaScript的Proxy可以做哪些有意思的事儿
2019/06/15 Javascript
vue-路由精讲 二级路由和三级路由的作用
2020/08/06 Javascript
vue或react项目生产环境去掉console.log的操作
2020/09/02 Javascript
keep-alive保持组件状态的方法
2020/12/02 Javascript
Vue实现点击当前行变色
2020/12/14 Vue.js
[16:14]教你分分钟做大人:米拉娜(HEROS)
2014/11/24 DOTA
python实现自动登录人人网并采集信息的方法
2015/06/28 Python
python daemon守护进程实现
2016/08/27 Python
Python 统计位数为偶数的数字代码详解
2020/03/15 Python
html5定位并在百度地图上显示的示例
2014/04/27 HTML / CSS
加拿大领先的优质厨具产品在线购物网站:Golda’s Kitchen
2017/11/17 全球购物
JustFab加拿大:女鞋、靴子、手袋和服装在线
2018/05/18 全球购物
是什么让J2EE适合用来开发多层的分布式的应用
2015/01/16 面试题
会务接待方案
2014/02/27 职场文书
村级环境卫生整治方案
2014/05/04 职场文书
小学见习报告
2014/10/31 职场文书
2015年党性分析材料
2014/12/19 职场文书
自主招生学校推荐信范文
2015/03/26 职场文书
房屋维修申请报告
2015/05/18 职场文书
干货干货!2019最新优秀创业计划书
2019/03/21 职场文书
读《皮囊》有感:理解是对他人的最大的善举
2019/11/14 职场文书