目前正在使用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.show(( 主((
你执行
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 块中删除长度蛇 += 1。