2016-01-21 172 views
1

我目前正在Python上使用TKinter庫創建一個貪吃蛇遊戲。 所以現在我已經實現了動作,食物系統和評分系統。我仍然需要一些幫助,以瞭解如何讓蛇在吃食物時成長。需要幫助的Python蛇遊戲

這裏是我的代碼(它的工作,你可以試一下!):

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() 
+0

lels重新使用通知版權。我認爲答案也應包含許可證。 – Ross

+0

在一個8小時的項目上工作,並且你會參加隊友 – Amperclock

+0

你發佈到Stack Overflow的任何內容都將受到Creative Commons許可條款的約束。您可以通過查看頁面右下角並單擊cc-wiki來查找更多信息。 – Ross

回答

0

我沒有看到實際顯示在屏幕上的蛇的當前位置相關的代碼和運動後刪除,但是如果你使蛇的長度變化並且以迭代的方式繪製並移除它,這就是你可以改變大小的地方。當食物被吃掉時,你可以簡單地增加蛇長度變量的大小,並且隨着蛇的移動沿着其向量進行,直到期望的增長髮生時暫停清除蛇的移動,此時可以以新的長度進行移動。請澄清實際呈現蛇位置的代碼部分。

+0

嗨,沒有屏幕刷新過程。我有理由改變「蛇」的座標:game.coords(player,playerX1,playerY1)。 我會嘗試刷新一個新版本,比如評分值 – Amperclock

+0

在這種情況下,每次食物被吃掉時,增加蛇的長度變量的大小並存儲相應的X和Y座標在位置字典中。在碰撞測試中,迭代位置字典以確認移動位置不會與當前的蛇位置相沖突。在運動過程中,遍歷位置字典來增加每條蛇的數量,而不是將蛇作爲單個變量移動。 –