移动球内Tkinter画布小部件(简单的Arkanoid游戏) [英] Move a ball inside Tkinter Canvas Widget (simple Arkanoid game)

查看:151
本文介绍了移动球内Tkinter画布小部件(简单的Arkanoid游戏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python和Tkinter的帮助下编写一个简单的Arkanoid。目标是使球从顶部,右侧和左侧反射。



以下是代码:



如果玩家错过了球,

  from Tkinter import * 
import time

root = Tk()
canv = Canvas(root,highlightthickness = 0)
canv.pack(fill ='both',expand = True)
top = canv.create_line(0,0,640,0,fill ='green',tags =('top'))
left = canv.create_line(0,0,0,480,fill ='green',tags =('left'))
right = canv.create_line(639,0,639,480,fill ='green',tags =('right'))
bottom = canv.create_line(0,478,640,478,fill ='red',tags =('bottom'))

rect = canv.create_rectangle(270,468,365,478,outline ='black',fill ='gray40',tags =('rect'))
ball = canv.create_oval ,20,40,outline ='black',fill ='gray40',tags =('ball'))

delta_x = delta_y = 3
new_x,new_y = delta_x,-delta_y
while True:
time.sleep(0.025)
if canv.find_overlapping(canv.coords(ball)[0],canv.coords(ball)[1],canv.coords 1)
new_x,new_y = delta_x,-delta_y
canv.move(ball,new_x,new_y)
print'fitst if',new_x,new_y
if canv.find_overlapping(canv.coords(ball)[0],canv.coords(ball)[1],canv.coords ,canv.coords(ball)[3])[0] == 2:
new_x,new_y = delta_x,delta_y
canv.move(ball,new_x,new_y)
print'2nd如果',new_x,new_y
if canv.find_overlapping(canv.coords(ball)[0],canv.coords(ball)[1],canv.coords(ball)[2],canv.coords )[3])[0] == 3:
new_x,new_y = -delta_x,delta_y
canv.move(ball,new_x,new_y)
if canv.find_overlapping(canv.coords (0)== 4:
new_x,(b)
new_x,(b)
new_x, new_y = delta_x,-delta_y
canv.move(ball,new_x,new_y)
print new_x,new_y
canv.move(ball,new_y,new_y)
canv.update )

def move_right(event):
canv.move(rect,7,0)
pass

def move_left $ b canv.move(rect,-7,0)
pass

root.bind('< Right>',move_right)
root.bind('< Left>',move_left)

root.geometry('%sx%s +%s +%s'%(640,480,100,100))
root.resizable(0,0 )
root.mainloop()

为什么球以错误的方式反射? p>

解决方案

这里是一个简单的黑客这个问题:

  delta_x = delta_y = 3 
while True:
objects = canv.find_overlapping(canv.coords(ball)[0],canv.coords(ball)[1],canv.coords ],canv.coords(ball)[3])
对象中的obj:
if obj == 1:
delta_y = -delta_y
if obj == 2:
delta_x = -delta_x
if obj == 3:
delta_x = -delta_x
if obj == 4:
delta_y = -delta_y
$ b b new_x,new_y = delta_x,delta_y
canv.move(ball,new_x,new_y)
canv.update()
time.sleep(0.025)

root .bind('< Right>',move_right)
root.bind('< Left>',move_left)

$ b b

I'm trying to write a simple Arkanoid with the help of Python and Tkinter. The goal is to make the ball reflect from the top, right and left sides. And if the player misses the ball so it touches the bottom side, the game would stop.

Here's the code:

from Tkinter import *
import time

root = Tk()
canv = Canvas(root, highlightthickness=0)
canv.pack(fill='both', expand=True)
top = canv.create_line(0, 0, 640, 0, fill='green', tags=('top'))
left = canv.create_line(0, 0, 0, 480, fill='green', tags=('left'))
right = canv.create_line(639, 0, 639, 480, fill='green', tags=('right'))
bottom = canv.create_line(0, 478, 640, 478, fill='red', tags=('bottom'))

rect = canv.create_rectangle(270, 468, 365, 478, outline='black', fill='gray40', tags=('rect'))
ball = canv.create_oval(0, 20, 20, 40, outline='black', fill='gray40', tags=('ball'))

delta_x = delta_y = 3
new_x, new_y = delta_x, -delta_y
while True:
    time.sleep(0.025)
    if canv.find_overlapping(canv.coords(ball)[0], canv.coords(ball)[1], canv.coords(ball)[2], canv.coords(ball)[3])[0] == 1:
        new_x, new_y = delta_x, -delta_y
        canv.move(ball, new_x, new_y)
        print 'fitst if', new_x, new_y
    if canv.find_overlapping(canv.coords(ball)[0], canv.coords(ball)[1], canv.coords(ball)[2], canv.coords(ball)[3])[0] == 2:
        new_x, new_y = delta_x, delta_y
        canv.move(ball, new_x, new_y)
        print '2nd if', new_x, new_y
    if canv.find_overlapping(canv.coords(ball)[0], canv.coords(ball)[1], canv.coords(ball)[2], canv.coords(ball)[3])[0] == 3:
        new_x, new_y = -delta_x, delta_y
        canv.move(ball, new_x, new_y)
    if canv.find_overlapping(canv.coords(ball)[0], canv.coords(ball)[1], canv.coords(ball)[2], canv.coords(ball)[3])[0] == 4:
        new_x, new_y = delta_x, -delta_y
        canv.move(ball, new_x, new_y)
    print new_x, new_y
    canv.move(ball, new_y, new_y)
    canv.update()

def move_right(event):
        canv.move(rect, 7, 0)
        pass

def move_left(event):
    canv.move(rect, -7, 0)
    pass

root.bind('<Right>', move_right)
root.bind('<Left>', move_left)

root.geometry('%sx%s+%s+%s' %(640, 480, 100, 100))
root.resizable(0, 0)
root.mainloop()

Why the ball reflecting in the wrong way?

解决方案

here is a simple hack for this problem:

delta_x = delta_y = 3
while True:
      objects = canv.find_overlapping(canv.coords(ball)[0], canv.coords(ball)[1], canv.coords(ball)[2], canv.coords(ball)[3])
      for obj in objects:
        if obj == 1:
            delta_y = -delta_y
        if obj == 2:
            delta_x = -delta_x
        if obj == 3:
            delta_x = -delta_x
        if obj == 4:
            delta_y = -delta_y

      new_x, new_y = delta_x, delta_y
      canv.move(ball, new_x, new_y)
      canv.update()
      time.sleep(0.025)

      root.bind('<Right>', move_right)
      root.bind('<Left>', move_left)

这篇关于移动球内Tkinter画布小部件(简单的Arkanoid游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆