Commit 5fe01f68 by BellCodeEditor

auto save

parent c6188a1e
Showing with 56 additions and 5 deletions
import pygame import pygame#导入pygame模块
import random#导入random模块
from pygame import locals#从pygame模块导入locals子模块
# 初始化pygame,为使用pygame做准备 # 初始化pygame,为使用pygame做准备
pygame.init() pygame.init()
background=pygame.image.load('bg.png')#载入背景图
right=pygame.image.load('right.png')#贪吃蛇朝右的蛇头载入
left=pygame.image.load('left.png')#贪吃蛇朝左的蛇头载入
up=pygame.image.load('up.png')#贪吃蛇朝上的蛇头载入
down=pygame.image.load('down.png')#贪吃蛇朝下的蛇头载入
food=pygame.image.load('apple.png')#将食物苹果载入
body=pygame.image.load('body.png')#将身体载入进去
x,y=240,120#将蛇头的坐标赋值给一个变量
apple_x=360
apple_y=300
position=[(180,90),(180,120),(210,120),(x,y)]#建立一个列表
setheading = "right" #设置贪吃蛇的朝向
snake_head=right
# 创建一个窗口 # 创建一个窗口
?? screen=pygame.display.set_mode(size=(660,480))
\ No newline at end of file FPSCLOCK=pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type==locals.QUIT:
exit() #监听事件,如果按下了x键,那么就退出
if 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))#将新坐标添加到新列表里
if x==apple_x and y==apple_y:
apple_x=random.randint(0,660)
apple_y=random.randint(0,480)
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,(apple_x,apple_y))#将食物画上去
pygame.display.update()#刷新画面
FPSCLOCK.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