Commit 45f49804 by BellCodeEditor

save project

parent ef7dc539
apple.png

2.05 KB

bg.png

22.5 KB

body.png

1.4 KB

down.png

2.01 KB

left.png

2.07 KB

File added
right.png

2.05 KB

import pygame
from pygame import locals
pygame.init()
FPSCLOCK = pygame.time.Clock()
screen = pygame.display.set_mode((660, 480))
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')#头朝右
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
left = pygame.image.load('left.png')#头朝左
up = pygame.image.load('up.png')#头朝上
down = pygame.image.load('down.png')#头朝下
x,y=240,120
position=[(180,90),(180,120),(210,120),(x,y)]
setheading = 'right'#头的初始方向
snake_head = right#头的初始造型
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
elif event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != 'left':#按下右且当前!=左
setheading = 'right'
snake_head = right
if event.key == locals.K_LEFT and setheading != 'right':#按下左且当前!=右
setheading = 'left'
snake_head = left
if event.key == locals.K_UP and setheading != 'down':#按下上且当前!=下
setheading = 'up'
snake_head = up
if event.key == locals.K_DOWN and setheading != 'up':#按下下且当前!=上
setheading = 'down'
snake_head = down
if setheading == 'right':#头坐标
x += 30
elif setheading == 'left':#头坐标
x -= 30
elif setheading == 'up':#头坐标
y -= 30
else:#头坐标
y += 30
position.append((x,y))
position.pop(0)
screen.blit(background, (0, 0))
screen.blit(snake_head,position[-1])
for i in range(len(position)-1):
screen.blit(body,position[i])
screen.blit(food, (360, 300))
pygame.display.update()
pygame.display.update()
FPSCLOCK.tick(3)
\ No newline at end of file
up.png

2.05 KB

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