2017-05-14 812 views
2

目前正在使用python来设计一个使用python的,使用import.draw而不使用pygame的蛇游戏!我的大部分游戏都已经完成并且工作得很好,除了任何时候点击箭头(向上,向下,向左或向右)蛇的长度变得更大,这不是我想要的要发生,因为一旦蛇吃了苹果,它的长度应该会变得更大! 这是我的代码: 进口绘制 进口随机贪吃蛇游戏帮助 - 不使用Pygame(Python)

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.show() 的main()

回答

0

您执行

lengthSnake += 1 

每次Draw.hasNextKeyTyped()是truthy(和键是“左”/“右”/“上”/“下”)。

0

有了这个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。