Commit 701f32ab by BellCodeEditor

save project

parent 8d92bff3
Showing with 108 additions and 17 deletions
import pygame
import pygame,random
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
# 帧率初始化
FPSCLICK = pygame.time.Clock()
# 创建一个窗口
baclground = pygame.image.load("bg.png")
right = pygame.image.load("right.png")
apple = pygame.image.load("apple.png")
body = pygame.image.load("body.png")
x,y = 240,120
position = [(180,120),(180,270),(210,120),(x,y)]
# 创建窗口
screen = pygame.display.set_mode((660,480))
# 导入图片
baclground = 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")
apple = pygame.image.load("apple.png") #屁股
body = pygame.image.load("body.png") #身体
# 头部初始化坐标
x,y = 240,120
# 屁股初始化坐标
apple_x = 360
apple_y = 300
# 吃屁股
eat_apple = 0
# 贪吃蛇每节身体的坐标的初始化
position = [(180,120),(180,270),(210,120),(x,y)]
setheading = 'right'
snake_head = right
while True:
for event in pygame.event.get():
if event.type == 12:
# 接收到退出事件后退出程序
exit()
# if event.type == locals.KEYDOWN:
# if event.key == event.K_DOWN:
# y += 30
# 按键设置
if event.type == locals.KEYDOWN:
# w键
if event.key == locals.K_w and setheading != 'down':
setheading = 'up'
snake_head = up
# a键
if event.key == locals.K_a and setheading != 'right':
setheading = 'left'
snake_head = left
# s键
if event.key == locals.K_s and setheading != 'up':
setheading = 'down'
snake_head = down
# d键
if event.key == locals.K_d and setheading != 'left':
setheading = 'right'
snake_head = right
# 设置贪吃蛇的移动方向(头部坐标)
if setheading == 'right':
x += 30
if setheading == 'left':
x -= 30
if setheading == 'up':
y -= 30
if setheading == 'down':
y += 30
position.append((x,y))
# 恰苹果
if (x,y) == (apple_x,apple_y):
apple_x = 30 * random.randint(0,16)
apple_x = 30 * random.randint(0,20)
eat_apple += 1
else:
position.pop(0)
# 碰墙退出
if x > 660 or y > 480 or x < 0 or y < 0:
exit()
# 分数退出
if eat_apple > 150:
exit()
# 自杀退出
if position[-1] in position[0:-1]:
exit()
# 背景图片添加于屏幕
screen.blit(baclground,(0,0))
screen.blit(right,(x,y))
screen.blit(apple,(360,300))
screen.blit(body,(210,120))
# 头部图片添加于屏幕
screen.blit(snake_head,position[-1])
# 屁股图片添加于屏幕
screen.blit(apple,(apple_x,apple_y))
for i in range(len(position)-1):
screen.blit(body,position[i])
pygame.display.update()
FPSCLICK.tick(3)
# 帧率
FPSCLICK.tick(5)
import pygame
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
while True:
screen = pygame.display.set_mode((1920,1080))
# 帧率初始化
FPSCLICK = pygame.time.Clock()
# 创建窗口
screen = pygame.display.set_mode((660,480))
while True:
for event in pygame.event.get():
if event.type == 12:
exit()
print(event)
\ 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