Commit 00b0c339 by BellCodeEditor

save project

parent e6b0378c
Showing with 20 additions and 14 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
# 初始化pygame,为使用硬件做准备 # 初始化pygame,为使用硬件做准备
pygame.init() pygame.init()
...@@ -24,6 +24,7 @@ staion=[(180,90),(180,120),(210,120),(x,y)] ...@@ -24,6 +24,7 @@ staion=[(180,90),(180,120),(210,120),(x,y)]
setheading = "right" setheading = "right"
snack_head = right snack_head = right
apple_x,apple_y = 360,300
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -31,27 +32,27 @@ while True: ...@@ -31,27 +32,27 @@ while True:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
elif event.type == locals.KEYDOWN: elif event.type == locals.KEYDOWN:
if event.type == locals.K_RIGHT and setheading !="left": if event.key == locals.K_RIGHT and setheading !="left":
setheading = "right" setheading = "right"
snack_head = right snack_head = right
if event.type == locals.K_LEFT and setheading !="right": if event.key == locals.K_LEFT and setheading !="right":
setheading = "right" setheading = "left"
snack_head = right snack_head = left
if event.type == locals.K_UP and setheading !="down": if event.key == locals.K_UP and setheading !="down":
setheading = "right" setheading = "up"
snack_head = right snack_head = up
if event.type == locals.K_DOWN and setheading !="up": if event.key == locals.K_DOWN and setheading !="up":
setheading = "right" setheading = "down"
snack_head = right snack_head = down
if setheading == "right": if setheading == "right":
x-=30 x+=30
elif setheading =="left" : elif setheading =="left" :
x+=30 x-=30
elif setheading == "up": elif setheading == "up":
y-=30 y-=30
else: else:
...@@ -59,6 +60,10 @@ while True: ...@@ -59,6 +60,10 @@ while True:
staion.append((x,y)) staion.append((x,y))
staion.pop(0) staion.pop(0)
if apple_x == x and apple_y == y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
# 将贪吃蛇画上去 # 将贪吃蛇画上去
...@@ -67,7 +72,7 @@ while True: ...@@ -67,7 +72,7 @@ while True:
for i in range(len(staion)-1): for i in range(len(staion)-1):
screen.blit(body,staion[i]) screen.blit(body,staion[i])
# 将果实画上去 # 将果实画上去
screen.blit(food, (360, 300)) screen.blit(food, (apple_x,apple_y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK .tick(2) FPSCLOCK .tick(2)
\ No newline at end of file
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