我正在尝试创建一个简单的点击游戏。我想随机制造“炸弹”,玩家需要在它爆炸之前点击它。现在我真的很难做到,所以我的程序可以注册点击坐标,并确定你是否点击了炸弹或者你是否错过了。有没有人有任何前进的指针/提示?
这是我到目前为止所得到的。
我的炸弹类:
from graphics import *
import time
class Bomb(object):
def __init__(self, location, radius, window):
self.circle = Circle(location, radius)
self.circle.draw(window)
self.circle.setFill("black")
self.start_time = time.time()
def update(self):
if time.time() - self.start_time > 3.0:
self.circle.setFill("blue")
def ready_to_explode(self):
if time.time() - self.start_time > 3.0:
return True
def is_clicked(self):
#use x y coordinates of click and determine if the distance between this point and center of circle is < or > than radius?
def explode(self):
self.circle.setFill("pink")
def defuse(self):
self.circle.setFill("green")
我的主要程序:
from graphics import *
import time
import random
from bomb import Bomb
window = GraphWin("Click-click-BOOM! *", 400, 400)
event_text = Text(Point(100, 100), "events")
event_text.draw(window)
time_text = Text(Point(100, 200), "time info")
time_text.draw(window)
def keyboard_callback(event):
event_text.setText(event.char)
if "q" == event.char:
global quit
quit = True
def click_callback(event):
click_output = "button1 click at "
click_output += "<" + str(event.x) + ", " + str(event.y) + ">"
event_text.setText(click_output)
window.bind_all("<Key>", keyboard_callback)
window.bind_all("<Button-1>", click_callback)
start_time = time.time()
last_time = start_time
quit = False
bombs = []
bomb_to_add = Bomb(Point(random.randint(1, 400), random.randint(1, 400)), 25, window)
bombs.append(bomb_to_add)
frames = 0
while not quit:
for bomb in bombs:
bomb.update()
if bomb.is_clicked():
bomb.defuse()
window.close()
exit()