2017-05-14 5 views
2

Derzeit arbeiten an der Gestaltung eines Snake-Spiel mit Python, mit import.draw, ohne pygame! Der Großteil meines Spiels ist vollendet und es funktioniert sehr gut, abgesehen von der Tatsache, dass jedes Mal, wenn ein Pfeil angeklickt wird (oben, unten, links oder rechts), die Länge der Schlange einfach größer wird, das ist nicht was ich will passieren, denn die Länge sollte erst größer werden, wenn die Schlange den Apfel isst! Dies ist mein Code: Zufalls Import Zeichnen ImportSnake Game Hilfe - nicht mit 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()

Antwort

0

Sie

lengthSnake += 1 

Draw.hasNextKeyTyped() truthy jedes Mal auszuführen ist (und die Taste ist "Links"/"Rechts"/"Auf"/"Ab").

0

Mit dieser while-Schleife teilen Sie dem Computer jedes Mal mit, wenn eine Taste gedrückt wird, fügen Sie einen Wert zur Länge der Schlange hinzu.

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` 

Entfernen Sie lengthSnake + = 1 von jedem if-Block.