Commit d354f024 by BellCodeEditor

save project

parent 69cdbf6b
Showing with 11 additions and 8 deletions
...@@ -22,7 +22,7 @@ apple_x=360 ...@@ -22,7 +22,7 @@ apple_x=360
apple_y=300 apple_y=300
setheading="right" #头部初始变量 setheading="right" #头部初始变量
snack_head=right #头部初始朝向 snake_head=right #头部初始朝向
while True: while True:
for event in pygame.event.get(): #遍历事件 for event in pygame.event.get(): #遍历事件
...@@ -31,16 +31,16 @@ while True: ...@@ -31,16 +31,16 @@ while True:
elif event.type==locals.KEYDOWN: elif event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and setheading!='left': if event.key==locals.K_RIGHT and setheading!='left':
setheading='right' setheading='right'
snack_head=right snake_head=right
if event.key==locals.K_LEFT and setheading!='right': if event.key==locals.K_LEFT and setheading!='right':
setheading='left' setheading='left'
snack_head=left snake_head=left
if event.key==locals.K_UP and setheading!='down': if event.key==locals.K_UP and setheading!='down':
setheading='up' setheading='up'
snack_head=up snake_head=up
if event.key==locals.K_DOWN and setheading!='up': if event.key==locals.K_DOWN and setheading!='up':
setheading='down' setheading='down'
snack_head=down #判断按键 snake_head=down #判断按键
if setheading=='right': if setheading=='right':
x+=30 x+=30
elif setheading=='left': elif setheading=='left':
...@@ -51,9 +51,12 @@ while True: ...@@ -51,9 +51,12 @@ while True:
y+=30 #移动 y+=30 #移动
position.append((x,y)) position.append((x,y))
if x==apple_x and y==apple_y: if x==apple_x and y==apple_y:
apple_x=random.randint(0,660) num1=random.randint(0,22)
apple_y=random.randint(0,480) num2=random.randint(0,16)
position.pop(0) apple_x=num1*30-30
apple_y=num2*30-30
else:
position.pop(0)
screen.blit(background, (0, 0)) #将背景图画上去 screen.blit(background, (0, 0)) #将背景图画上去
screen.blit(right,position[-1]) #将贪吃蛇画上去 screen.blit(right,position[-1]) #将贪吃蛇画上去
for i in range(len(position)-1): for i in range(len(position)-1):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment