目前正在使用python设计蛇游戏,使用import.draw,没有pygame!我的大部分游戏都已经完成并且运行得非常好,除了每次点击箭头(向上,向下,向左或向右)之后蛇的长度变得更大,这不是我想要的发生这种情况,因为一旦蛇吃掉苹果,长度应该变大! 这是我的代码: 导入Draw 随机导入
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 :(得分:0)
你执行
lengthSnake += 1
每次Draw.hasNextKeyTyped()
都是真实的(关键是&#34;左&#34; /&#34;右&#34; /&#34; Up&#34; /&#34; Down&# 34。)
答案 1 :(得分: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。