python实现简单反弹球游戏

作者:匿名用户__ 时间:2021-05-06 23:25:45 

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实现简单反弹球游戏

来源:https://blog.csdn.net/wangxun20081008/article/details/115600587

标签:python,反弹球
0
投稿

猜你喜欢

  • Python socket模块ftp传输文件过程解析

    2021-04-17 02:22:59
  • Flask使用SocketIO实现WebSocket与Vue进行实时推送

    2023-02-19 05:05:45
  • python识别验证码的思路及解决方案

    2022-02-02 16:05:13
  • Python使用matplotlib绘制正弦和余弦曲线的方法示例

    2023-10-03 13:44:57
  • Python函数和模块的使用详情

    2023-10-11 13:51:20
  • Python map及filter函数使用方法解析

    2021-11-20 05:25:03
  • Python中functools模块的常用函数解析

    2022-08-12 08:10:50
  • Python爬虫中urllib3与urllib的区别是什么

    2023-04-04 05:48:12
  • 详解Oracle 中实现数据透视表的几种方法

    2023-07-24 03:45:04
  • 如何基于pythonnet调用halcon脚本

    2022-09-27 16:34:23
  • SQL Server 2008的一些新特点及独到之处

    2009-01-15 12:59:00
  • laravel多视图共享数据实例代码

    2023-06-19 01:34:14
  • Seaborn数据分析NBA球员信息数据集

    2021-06-27 03:36:04
  • Python通过4种方式实现进程数据通信

    2023-11-04 15:13:48
  • Python制作微信好友背景墙教程(附完整代码)

    2022-11-13 08:31:08
  • 蜕变——记QQ医生3.0

    2009-09-16 14:41:00
  • 使用Python opencv实现视频与图片的相互转换

    2022-03-04 15:20:31
  • 实例解析:MySQL 实例管理器识别的命令

    2009-02-23 17:33:00
  • python中的opencv和PIL(pillow)转化操作

    2023-04-01 18:54:46
  • Python绘图之turtle库的基础语法使用

    2023-04-09 18:39:16
  • asp之家 网络编程 m.aspxhome.com