Python游戏需要帮助吗

2024-10-05 11:05:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用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()

Tags: andtexteventgameifcreatefillglobal
1条回答
网友
1楼 · 发布于 2024-10-05 11:05:43

我看不到在屏幕上实际显示蛇的当前位置并在移动后将其移除的相关代码,但是如果您创建snake变量的长度并以迭代方式绘制和移除它,则可以在此处更改大小。当食物被吃掉时,你可以简单地增加蛇的长度变量的大小,并在蛇沿着其矢量前进的过程中暂停对蛇的移动的擦除,直到达到所需的生长,此时可以以新的长度速率进行移除。请澄清代码中实际呈现snakes位置的部分。在

相关问题 更多 >

    热门问题