Python 未定义main()变量

Python 未定义main()变量,python,tkinter,error-handling,Python,Tkinter,Error Handling,我编写了用Python创建游戏“Snake”的代码,并给出了一个错误,说明main()变量未定义,请帮助,这是非常必要的: Traceback (most recent call last): File "main.py", line 82, in <module> main() NameError: name 'main' is not defined 回溯(最近一次呼叫最后一次): 文件“main.py”,第82行,在 main() NameError:未定义名称“

我编写了用Python创建游戏“Snake”的代码,并给出了一个错误,说明main()变量未定义,请帮助,这是非常必要的:

Traceback (most recent call last):
  File "main.py", line 82, in <module>
    main()
NameError: name 'main' is not defined
回溯(最近一次呼叫最后一次):
文件“main.py”,第82行,在
main()
NameError:未定义名称“main”
代码:

from tkinter import *
import random

WIDTH = 800
HEIGHT = 600
SEG_SIZE = 20
IN_GAME = True

def create_block():

    global BLOCK
    posx = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE)/SEG_SIZE)
    posy = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE)/SEG_SIZE)
    BLOCK = c.create_oval(posx, posy,
                          posx+SEG_SIZE, posy+SEG_SIZE,
                          fill="red")
    def main():
        global IN_GAME
        if IN_GAME:
            s.move()
            head_coords = c.coords(s.segments[-1].instance)
            x1, y1, x2, y2 = head_coords
            if x2 > WIDTH or x1 < 0 or y1 < 0 or y2 > HEIGHT:
                IN_GAME = False
            elif head_coords == c.coords(BLOCK):
                s.add_segment()
                c.delete(BLOCK)
                create_block()
            else:
                for index in range(len(s.segments)-1):
                    if head_coords == c.coords(s.segments[index].instance):
                        IN_GAME = False
            root.after(100, main)
        else:
                c.create_text(WIDTH/2, HEIGHT/2,
                       text="GAME OVER! SAS",
                       fant="Arial 20",
                       fill="red")
class Segment(object):
  def __init__(self, x, y):
    self.instance = c.create_rectangle(x, y,
                                       x+SEG_SIZE, y+SEG_SIZE,
                                       fill="white")
class Snake(object):
     def __init__(self, segments):
        self.segments = segments
        self.mapping = {"Down": (0, 1), "Right": (1, 0),
                        "Up": (0, -1), "Left": (-1, 0)}
        self.vector = self.mapping["Right"]
     def move(self):
        for index in range(len(self.segments)-1):
           segment = self.segments[index].instance
           c.coords(segment, x1, y1, x2, y2)

        x1, y1, x2, y2 = c.coords(self.segments[-2].instance)
        c.coords(self.segments[-1].instance,
                 x1+self.vector[0]*SEG_SIZE, y1+self.vector[1]*SEG_SIZE,
                 x2+self.vector[0]*SEG_SIZE, y2+self.vector[1]*SEG_SIZE)
     def add_segment(self):
         last_seg = c.coords(self.segments[0].instance)
         x = last_seg[2] - SEG_SIZE
         y = last_seg[3] - SEG_SIZE
         self.segments.insert(0, Segment(x, y))

     def change_direction(self, event):
         if event.keysym in self.mapping:
             self.vector = self.mapping[event.keysym]
root = Tk()
root.title("Snake")


c = Canvas(root, width=WIDTH, height=HEIGHT, bg="#003300")
c.grid()
c.focus_set()
segments = [Segment(SEG_SIZE, SEG_SIZE),
            Segment(SEG_SIZE*2, SEG_SIZE),
            Segment(SEG_SIZE*3, SEG_SIZE)]
s = Snake(segments)
c.bind("<KeyPress>", s.change_direction)

create_block()
main()
root.mainloop()
从tkinter导入*
随机输入
宽度=800
高度=600
SEG_尺寸=20
在游戏中=正确
def create_block():
全局块
posx=分段大小*随机。randint(1,(宽度-分段大小)/分段大小)
posy=SEG_SIZE*random.randint(1,(宽度-SEG_SIZE)/SEG_SIZE)
块=c.创建椭圆(posx、posy、,
posx+SEG_尺寸,posy+SEG_尺寸,
fill=“red”)
def main():
全球游戏
如果在游戏中:
s、 移动()
head_coords=c.coords(s.segments[-1]。实例)
x1,y1,x2,y2=头部坐标
如果x2>宽度或x1<0或y1<0或y2>高度:
在游戏中=错误
elif head_coords==c.coords(块):
s、 添加_段()
c、 删除(块)
创建_块()
其他:
对于范围内的索引(长度(s段)-1):
如果head_coords==c.coords(s.segments[index].instance):
在游戏中=错误
根。后(100,主)
其他:
c、 创建文本(宽度/2,高度/2,
text=“游戏结束!SAS”,
fant=“Arial 20”,
fill=“red”)
类段(对象):
定义初始化(self,x,y):
self.instance=c.create_矩形(x,y,
x+SEG_尺寸,y+SEG_尺寸,
fill=“白色”)
类(对象):
定义初始化(自,段):
self.segments=段
self.mapping={“Down”:(0,1),“Right”:(1,0),
“向上”:(0,-1),“左”:(-1,0)}
self.vector=self.mapping[“Right”]
def移动(自我):
对于范围内的索引(len(self.segments)-1):
segment=self.segments[index]。实例
c、 坐标(段,x1,y1,x2,y2)
x1,y1,x2,y2=c.coords(self.segments[-2]。实例)
c、 coords(self.segments[-1]。实例,
x1+自向量[0]*分段大小,y1+自向量[1]*分段大小,
x2+自向量[0]*分段大小,y2+自向量[1]*分段大小)
def添加_段(自身):
last_seg=c.coords(self.segments[0]。实例)
x=最后一段[2]-段大小
y=最后一段[3]-段大小
self.segments.insert(0,segments(x,y))
def更改方向(自身、事件):
如果self.mapping中的event.keysym:
self.vector=self.mapping[event.keysym]
root=Tk()
根标题(“蛇”)
c=画布(根,宽=宽,高=高,bg=“#003300”)
c、 网格()
c、 焦点集()
分段=[分段(分段大小,分段大小),
分段(分段尺寸*2,分段尺寸),
分段(分段尺寸*3,分段尺寸)]
s=蛇(段)
c、 绑定(“,s.改变方向)
创建_块()
main()
root.mainloop()

这里。我完全修改了你的代码,它运行时没有错误。但是,该游戏不可玩,因为您尚未编写代码

代码:

from tkinter import *
import random

WIDTH = 800
HEIGHT = 600
SEG_SIZE = 20
IN_GAME = True

def create_block():

    global BLOCK
    posx = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE)/SEG_SIZE)
    posy = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE)/SEG_SIZE)
    BLOCK = c.create_oval(posx, posy,
                          posx+SEG_SIZE, posy+SEG_SIZE,
                          fill="red")
    def main():
        global IN_GAME
        if IN_GAME:
            s.move()
            head_coords = c.coords(s.segments[-1].instance)
            x1, y1, x2, y2 = head_coords
            if x2 > WIDTH or x1 < 0 or y1 < 0 or y2 > HEIGHT:
                IN_GAME = False
            elif head_coords == c.coords(BLOCK):
                s.add_segment()
                c.delete(BLOCK)
                create_block()
            else:
                for index in range(len(s.segments)-1):
                    if head_coords == c.coords(s.segments[index].instance):
                        IN_GAME = False
            root.after(100, main)
        else:
                c.create_text(WIDTH/2, HEIGHT/2,
                       text="GAME OVER! SAS",
                       fant="Arial 20",
                       fill="red")
class Segment(object):
  def __init__(self, x, y):
    self.instance = c.create_rectangle(x, y,
                                       x+SEG_SIZE, y+SEG_SIZE,
                                       fill="white")
class Snake(object):
     def __init__(self, segments):
        self.segments = segments
        self.mapping = {"Down": (0, 1), "Right": (1, 0),
                        "Up": (0, -1), "Left": (-1, 0)}
        self.vector = self.mapping["Right"]
     def move(self):
        for index in range(len(self.segments)-1):
           segment = self.segments[index].instance
           c.coords(segment, x1, y1, x2, y2)

        x1, y1, x2, y2 = c.coords(self.segments[-2].instance)
        c.coords(self.segments[-1].instance,
                 x1+self.vector[0]*SEG_SIZE, y1+self.vector[1]*SEG_SIZE,
                 x2+self.vector[0]*SEG_SIZE, y2+self.vector[1]*SEG_SIZE)
     def add_segment(self):
         last_seg = c.coords(self.segments[0].instance)
         x = last_seg[2] - SEG_SIZE
         y = last_seg[3] - SEG_SIZE
         self.segments.insert(0, Segment(x, y))

     def change_direction(self, event):
         if event.keysym in self.mapping:
             self.vector = self.mapping[event.keysym]
root = Tk()
root.title("Snake")


c = Canvas(root, width=WIDTH, height=HEIGHT, bg="#003300")
c.grid()
c.focus_set()
segments = [Segment(SEG_SIZE, SEG_SIZE),
            Segment(SEG_SIZE*2, SEG_SIZE),
            Segment(SEG_SIZE*3, SEG_SIZE)]
s = Snake(segments)
c.bind("<KeyPress>", s.change_direction)

create_block()
main()
root.mainloop()
从tkinter导入*
随机输入
宽度=800
高度=600
SEG_尺寸=20
在游戏中=正确
root=Tk()
根标题(“蛇”)
c=画布(根,宽=宽,高=高,bg=“#003300”)
c、 网格()
c、 焦点集()
posx=分段大小*随机。randint(1,(宽度-分段大小)/分段大小)
posy=SEG_SIZE*random.randint(1,(宽度-SEG_SIZE)/SEG_SIZE)
块=c.创建椭圆(posx、posy、,
posx+SEG_尺寸,posy+SEG_尺寸,
fill=“red”)
def main():
全球游戏
如果在游戏中:
s、 移动()
head_coords=c.coords(s.segments[-1]。实例)
x1,y1,x2,y2=头部坐标
如果x2>宽度或x1<0或y1<0或y2>高度:
在游戏中=错误
elif head_coords==c.coords(块):
s、 添加_段()
c、 删除(块)
创建_块()
其他:
对于范围内的索引(长度(s段)-1):
如果head_coords==c.coords(s.segments[index].instance):
在游戏中=错误
根。后(100,主)
其他:
c、 创建文本(宽度/2,高度/2,
text=“游戏结束!SAS”,
fant=“Arial 20”,
fill=“red”)
类段(对象):
定义初始化(self,x,y):
self.instance=c.create_矩形(x,y,
x+SEG_尺寸,y+SEG_尺寸,
fill=“白色”)
类(对象):
定义初始化(自,段):
self.segments=段
self.mapping={“Down”:(0,1),“Right”:(1,0),
“向上”:(0,-1),“左”:(-1,0)}
self.vector=self.mapping[“Right”]
def移动(自我):
全局x1、y1、x2、y2
对于范围内的索引(len(self.segments)-1):
segment=self.segments[index]。实例
x1,y1,x2,y2=c.coords(self.segments[-2]。实例)
c、 坐标(段,x1,y1,x2,
y2)
c、 coords(self.segments[-1]。实例,
x1+自向量[0]*分段大小,y1+自向量[1]*分段大小,
x2+自向量[0]*分段大小,y2+自向量[1]*分段大小)
def添加_段(自身):
last_seg=c.coords(self.segments[0]。实例)
x=最后一段[2]-段大小
y=最后一段[3]-段大小
self.segments.insert(0,segments(x,y))
def更改方向(自身、事件):
如果self.mapping中的event.keysym:
self.vector=self.mapping[event.keysym]
分段=[分段(分段大小,分段大小),
分段(分段尺寸*2,分段尺寸),
分段(分段尺寸*3,分段尺寸)]
s=蛇(段)
c、 绑定(“,s.改变方向)
main()
root.mainloop()
希望这有帮助

注:如果你想让我帮你