Commit 1fe7b89f by BellCodeEditor

save project

parent 0f4da355
kk.jpg

67.4 KB

...@@ -6,11 +6,11 @@ score=0 ...@@ -6,11 +6,11 @@ score=0
pygame.init() pygame.init()
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((1080,1080))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 背景 # 背景
background = pygame.image.load('bg.png') background = pygame.image.load('kk.jpg')
right = pygame.image.load('right.png') # 头 朝右 right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果 food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体 body = pygame.image.load('body.png') # 身体
...@@ -34,29 +34,25 @@ while True: ...@@ -34,29 +34,25 @@ while True:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: if 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"
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"
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"
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"
snake_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:
y += 30 y += 30
position.append((x, y)) position.append((x, y))
if x==food_x and y==food_y: if x==food_x and y==food_y:
d_x=random.randint(1,22) d_x=random.randint(1,22)
d_y=random.randint(1,16) d_y=random.randint(1,16)
...@@ -76,4 +72,6 @@ while True: ...@@ -76,4 +72,6 @@ while True:
fen=font.render(text,True,(0,0,0)) fen=font.render(text,True,(0,0,0))
screen.blit(fen,(540,0)) screen.blit(fen,(540,0))
# 将果实画上去 # 将果实画上去
screen.blit(food, (food_x, food_y)) screen.blit(food, (food_x, food_y))
\ No newline at end of file pygame.display.update()
FPSCLOCK.tick(20)
\ 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