需要Python Snake游戏的帮助

时间:2016-01-21 02:03:55

标签: python tkinter

我目前正在使用TKinter库在Python上创建一个Snake游戏。 所以现在,我已经实施了动作,食物系统和分数系统。我仍然需要一些帮助,让我在吃食物时能让蛇长出来。

这是我的代码(它正在运行,你可以测试它!):

from tkinter import *
import os
from random import randint

#CODED BY NOCIPH/ALTEUS

#---SETTING UP VARIABLES---
gameWindow = Tk()
gameWindow.title("ATSnake")

playerX1 = 100
playerY1 = 100
playerMov = 0
alreadyMove = 0

foodX1 = 10
foodX2 = 20
foodY1 = 10
foodY2 = 20
foodPresent = 0

score = 0


def key(event):
    global playerMov
    global alreadyMove

    if event.char == 'z' and playerMov != 3:
        playerMov = 1
        print("Input:",event.char,"playerX1: ",playerX1," - playerY1",playerY1,"Up")
    if event.char == 'q' and playerMov != 4:
        playerMov = 2
        print("Input:",event.char,"playerX1: ",playerX1," - playerY1",playerY1,"Left")
    if event.char == 's' and playerMov != 1:
        playerMov = 3
        print("Input:",event.char,"playerX1: ",playerX1," - playerY1",playerY1,"Down")
    if event.char == 'd' and playerMov != 2:
        playerMov = 4
        print("Input:",event.char,"playerX1: ",playerX1," - playerY1",playerY1,"Right")

    if alreadyMove == 0:
        game.after(500,moving)
        alreadyMove = 1

def moving():
    game.after_cancel(moving)
    global playerMov
    global playerX1
    global playerY1

    if playerMov == 1:
        playerY1 = playerY1 - 10
    if playerMov == 2:
        playerX1 = playerX1 - 10
    if playerMov == 3:
        playerY1 = playerY1 + 10
    if playerMov == 4:
        playerX1 = playerX1 + 10
    game.after(100,moving)
    game.coords(player, playerX1, playerY1)
    checkColl()

def checkColl():
    global playerX1
    global playerY1
    global alreadyMove
    global foodPresent

    #---CROSSING EDGES---
    #--- X ---
    if playerX1 > 200:
        playerX1 = 0
    else:
        if playerX1 < 0:
            playerX1 = 200

    #--- Y ---
    if playerY1 > 200:
        playerY1 = 0
    else:
        if playerY1 < 0:
            playerY1 = 200

    #--Check fruit collision
    print("foodPresent :",foodPresent)
    if playerX1 >= foodX1 and playerX1 <= foodX2 and playerY1 >= foodY1 and playerY1 <= foodY2:
        foodCom(1)
    else:
        foodCom(0)
    printScore()


def callback(event):
    game.focus_set()
    print ("clicked at", event.x, event.y)


def foodCom(command):          #---0: create  1: Delete
    global foodX1, foodY1, foodX2, foodY2, foodPresent, foodIni, food, score
    print("CallingFoodFonction")

    if command == 1:
        game.delete(food)
        foodPresent = 0
    else:
        if foodPresent == 0:
            foodX1  = randint(1,19) * 10
            foodY1  = randint(1,19) * 10
            foodX2 = foodX1 + 10
            foodY2 = foodY1 + 10
            food = game.create_rectangle(foodX1, foodY1, foodX2, foodY2, fill="red")

            score = score + 1
            foodPresent = 1
            foodIni = 0


def printScore():
    global score, scoreTxt
    game.delete(scoreTxt)
    scoreTxt = game.create_text(190, 190, text=score, font="Arial 10 bold", fill="grey")

#---OBJECT DEFINITION---
game = Canvas(gameWindow, width=200, height=200, background='white')
snakeTitle = game.create_text(100, 100, text="SNAKE", font="Arial 30 bold", fill="#EBEAEE")
PAK = game.create_text(100, 120, text="Press a key to start", font="Arial 15", fill="#EBEAEE")
CTRLs = game.create_text(100, 140, text="Use ZQSD to move", font="Arial 15", fill="#EBEAEE")
player = game.create_text(100, 100, text="X", font="Arial 15", fill="blue")
scoreTxt = game.create_text(190, 190, text=score, font="Arial 10 bold", fill="grey")


#---BINDING---
game.bind("<Key>", key)
game.bind("<Button-1>", callback)
game.pack()

foodCom(0)
gameWindow.mainloop()

1 个答案:

答案 0 :(得分:0)

我没有看到实际显示蛇在屏幕上的当前位置的关联代码并在移动后将其移除,但是如果你使蛇的长度变量并绘制它,你可以在这里改变大小并以迭代方式删除。当食用食物时,你可以简单地增加蛇长变量的大小,并在它沿着它的向量进行时暂停蛇的运动,直到所需的生长发生,此时可以以新的长度速率进行移除。请澄清实际呈现蛇位置的代码部分。