鱼C论坛

 找回密码
 立即注册
查看: 1338|回复: 2

[已解决]弹球游戏的问题

[复制链接]
发表于 2017-4-1 18:18:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from tkinter import *
  2. import random
  3. import time

  4. class Ball:
  5.     def __init__(self,canvas,paddle,color):
  6.         self.canvas = canvas
  7.         self.paddle = paddle
  8.         self.id = canvas.create_oval(10,10,25,25,fill=color)
  9.         self.canvas.move(self.id,245,100)
  10.         starts = [-3,-2,-1,1,2,3]
  11.         random.shuffle(starts)
  12.         self.x = starts[0]
  13.         self.y = -3
  14.         self.canvas_height = self.canvas.winfo_height()
  15.         self.canvas_width = self.canvas.winfo_width()
  16.         
  17.    
  18.         
  19.     def hit_paddle(self,pos):
  20.         paddle_pos = self.canvas.coords(self.paddle.id)
  21.         if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  22.             if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  23.                 return True
  24.             return False
  25.         
  26.     def draw(self):
  27.         self.canvas.move(self.id,self.x,self.y)
  28.         pos = self.canvas.coords(self.id)
  29.         if pos[1] <= 0:
  30.             self.y = 3
  31.         if pos[3] >=self.canvas_height:
  32.             self.y = -3
  33.         if self.hit_paddle(pos) == True:
  34.             self.y = -3
  35.         if pos[0] <= 0:
  36.             self.x = 3
  37.         if pos[2] >= self.canvas_width:
  38.             self.x = -3

  39. class Paddle:
  40.     def __init__(self,canvas,color):
  41.         self.canvas = canvas
  42.         self.id = canvas.create_rectangle(0,0,100,10,fill=color)
  43.         self.canvas.move(self.id,200,300)
  44.         self.x = 0
  45.         self.canvas_width = self.canvas.winfo_width()
  46.         self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  47.         self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  48.         
  49.     def draw(self):
  50.         self.canvas.move(self.id,self.x,0)
  51.         pos = self.canvas.coords(self.id)
  52.         if pos[0] <= 0:
  53.             self.x = 0
  54.         elif pos[2] >= self.canvas_width:
  55.             self.x = 0

  56.     def turn_left(self,evt):
  57.         self.x = -2

  58.     def turn_right(self,evt):
  59.         self.x = 2

  60.         
  61. tk = Tk()
  62. tk.title("Game")
  63. tk.resizable(0,0)
  64. tk.wm_attributes("-topmost",1)
  65. canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
  66. canvas.pack()
  67. tk.update()

  68. while 1:
  69.     ball.draw()
  70.     paddle.draw()  
  71.     tk.update_idletasks()
  72.     tk.update()
  73.     time.sleep(0.01)
复制代码

为什么会报错呢...
最佳答案
2017-4-1 18:21:34
恩...你一定没好好听实例化部分
创建啊
  1. from tkinter import *
  2. import random
  3. import time

  4. class Ball:
  5.     def __init__(self,canvas,paddle,color):
  6.         self.canvas = canvas
  7.         self.paddle = paddle
  8.         self.id = canvas.create_oval(10,10,25,25,fill=color)
  9.         self.canvas.move(self.id,245,100)
  10.         starts = [-3,-2,-1,1,2,3]
  11.         random.shuffle(starts)
  12.         self.x = starts[0]
  13.         self.y = -3
  14.         self.canvas_height = self.canvas.winfo_height()
  15.         self.canvas_width = self.canvas.winfo_width()
  16.         
  17.    
  18.         
  19.     def hit_paddle(self,pos):
  20.         paddle_pos = self.canvas.coords(self.paddle.id)
  21.         if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  22.             if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  23.                 return True
  24.             return False
  25.         
  26.     def draw(self):
  27.         self.canvas.move(self.id,self.x,self.y)
  28.         pos = self.canvas.coords(self.id)
  29.         if pos[1] <= 0:
  30.             self.y = 3
  31.         if pos[3] >=self.canvas_height:
  32.             self.y = -3
  33.         if self.hit_paddle(pos) == True:
  34.             self.y = -3
  35.         if pos[0] <= 0:
  36.             self.x = 3
  37.         if pos[2] >= self.canvas_width:
  38.             self.x = -3

  39. class Paddle:
  40.     def __init__(self,canvas,color):
  41.         self.canvas = canvas
  42.         self.id = canvas.create_rectangle(0,0,100,10,fill=color)
  43.         self.canvas.move(self.id,200,300)
  44.         self.x = 0
  45.         self.canvas_width = self.canvas.winfo_width()
  46.         self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  47.         self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  48.         
  49.     def draw(self):
  50.         self.canvas.move(self.id,self.x,0)
  51.         pos = self.canvas.coords(self.id)
  52.         if pos[0] <= 0:
  53.             self.x = 0
  54.         elif pos[2] >= self.canvas_width:
  55.             self.x = 0

  56.     def turn_left(self,evt):
  57.         self.x = -2

  58.     def turn_right(self,evt):
  59.         self.x = 2

  60.         
  61. tk = Tk()
  62. tk.title("Game")
  63. tk.resizable(0,0)
  64. tk.wm_attributes("-topmost",1)
  65. canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
  66. canvas.pack()
  67. tk.update()

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


  70. while 1:
  71.     ball.draw()
  72.     paddle.draw()  
  73.     tk.update_idletasks()
  74.     tk.update()
  75.     time.sleep(0.01)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-1 18:21:34 | 显示全部楼层    本楼为最佳答案   
恩...你一定没好好听实例化部分
创建啊
  1. from tkinter import *
  2. import random
  3. import time

  4. class Ball:
  5.     def __init__(self,canvas,paddle,color):
  6.         self.canvas = canvas
  7.         self.paddle = paddle
  8.         self.id = canvas.create_oval(10,10,25,25,fill=color)
  9.         self.canvas.move(self.id,245,100)
  10.         starts = [-3,-2,-1,1,2,3]
  11.         random.shuffle(starts)
  12.         self.x = starts[0]
  13.         self.y = -3
  14.         self.canvas_height = self.canvas.winfo_height()
  15.         self.canvas_width = self.canvas.winfo_width()
  16.         
  17.    
  18.         
  19.     def hit_paddle(self,pos):
  20.         paddle_pos = self.canvas.coords(self.paddle.id)
  21.         if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
  22.             if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
  23.                 return True
  24.             return False
  25.         
  26.     def draw(self):
  27.         self.canvas.move(self.id,self.x,self.y)
  28.         pos = self.canvas.coords(self.id)
  29.         if pos[1] <= 0:
  30.             self.y = 3
  31.         if pos[3] >=self.canvas_height:
  32.             self.y = -3
  33.         if self.hit_paddle(pos) == True:
  34.             self.y = -3
  35.         if pos[0] <= 0:
  36.             self.x = 3
  37.         if pos[2] >= self.canvas_width:
  38.             self.x = -3

  39. class Paddle:
  40.     def __init__(self,canvas,color):
  41.         self.canvas = canvas
  42.         self.id = canvas.create_rectangle(0,0,100,10,fill=color)
  43.         self.canvas.move(self.id,200,300)
  44.         self.x = 0
  45.         self.canvas_width = self.canvas.winfo_width()
  46.         self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  47.         self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  48.         
  49.     def draw(self):
  50.         self.canvas.move(self.id,self.x,0)
  51.         pos = self.canvas.coords(self.id)
  52.         if pos[0] <= 0:
  53.             self.x = 0
  54.         elif pos[2] >= self.canvas_width:
  55.             self.x = 0

  56.     def turn_left(self,evt):
  57.         self.x = -2

  58.     def turn_right(self,evt):
  59.         self.x = 2

  60.         
  61. tk = Tk()
  62. tk.title("Game")
  63. tk.resizable(0,0)
  64. tk.wm_attributes("-topmost",1)
  65. canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
  66. canvas.pack()
  67. tk.update()

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


  70. while 1:
  71.     ball.draw()
  72.     paddle.draw()  
  73.     tk.update_idletasks()
  74.     tk.update()
  75.     time.sleep(0.01)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-1 18:24:44 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 12:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表