Python蛇游戏没有抓点

时间:2018-06-04 19:07:35

标签: python tkinter

我试图在python中编写游戏并且它无法正常工作。 逻辑是它搜索一块......然后将i和j设置为碎片的坐标,如果值为-1(只有碎片有),那么它会继续,如果不是,那么走开,我不在乎你。然后通过减去x和y轴值的绝对值并且如果变量dist1大于var dist,它获得蛇与该块的距离。然后它获取最近一块的坐标,然后设置方向。 (如果x轴高于蛇的x轴,则向上(0))。事情是,它没有达到碎片或它没有抓住它们,idk。

from tkinter import Tk, Canvas
from random import randint
from PIL import ImageTk, Image

class Dil:
    def __init__(self,x,y,v):
        self.h = 0 #point value
        self.snake = None
        self.vzhled = platno.create_rectangle(x*v,y*v,(x+1)*v,(y+1)*v, fill="white", outline = "#CCC")


class snake:
    def __init__(self,x,y,lenght,dir,col,player,up,right,down,left):
        self.x = x
        self.y = y
        self.lenght = lenght
        self.dir = dir
        self.col = col
        self.player = player
        self.up = up
        self.right = right
        self.down = down
        self.left = left

    def logikablba(self):
        self.dir = randint(0,3)
        try:
            if self.dir == 0 and sit[self.x][self.y-1].h>0:
                self.logika()
            if self.dir == 1 and sit[self.x+1][self.y].h>0:
                self.logika()
            if self.dir == 2 and sit[self.x][self.y+1].h>0:
                self.logika()
            if self.dir == 3 and sit[self.x-1][self.y].h>0:
                self.logika()
        except:
            pass

    def logika(self):
        dist = 0
        dist1 = 0
        osax = 0
        osay = 0


        for i in range(ct):
            for j in range(ct):
                if sit[i][j].h == -1:
                    dist = abs(i-self.x) + abs(j-self.y)

                if dist >= dist1:
                    dist = dist1
                    osax = i
                    osay = j


        if osax > self.x:
            self.dir = 1

        if osax < self.x:
            self.dir = 3

        if osay < self.y:
            self.dir = 2

        if osay > self.y:
            self.dir = 0




def draw():
    for i in range(ct):
        for j in range(ct):
            if sit[i][j].h>0:
                sit[i][j].h -= 1
                if sit[i][j].h == 0:
                    platno.itemconfig(sit[i][j].vzhled, fill = "white")


def pohyb():
    for snake in snakei:
        if not snake.player: snake.logika()

        if snake.dir == 0: snake.y += -1
        if snake.dir == 1: snake.x += 1
        if snake.dir == 2: snake.y += 1
        if snake.dir == 3: snake.x += -1
        if snake.y<0:
            snake.y = ct-1
        if snake.x<0:
            snake.x = ct-1
        if snake.y==ct:
            snake.y = 0
        if snake.x==ct:
            snake.x = 0
        if sit[snake.x][snake.y].h <= 0:
            if sit[snake.x][snake.y].h == -1:
                #grabbed it

                drobek()
            sit[snake.x][snake.y].h = snake.lenght+1
            platno.itemconfig(sit[snake.x][snake.y].vzhled, fill = snake.col)



    draw()
    okno.after(50,pohyb)

def stisk(e):
    k = e.keysym
    for snake in snakei:
        if snake.player:
            if snake.dir!=2:
                if snake.up == k: snake.dir = 0

            if snake.dir!=3:
                if snake.right == k: snake.dir = 1

            if snake.dir!=0:
                if snake.down == k: snake.dir = 2

            if snake.dir!=1:
                if snake.left == k: snake.dir = 3

def klik (e):
    x = e.x//(600//ct)
    y = e.y//(600//ct)
    sit[x][y].h = -1
    platno.itemconfig(sit[x][y].vzhled, fill = "blue")


def drobek():
    x = randint(0, ct-1)
    y = randint(0, ct-1)
    while sit[x][y].h != 0:
        x = randint(0, ct-1)
        y = randint(0, ct-1)
    sit[x][y].h = -1
    platno.itemconfig(sit[x][y].vzhled, fill = "yellow")



ct = 25
ctDROBKU = 20

okno = Tk()
platno = Canvas(okno,width=600,height=600)
platno.pack()





sit = []
for i in range(ct):
    sit.append([])
    for j in range(ct):
        sit[i].append(Dil(i,j,600//ct))

for i in range(ctDROBKU):
    drobek()

karel = snake(20,20,15,0,"red",False,"Up","Right","Down","Left")
snakei = [karel]


okno.bind("<Key>",stisk)
okno.bind("<Button-1>",klik)
okno.after(1000, pohyb)
okno.mainloop()


                   ´

0 个答案:

没有答案