Python实现的弹球小游戏示例

作者:peerlessbloom 时间:2022-06-04 01:15:35 

本文实例讲述了Python实现的弹球小游戏。分享给大家供大家参考,具体如下:

弹球

1. Ball 类

draw负责移动Ball
碰撞检测,反弹,Ball检测Paddle

2.Paddle类

draw负责移动Paddle
碰撞检测,确定能不能继续
监听键盘事件

3.主循环

绘制Ball和Paddle
update
sleep

代码


from Tkinter import *
import random
import time
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)
   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
 def draw(self):
   self.canvas.move(self.id, self.x, self.y)
   pos = self.canvas.coords(self.id)#top-left bottom-right
   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]):
       return True
   return False
class Paddle:
 def __init__(self, canvas, color):
   self.canvas = canvas
   self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
   self.x = 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)
   #self.x = 0
 def turn_left(self, event):
   self.x = -4
 def turn_right(self, event):
   self.x = 4
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)#not resizable
tk.wm_attributes("-topmost", 1)#at top
canvas = Canvas(tk, width = 500, height = 500, bd = 0, highlightthickness = 0)
canvas.pack()
tk.update()#init
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,弹球,小游戏
0
投稿

猜你喜欢

  • python查看微信好友是否删除自己

    2021-01-31 23:16:32
  • Golang简介与基本语法的学习

    2024-02-07 17:03:06
  • 发布你的Python模块详解

    2021-06-10 02:03:53
  • Python函数默认参数设置的具体方法

    2021-03-13 08:19:07
  • 利用JavaScript实现防抖节流函数的示例代码

    2024-05-11 09:31:55
  • 用Python画一个LinkinPark的logo代码实例

    2022-09-27 05:52:19
  • pytorch tensorboard可视化的使用详解

    2022-09-27 01:01:51
  • python实现ID3决策树算法

    2021-03-15 14:30:13
  • 教你怎么用Python处理excel实现自动化办公

    2023-11-05 02:19:50
  • asp智能脏话过滤系统v1.0

    2011-04-14 11:00:00
  • 解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated

    2024-01-16 14:08:53
  • 利用python对mysql表做全局模糊搜索并分页实例

    2024-01-20 05:29:47
  • python变量赋值机制踩坑记录

    2021-08-19 08:59:44
  • python中 logging的使用详解

    2023-01-11 21:31:14
  • ubuntu 16.04下mysql5.7.17开放远程3306端口

    2024-01-17 13:00:03
  • python跨文件使用全局变量的实现

    2023-11-27 00:16:40
  • python使用fork实现守护进程的方法

    2021-08-27 00:37:50
  • JavaScript对象的property属性详解

    2024-05-05 09:22:57
  • Go中过滤范型集合性能示例详解

    2024-04-27 15:27:35
  • TensorFlow2.1.0最新版本安装详细教程

    2021-11-08 00:14:02
  • asp之家 网络编程 m.aspxhome.com