Commit ff765c25 by BellCodeEditor

save project

parent f06660f4
Showing with 34 additions and 18 deletions
import pygame
from pygame import locals
# 初始化pygame,为使用硬件做准备
x,y=240,120
weizhi=[(180,90),(180,120),(210,120),(x,y)]
pygame.init()
CLOCK=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')
x,y=240,120
left=pygame.image.load('left.png')
up=pygame.image.load('up.png')
down=pygame.image.load('down.png')
chaoxiang = "right"
snake_head = right
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
# 将背景图画上去
if event.type==locals.KEYDOWN:
if event.key==locals.K_RIGHT and chaoxiang !='left':
chaoxiang='right'
snake_head=right
if event.key==locals.K_LEFT and chaoxiang !='right':
chaoxiang='left'
snake_head=left
if event.key==locals.K_UP and chaoxiang !='down':
chaoxiang='up'
snake_head=up
if event.key==locals.K_DOWN and chaoxiang !='up':
chaoxiang='down'
snake_head=down
#设置头坐标
if chaoxiang=='right':
x+=30
elif chaoxiang=='left':
x-=30
elif chaoxiang=='up':
y-=30
else:
y+=30
weizhi.append((x,y))
weizhi.pop(0)
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, (x, y))
x+=30
# 将贪吃蛇的身体画上去
screen.blit(body, (210, 120))
screen.blit(body, (180, 120))
screen.blit(body, (180, 90))
# 将果实画上去
screen.blit(snake_head, (x, y))
for i in range(len(weizhi)-1):
screen.blit(body,weizhi [i])
screen.blit(food, (360, 300))
# 刷新画面
pygame.display.update()
CLOCK.tick(3)
\ 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