Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Snake游戏帮助-不使用Pygame(Python)_Python_Graphics_Cluster Computing_Draw - Fatal编程技术网

Snake游戏帮助-不使用Pygame(Python)

Snake游戏帮助-不使用Pygame(Python),python,graphics,cluster-computing,draw,Python,Graphics,Cluster Computing,Draw,目前正在使用python设计一个蛇游戏,带有import.draw,没有pygame!我的大部分游戏都已经完成了,并且运行得很好,除了每次点击箭头(上、下、左、右)时,蛇的长度都会变大,这不是我想要的,因为蛇吃了苹果后,蛇的长度才会变大! 这是我的代码: 进口提款 随机输入 Draw.setCanvasSize(600,600) masterList = [[None for col in range(30)] for row in range(30)] def startscreen():

目前正在使用python设计一个蛇游戏,带有import.draw,没有pygame!我的大部分游戏都已经完成了,并且运行得很好,除了每次点击箭头(上、下、左、右)时,蛇的长度都会变大,这不是我想要的,因为蛇吃了苹果后,蛇的长度才会变大! 这是我的代码: 进口提款 随机输入

Draw.setCanvasSize(600,600)
masterList = [[None for col in range(30)] for row in range(30)]

def startscreen():  #WELCOME SCREEN
#set screen
#(may change to loop later)
Draw.setBackground(Draw.CYAN)
Draw.setColor(Draw.RED)
Draw.filledRect(0,100,600,200)
Draw.setColor(Draw.GREEN)
Draw.filledRect(0,200,600,200)
Draw.setColor(Draw.PINK)
Draw.filledRect(0,200,600,200)
Draw.setColor(Draw.BLUE)
Draw.filledRect(0,300,600,200)
Draw.setColor(Draw.YELLOW)
Draw.filledRect(0,400,600,200)
Draw.setColor(Draw.VIOLET)
Draw.filledRect(0,500,600,200)
Draw.setFontSize(80)
Draw.setFontFamily("Courier")
a = "SNAKE GAME"
Draw.setColor(Draw.BLACK)
Draw.string(a,50,220)
Draw.setFontSize(35)
Draw.string("CLICK ANYWHERE TO CONTINUE",30,330)

def clickAnywhere():
while True:
    if Draw.mousePressed():
        return True

#code for press enter to continue using draw package and keys
def playGame(size):
#choose starting point for apple
appleX = random.randrange(0, 580, 20)
appleY = random.randrange(0, 580, 20)  
positionApple = random.randrange(0, 580, 20) 

masterList[positionApple//20][positionApple//20] = 'a'

# initial position of snake in middle
lengthSnake = 4 # four boxes
positionSnakeX = 300
positionSnakeY = 300
for i in range(15, 19):
    masterList[15][i] = 'x'

drawTheBoard(appleX,appleY, positionSnakeX, positionSnakeY,lengthSnake)

#while positionSnake != 0 and positionSnake != 600:
while True:
    # check if user pressed a key to change snake direction
    if Draw.hasNextKeyTyped():
        newKey = Draw.nextKeyTyped()
        if newKey == "Left":
            positionSnakeX -= 20
            lengthSnake += 1
        elif newKey == "Right":
            positionSnakeX += 20   
            lengthSnake += 1
        elif newKey == "Up":
            positionSnakeY -= 20 
            lengthSnake += 1
        elif newKey == "Down":
            positionSnakeY += 20  
            lengthSnake += 1

        drawTheBoard(appleX,appleY, positionSnakeX, positionSnakeY, 
lengthSnake)


    # if snake hit the apple, then add a box to the snake
    if hitApple(positionSnakeX, positionSnakeY, positionApple, positionApple):
        lengthSnake += 1
        masterList[positionApple/20][positionApple/20] = None
        positionApple = random.randrange(0, 580, 20)
        masterList[positionApple/20][positionApple/20] = 'a'

    #check if snake hit wall using hitWall()
  #      if hitWall(positionSnake, positionSnake):
   #          return False # will this break the while loop 

    # check if snake hit itself

    # otherwise draw the board
    #    drawTheBoard(positionApple, positionSnakeX, positionSnakeY, 
   lengthSnake) # function that clears and resets board after each round
    #def moveSnake(s,dx,dy):
   #head= s[0]
   #news= [[head[0] + dy, head[1]+dx]]+s[0:-1]]
   #return news

 def drawTheBoard(appleX,appleY,snakeX,snakeY,lengthSnake):
#clear Canvas
Draw.clear()
#Draw Snake using for loop to draw the number of rectangles corresponding to
#how many boxes in 2D list it has
Draw.picture("snakeBackground.gif",0,0)
Draw.setColor(Draw.BLACK)
for i in range(lengthSnake):
    Draw.filledRect(snakeX+(20*i), snakeY, 20, 20)
    # change the x, y coordinates based on 2D list

# Draw Apple
Draw.setColor(Draw.RED)
drawApple(appleX, appleY)
if snakeX == appleX and snakeY == appleY:
    drawApple()

    #return true
    #if snake hit wall
    #return False
Draw.show()

 def drawApple(appleX, appleY):
   #appleX = random.randrange(0, 580, 20) 
   #appleY = random.randrange(0, 580, 20) 
   Draw.setColor(Draw.RED)
   Draw.filledOval(appleX, appleY, 20, 20)       

 def hitApple(snakeX, snakeY, appleX, appleY):
if (appleX < snakeX < appleX + 20) or (appleY < snakeY < appleY + 20):
    return True
return False

 #def hitWall(snakeCoords):
#use x, y coordinates to check if snake hit wall, returning True or False
#if snake's x is less than 0 or x is greater than or equal to numCols
#if y is less than 0 or y is greater than or equal to numRows
#return True
# Else, return False
# (0,600) is considered hitting the wall

 def main():
startscreen()
if clickAnywhere():
    Draw.clear()
#clear the screen
snakeSize = 4
#for each of x times
#ans= playGame(size)
#If ans= playGame(size):
#snakeSize += 1
playGame(snakeSize)
Draw.setCanvasSize(600600)
主列表=[[范围(30)中的列无](30)中的行]
def startscreen():#欢迎屏幕
#设置屏幕
#(以后可能会更改为循环)
图.立根背景(图.青色)
Draw.setColor(Draw.RED)
绘制填充图(0100600200)
Draw.setColor(Draw.GREEN)
绘制填充图(02000600200)
Draw.setColor(Draw.PINK)
绘制填充图(02000600200)
Draw.setColor(Draw.BLUE)
绘制填充图(0300600200)
Draw.setColor(Draw.YELLOW)
绘图.填充图(0400600200)
Draw.setColor(Draw.VIOLET)
绘制填充图(0500600200)
Draw.setFontSize(80)
Draw.setFontFamily(“信使”)
a=“蛇游戏”
Draw.setColor(Draw.BLACK)
绘制字符串(a,50220)
Draw.setFontSize(35)
Draw.string(“单击任意位置继续”,30330)
def clickAnywhere():
尽管如此:
如果Draw.mousePressed():
返回真值
#按enter键继续使用绘图包和键的代码
def游戏(大小):
#选择苹果的起点
appleX=random.randrange(0580,20)
appleY=random.randrange(058020)
positionApple=random.randrange(0580,20)
主列表[positionApple//20][positionApple//20]=“a”
#蛇在中间的初始位置
长度蛇=4#四个盒子
位置蛇X=300
位置snakey=300
对于范围(15,19)内的i:
主列表[15][i]=“x”
拉丝板(appleX、appleY、positionSnakeX、PositionSnake、lengthSnake)
#而蛇0和0!=600:
尽管如此:
#检查用户是否按键更改蛇行方向
如果Draw.hasNextKeyTyped():
newKey=Draw.nextKeyTyped()
如果newKey==“Left”:
位置蛇X-=20
长度+=1
elif newKey==“右”:
位置蛇X+=20
长度+=1
elif newKey==“向上”:
位置snakey-=20
长度+=1
elif newKey==“向下”:
位置键+=20
长度+=1
绘图板(appleX、appleY、positionSnakeX、positionSnakeY、,
长度(蛇)
#如果蛇撞到了苹果,那么在蛇身上加一个盒子
如果hitApple(位置Snakex、位置Snakey、位置Apple、位置Apple):
长度+=1
主列表[positionApple/20][positionApple/20]=无
positionApple=random.randrange(0580,20)
主列表[positionApple/20][positionApple/20]=“a”
#使用hitWall()检查蛇是否撞击墙壁
#如果hitWall(位置蛇,位置蛇):
#返回False#这会中断while循环吗
#检查蛇是否撞到自己
#否则就画黑板
#绘图板(位置苹果、位置斯内克斯、位置斯内克斯、,
lengthSnake)#在每一轮后清除和重置电路板的功能
#def移动蛇(s、dx、dy):
#水头=s[0]
#新闻=[[head[0]+dy,head[1]+dx]]+s[0:-1]]
#返回消息
def牵引板(appleX、appleY、snakeX、snakeY、lengthSnake):
#透明帆布
Draw.clear()
#绘制Snake使用for循环绘制对应于
#二维列表中有多少个框
Draw.picture(“snakeBackground.gif”,0,0)
Draw.setColor(Draw.BLACK)
对于范围内的i(长度):
Draw.filledRect(蛇X+(20*i),蛇,20,20)
#基于二维列表更改x、y坐标
#画苹果
Draw.setColor(Draw.RED)
苹果树(苹果树,苹果树)
如果snakeX==appleX和snakeY==appleY:
苹果
#返回真值
#如果蛇撞到墙
#返回错误
Draw.show()
def drawApple(appleX、appleY):
#appleX=random.randrange(0580,20)
#appleY=random.randrange(058020)
Draw.setColor(Draw.RED)
Draw.filledOval(appleX,appleY,20,20)
def hitApple(蛇形、蛇形、appleX、appleY):
如果(appleX
#Draw.show() main()

您执行

lengthSnake += 1

每次
Draw.hasNextKeyTyped()
都是真实的(并且键是“左”/“右”/“上”/“下”)。

通过这个while循环,每次按下一个键时,您都会告诉计算机在蛇的长度上增加一个

while True:
    # check if user pressed a key to change snake direction
    if Draw.hasNextKeyTyped():
        newKey = Draw.nextKeyTyped()
        if newKey == "Left":
            positionSnakeX -= 20
            lengthSnake += 1
        elif newKey == "Right":
            positionSnakeX += 20   
            lengthSnake += 1
        elif newKey == "Up":
            positionSnakeY -= 20 
            lengthSnake += 1
        elif newKey == "Down":
            positionSnakeY += 20  
            lengthSnake += 1`enter code here`
从每个if块中移除lengthSnake+=1