请仔细阅读我的代码:
#Tron
from tkinter import *
from PIL import ImageTk
import os
#####Setting The Variables
canvas_height = 500
canvas_width = 600
canvas_colour = "#C9BFBF"
p1_x = (2*canvas_width)/8
p1_y = (2*canvas_height)/8
p2_x = (6*canvas_width)/8
p2_y = (2*canvas_height)/8
p3_x = (2*canvas_width)/8
p3_y = (6*canvas_height)/8
p4_x = (6*canvas_width)/8
p4_y = (6*canvas_height)/8
#p1_colour = input("{Player 1}Input your colour please. Or use a hex value.")
#os.system("cls")
#p2_colour = input("{Player 2}Input your colour please. Or use a hex value.")
#os.system("cls")
#while p1_colour == p2_colour:
# p2_colour = input("{Player 2}Input your colour please. Or use a hex value.")
# os.system("cls")
#p3_colour = input("{Player 3}Input your colour please. Or use a hex value.")
#os.system("cls")
#while p3_colour == p2_colour or p3_colour == p1_colour:
# p3_colour = input("{Player 3}Input your colour please. Or use a hex value.")
# os.system("cls")
#p4_colour = input("{Player 4}Input your colour please. Or use a hex value.")
#os.system("cls")
#while p4_colour == p3_colour or p4_colour == p2_colour or p4_colour == p1_colour:
# p4_colour = input("{Player 4}Input your colour please. Or use a hex value.")
# os.system("cls")
line_width = 7
line_length = 7
#os.system("cls")
##Some defining...
def p1_red():
global p1_color
p1_colour = "red"
p1_choice = 1
def p1_blue():
global p1_color
p1_colour = "blue"
p1_choice = 1
def p1_yellow():
global p1_color
p1_colour = "yellow"
p1_choice = 1
def p1_purple():
global p1_color
p1_colour = "purple"
p1_choice = 1
def p1_black():
global p1_color
p1_colour = "black"
p1_choice = 1
def p2_red():
global p2_color
p2_colour = "red"
p2_choice = 1
def p2_blue():
global p2_color
p2_colour = "blue"
p2_choice = 1
def p2_yellow():
global p2_color
p2_colour = "yellow"
p2_choice = 1
def p2_purple():
global p2_color
p2_colour = "purple"
p2_choice = 1
def p2_black():
global p2_color
p2_colour = "black"
p2_choice = 1
def p3_red():
global p3_color
p3_colour = "red"
p3_choice = 1
def p3_blue():
global p3_color
p3_colour = "blue"
p3_choice = 1
def p3_yellow():
global p3_color
p3_colour = "yellow"
p3_choice = 1
def p3_purple():
global p3_color
p3_colour = "purple"
p3_choice = 1
def p3_black():
global p3_color
p3_colour = "black"
p3_choice = 1
def p4_red():
global p4_color
p4_colour = "red"
p4_choice = 1
def p4_blue():
global p4_color
p4_colour = "blue"
p4_choice = 1
def p4_yellow():
global p4_color
p4_colour = "yellow"
p4_choice = 1
def p4_purple():
global p4_color
p4_colour = "purple"
p4_choice = 1
def p4_black():
global p4_color
p4_colour = "black"
p4_choice = 1
def Game():
####Main Window
window = Toplevel()
window.title("Tron")
canvas = Canvas(window, bg = canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0)
canvas.grid()
menu = Menu(window)
window.config(menu=menu)
###defining the drop down menu....
def quit(self):
global window
window.destroy()
def key_bind(*args):
global Tk
global canvas_colour
global canvas_height
global canvas_width
window2 = Tk()
window2.title("Key Bindings")
canvas2 = Frame(bg = canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0)
canvas2.grid()
text = Text(window2)
text.insert(INSERT, "Key Bindings\nWASD = Player 1 movement\nIJKL = Player 2 movement\nArrow Keys = Player 3 movement\n8462 = Player 4 movement\nEsc key = Quit")
text.grid()
window2.mainloop()
Drive_location = "Tron_img.gif"
image = ImageTk.PhotoImage(file = Drive_location)
canvas.create_image(10, 10, image = image, anchor = NW)
subMenu = Menu(menu)
menu.add_cascade(label = "File", menu = subMenu)
subMenu.add_command(label="Key Bindings", command=key_bind)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=quit)
####Functions
# player controls
##p1
def p1_move_N(self):
global p1_y
canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour)
p1_y = p1_y - line_length
def p1_move_S(self):
global p1_y
canvas.create_line(p1_x, p1_y, p1_x + line_length, p1_y, width=line_width, fill=p1_colour)
p1_y = p1_y + line_length
def p1_move_E(self):
global p1_x
canvas.create_line(p1_x, p1_y, (p1_x + line_length), p1_y, width=line_width, fill=p1_colour)
p1_x = p1_x + line_length
def p1_move_W(self):
global p1_x
canvas.create_line(p1_x, p1_y, p1_x - line_length, p1_y, width=line_width, fill=p1_colour)
p1_x = p1_x - line_length
##p2
def p2_move_N(self):
global p2_y
canvas.create_line(p2_x, p2_y, p2_x, (p2_y-line_length), width=line_width, fill=p2_colour)
p2_y = p2_y - line_length
def p2_move_S(self):
global p2_y
canvas.create_line(p2_x, p2_y, p2_x + line_length, p2_y, width=line_width, fill=p2_colour)
p2_y = p2_y + line_length
def p2_move_E(self):
global p2_x
canvas.create_line(p2_x, p2_y, (p2_x + line_length), p2_y, width=line_width, fill=p2_colour)
p2_x = p2_x + line_length
def p2_move_W(self):
global p2_x
canvas.create_line(p2_x, p2_y, p2_x - line_length, p2_y, width=line_width, fill=p2_colour)
p2_x = p2_x - line_length
##p3
def p3_move_N(self):
global p3_y
canvas.create_line(p3_x, p3_y, p3_x, (p3_y-line_length), width=line_width, fill=p3_colour)
p3_y = p3_y - line_length
def p3_move_S(self):
global p3_y
canvas.create_line(p3_x, p3_y, p3_x + line_length, p3_y, width=line_width, fill=p3_colour)
p3_y = p3_y + line_length
def p3_move_E(self):
global p3_x
canvas.create_line(p3_x, p3_y, (p3_x + line_length), p3_y, width=line_width, fill=p3_colour)
p3_x = p3_x + line_length
def p3_move_W(self):
global p3_x
canvas.create_line(p3_x, p3_y, p3_x - line_length, p3_y, width=line_width, fill=p3_colour)
p3_x = p3_x - line_length
##p4
def p4_move_N(self):
global p4_y
canvas.create_line(p4_x, p4_y, p4_x, (p4_y-line_length), width=line_width, fill=p4_colour)
p4_y = p4_y - line_length
def p4_move_S(self):
global p4_y
canvas.create_line(p4_x, p4_y, p4_x + line_length, p4_y, width=line_width, fill=p4_colour)
p4_y = p4_y + line_length
def p4_move_E(self):
global p4_x
canvas.create_line(p4_x, p4_y, (p4_x + line_length), p4_y, width=line_width, fill=p4_colour)
p4_x = p4_x + line_length
def p4_move_W(self):
global p4_x
canvas.create_line(p4_x, p4_y, p4_x - line_length, p4_y, width=line_width, fill=p4_colour)
p4_x = p4_x - line_length
#######bind movement to key presses
##p1
window.bind("w", p1_move_N)
window.bind("s", p1_move_S)
window.bind("a", p1_move_W)
window.bind("d", p1_move_E)
##p2
window.bind("i", p2_move_N)
window.bind("k", p2_move_S)
window.bind("j", p2_move_W)
window.bind("l", p2_move_E)
##p3
window.bind("<Up>", p3_move_N)
window.bind("<Down>", p3_move_S)
window.bind("<Left>", p3_move_W)
window.bind("<Right>", p3_move_E)
##p4
window.bind("8", p4_move_N)
window.bind("2", p4_move_S)
window.bind("4", p4_move_W)
window.bind("6", p4_move_E)
window.bind("<Key-Escape>", quit)
window.mainloop()
#############
colors = Tk()
colors.title("Tron - Character Colours")
frame = Frame(colors)
frame.grid()
colors.call('wm', 'attributes', '.', '-topmost', '1')
p1_button_red = Button(colors, text="P1 - Red", command=p1_red)
p1_button_red.grid(row=0, column=0)
p1_button_blu = Button(colors, text="P1 - Blue", command=p1_blue)
p1_button_blu.grid(row=0, column=1)
p1_button_yel = Button(colors, text="P1 - Yellow", command=p1_yellow)
p1_button_yel.grid(row=0, column=2)
p1_button_pur = Button(colors, text="P1 - Purple", command=p1_purple)
p1_button_pur.grid(row=0, column=3)
p1_button_bla = Button(colors, text="P1 - Black", command=p1_black)
p1_button_bla.grid(row=0, column=4)
p2_button_red = Button(colors, text="P2 - Red", command=p2_red)
p2_button_red.grid(row=1, column=0)
p2_button_blu = Button(colors, text="P2 - Blue", command=p2_blue)
p2_button_blu.grid(row=1, column=1)
p2_button_yel = Button(colors, text="P2 - Yellow", command=p2_yellow)
p2_button_yel.grid(row=1, column=2)
p2_button_pur = Button(colors, text="P2 - Purple", command=p2_purple)
p2_button_pur.grid(row=1, column=3)
p2_button_bla = Button(colors, text="P2 - Black", command=p2_black)
p2_button_bla.grid(row=1, column=4)
p3_button_red = Button(colors, text="P3 - Red", command=p3_red)
p3_button_red.grid(row=2, column=0)
p3_button_blu = Button(colors, text="P3 - Blue", command=p3_blue)
p3_button_red.grid(row=2, column=1)
p3_button_yel = Button(colors, text="P3 - Yellow", command=p3_yellow)
p3_button_red.grid(row=2, column=2)
p3_button_pur = Button(colors, text="P3 - Purple", command=p3_purple)
p3_button_red.grid(row=2, column=3)
p3_button_bla = Button(colors, text="P3 - Black", command=p3_black)
p3_button_red.grid(row=2, column=4)
p4_button_red = Button(colors, text="P4 - Red", command=p4_red)
p4_button_red.grid(row=3, column=0)
p4_button_blu = Button(colors, text="P4 - Blue", command=p4_blue)
p4_button_red.grid(row=3, column=1)
p4_button_yel = Button(colors, text="P4 - Yellow", command=p4_yellow)
p4_button_red.grid(row=3, column=2)
p4_button_pur = Button(colors, text="P4 - Purple", command=p4_purple)
p4_button_red.grid(row=3, column=3)
p4_button_bla = Button(colors, text="P4 - Black", command=p4_black)
p4_button_red.grid(row=3, column=4)
next_but = Button(colors, text="Next", command = Game)
next_but.grid(row=5, column=0)
colors.mainloop()
答案 0 :(得分:0)
global p3_color
p3_colour = "red"
你错过了一个&#34; u&#34;在global
声明中。