我正在创建2D赛车游戏。我想弄清楚如何根据变量是否在轨道上来改变汽车运动的变量。到目前为止,我的足迹是由绿色画布和灰色弧/矩形组成。我想弄清楚如何根据汽车是在赛道上还是不在赛道上来改变速度(或其他变量)。
from tkinter import *
root = Tk()
canvas = Canvas(root, width = 1360, height = 710, bg = 'green')
canvas.grid(row=0, column=0, rowspan=6, columnspan=8)
Finish_strait = canvas.create_rectangle(1200,650, 300,675, fill='grey', outline='grey')
Elf = canvas.create_arc(200, 525, 400, 675, fill='grey', start=180, outline='grey')
Elf = canvas.create_oval(221, 500, 370, 650, fill='green', outline='green')
Elf_strait = canvas.create_rectangle(225,600, 200,575, fill='grey', outline='grey')
Renault1 = canvas.create_arc(225,650, 100,500, fill='grey', outline='grey')
Renault1 = canvas.create_oval(100, 522, 200, 625, fill='green', outline='green')
Renault2 = canvas.create_arc(50,200, 275,523, fill='grey', start=180, outline='grey')
Renault2 = canvas.create_arc(75,187, 300,504, fill='green', start=180, outline='green')
Renault3 = canvas.create_arc(50,200, 275,523, fill='grey', start=90, outline='grey')
Renault3 = canvas.create_arc(75,223, 300,510, fill='green', start=90, outline='green')
Renault_strait = canvas.create_rectangle(160,225, 400,200, fill='grey', outline='grey')
Repsol1 = canvas.create_arc(300,200,500,300, fill='grey', outline='grey')
Repsol1 = canvas.create_arc(300,226,470,300, fill='green', outline='green')
Repsol2 = canvas.create_arc(300,100,500,400, fill='grey', outline='grey', start=-90)
Repsol2 = canvas.create_arc(300,123,465,376, fill='green', outline='green', start=-90)
Repsol_strait = canvas.create_rectangle(300,375,400,400, fill='grey', outline='grey')
Seat = canvas.create_arc(190,375,425,500, fill='grey', outline='grey', start=90, extent=160)
Seat = canvas.create_arc(220,401,415,475, fill='green', outline='green', start=90, extent=170)
Seat_strait = canvas.create_polygon(282,474, 450,515, 420,535, 268,496, fill='grey', outline='grey')
Seat_strait2 = canvas.create_rectangle(420,535, 550,510, fill='grey', outline='grey')
Würth = canvas.create_arc(450,535, 650,425, fill='grey', outline='grey', start=-90)
Würth = canvas.create_arc(474,509, 626,425, fill='green', outline='green', start=-90)
Würth_strait = canvas.create_rectangle(650,480, 625,250, fill='grey', outline='grey')
Campsa = canvas.create_arc(625,200, 700,300, fill='grey', outline='grey', start=45, extent=165)
Campsa = canvas.create_arc(651,230, 680,300, fill='green', outline='green', extent=180)
Campsa_strait = canvas.create_polygon(690,215, 1000,515, 980,535, 675,235, fill='grey', outline='grey')
La_Caixa = canvas.create_arc(940,541, 1100,450, fill='grey', outline='grey', extent=150, start=239)
La_Caixa = canvas.create_arc(980,520, 1065,420, fill='green', outline='green', extent=110, start=239)
La_Caixa_strait = canvas.create_rectangle(1065,500, 1090,200, fill='grey', outline='grey')
Banc_Sabadell = canvas.create_arc(1065,250, 1115,150, fill='grey', outline='grey', start=90)
Banc_Sabadell_strait = canvas.create_rectangle(1090,150, 1200,175, fill='grey', outline='grey')
Europcar = canvas.create_arc(1175,250, 1225,150, fill='grey', outline='grey')
Europcar_strait = canvas.create_rectangle(1200,400, 1225,200, fill='grey', outline='grey')
Chicane_RACC = canvas.create_arc(1200,350, 1250,450, fill='grey', outline='grey', start=180)
Chicane_RACC = canvas.create_arc(1200,410, 1251,490, fill='grey', outline='grey',start=0)
Chicane_RACC_strait = canvas.create_rectangle(1225,450, 1251,600, fill='grey', outline='grey')
New_Holland = canvas.create_arc(1150,526, 1251,675, fill='grey', outline='grey', start=270)
New_Holland = canvas.create_arc(1175,526, 1224,650, fill='green', outline='green', start=270)
Finish_Line = canvas.create_rectangle(950,650, 955,675, fill='white', outline='white')
player = canvas.create_rectangle(1000,655,1010,660, fill='red')
canvas.focus_set
root.mainloop()
答案 0 :(得分:0)
您只需使用canvas.find_overlapping
来检查您的汽车是否在轨道上。下面的示例使用<Button-1>
模拟您的汽车在哪里:
def get_color_of_ground(event):
result = canvas.find_overlapping(event.x, event.y, event.x+10, event.y+5)
if result:
print ("On Track!")
else:
print ("Out of Track!")
root.bind("<Button-1>",lambda e: get_color_of_ground(e))
只需编辑print
语句即可,但是您想调整速度。
顺滑的轨道布局,顺便说一句。