Commit 7b7776b9 by BellCodeEditor

save project

parent e934cfd0
Showing with 40 additions and 76 deletions
#初始化 import pygame
import pygame #导入pygame模块 from pygame import locals
from pygame import locals#导入locals模块 #引用locals模块
pygame.init()#初始化pygame,为使用pygame做准备 x,y = 240,120 #事先设置蛇头的位置
position = [(180,90),(180,120),(210,120),(x,y)]
#开"时钟" # 初始化pygame,为使用硬件做准备
FPSCLOCK = pygame.time.Clock()#把时钟模块赋值给变量 pygame.init()
# 创建一个窗口
#建窗口 screen = pygame.display.set_mode((660,480))
screen= pygame.display.set_mode((660,480))#创建660x480的游戏窗口 # pygame.display.set_mode((660, 480))
#pygame时钟,设定游戏快慢(帧数)
#设变量 FPSCLOCK = pygame.time.Clock()
snake_head = 'right'#蛇头图片变量,设为向右 # 加载好背景,用变量存起来
setheading ='right'#蛇头方向变量,设为向右
background = pygame.image.load('bg.png')
#预加载 right = pygame.image.load('right.png')
background = pygame.image.load('bg.png') #加载背景素材 food=pygame.image.load('apple.png')
right = pygame.image.load('right.png') #加载蛇头(右)素材 body=pygame.image.load('body.png')
right = pygame.image.load('left.png') #加载蛇头(左)素材 while True:
right = pygame.image.load('down.png') #加载蛇头(下)素材 for event in pygame.event.get(): #pygame.event.get()玩家所做的所有事件
right = pygame.image.load('up.png') #加载蛇头(上)素材 if event.type == locals.QUIT:
body = pygame.image.load('body.png') #加载蛇身素材             # 接收到退出事件后退出程序
#body = pygame.image.load('图片名.png')             exit() # 退出(关窗口)
x += 30
x, y = 240, 120 #蛇头坐标变量 position.append((x,y))
snake = [(180, 90), (180, 120), (210, 120), (x, y)] #蛇身和蛇头位置的列表 position.pop(position[0])
'''     # 将背景图渲染上去
x,y如同两个装货卡车,现在用x车和y车上的物品填充列表,将列表赋值给snake,     screen.blit(background,(0,0))
在随后xy车上的货物改变后,列表里对应位置的货物不会改变的,只有再次赋值的时     # 将贪吃蛇渲染上去
候才会从xy车上取更新的货物。     screen.blit(right, (x, y))
'''     # 将贪吃蛇的身体(3个)渲染上去
for i in range(len(position)-1):
#判断事件 screen.blit(body,(position[i]))
while True:#重复执行以下代码     # 将果实渲染上去
for event in pygame.event.get():#将所有玩家所做的事件赋值给event screen.blit(food, (360, 300))
if event.type == locals.QUIT:#如果事件的编号=locals.QUIT的编号(locals.QUIT也就是点击退出发生) #渲染是有顺序的,后渲染上的比前一个要靠上一层(后渲染上的可以覆盖前渲染上的)
exit()#退出程序 # 刷新画面(不然还是看不见图片)
if event.type == locals.KEYDOWN:#如果出现键盘事件     pygame.display.update()
if event.key ==locals.K_DOWN and setheading != 'up': FPSCLOCK.tick(3)
setheading = 'down'#按下“下”键且方向不冲上,方向就改为下 \ No newline at end of file
if event.key ==locals.K_UP and setheading != 'down':
setheading = 'up'#按下“上”键且方向不冲下,方向就改为上
if event.key ==locals.K_RIGHT and setheading != 'left':
setheading = 'right'#按下“右”键且方向不冲左,方向就改为右
if event.key ==locals.K_LEFT and setheading != 'right':
setheading = 'left'#按下“左”键且方向不冲右,方向就改为左
#检测方向,移动
if setheading == 'up':#如果方向为上
y -= 30 #y坐标减三十(向上挪30)
snake_head = 'up'#蛇头图案为“上”
if setheading == 'down':#如果方向为下
y += 30 #y坐标加三十(向下挪30)
snake_head = 'down'#蛇头图案为“下”
if setheading == 'left':#如果方向为左
x -= 30 #x坐标减三十(向左挪30)
snake_head = 'left'#蛇头图案为“左”
if setheading == 'right':#如果方向为右
x += 30 #x坐标加三十(向右挪30)
snake_head = 'right'#蛇头图案为“右”
# 随着x,y的增加,在列表加尾去头,实现整体移动
snake.append((x, y))#加尾
snake.pop(0)#去头
screen.blit(background, (0, 0))#渲染背景
screen.blit(snake_head,snake[-1])#按方向渲染头
#screen.blit(代表图片的变量名, (从x, y开始渲染))
#渲染蛇身
for i in range(len(snake)-1):#len()指取蛇身位置列表的长度
screen.blit(body, snake[i])
#通过-1来遍历列表中从第1项到倒数第2项所有坐标
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