Commit bb2a2b4d by BellCodeEditor

save project

parent e273e0b5
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

import pygame
from pygame import locals
pygame.init()
screen = pygame.display.set_mode((660,480))
backgrade = 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')
#设置控制速度时钟模块
FPS = pygame.time.Clock()
#设置贪吃蛇的初始朝向
setheading = "right"
#设置贪吃蛇头部的朝向
snake_head = right
#贪吃蛇的头部
x,y=240,120
#贪吃蛇的移动:
#设置贪吃蛇的初始位置,按照从尾到头的顺序
pos = [(180,90),(180,120),(210,120),(x,y)]
while True:
#获取事件
for event in pygame.event.get():
# print(event)
#获取对应的事件id,与之匹配,然后执行相应的事件
if event.type == locals.QUIT:
exit()
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
#贪吃蛇头部向右移动
#x+=30
#时刻记录头部坐标
pos.append((x,y))
#最尾部往前移动了,所以删除掉
pos.pop(0)
screen.blit(backgrade,(0,0))
#用列表中的元素表示坐标
screen.blit(snake_head,pos[-1])
#获取贪吃蛇身体元素
for i in range(len(pos)-1):
screen.blit(body,pos[i])
# screen.blit(body,(210,120))
# screen.blit(body,(180,120))
# screen.blit(body,(180,90))
screen.blit(food,(360,300))
pygame.display.update()
#在一秒内刷新不超过3次
FPS.tick(3)
\ No newline at end of file
File added
right.png

2.05 KB

import pygame
from pygame import locals
pygame.init()
screen = pygame.display.set_mode((660,480))
backgrade = 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')
#设置控制速度时钟模块
FPS = pygame.time.Clock()
#设置贪吃蛇的初始朝向
setheading = "right"
#设置贪吃蛇头部的朝向
snake_head = right
#贪吃蛇的头部
x,y=240,120
#贪吃蛇的移动:
#设置贪吃蛇的初始位置,按照从尾到头的顺序
pos = [(180,90),(180,120),(210,120),(x,y)]
while True:
#获取事件
for event in pygame.event.get():
# print(event)
#获取对应的事件id,与之匹配,然后执行相应的事件
if event.type == locals.QUIT:
exit()
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
#贪吃蛇头部向右移动
#x+=30
#时刻记录头部坐标
pos.append((x,y))
#最尾部往前移动了,所以删除掉
pos.pop(0)
screen.blit(backgrade,(0,0))
#用列表中的元素表示坐标
screen.blit(snake_head,pos[-1])
#获取贪吃蛇身体元素
for i in range(len(pos)-1):
screen.blit(body,pos[i])
# screen.blit(body,(210,120))
# screen.blit(body,(180,120))
# screen.blit(body,(180,90))
screen.blit(food,(360,300))
pygame.display.update()
#在一秒内刷新不超过3次
FPS.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