Commit d0264f15 by BellCodeEditor

save project

parent 701f32ab
Showing with 69 additions and 11 deletions
bad_food.png

174 Bytes

import pygame,random
from pygame import locals
# 初始化pygame,为使用pygame做准备
pygame.init()
# 帧率初始化
FPSCLICK = pygame.time.Clock()
......@@ -19,8 +23,12 @@ up = pygame.image.load("up.png")
down = pygame.image.load("down.png")
apple = pygame.image.load("apple.png") #屁股
bad_food = pygame.image.load("bad_food.png") # 坏食物
body = pygame.image.load("body.png") #身体
#字体
my_font = pygame.font.Font('neuropol.ttf',18)
# 头部初始化坐标
......@@ -30,6 +38,10 @@ x,y = 240,120
apple_x = 360
apple_y = 300
#坏屁股初始化坐标
bad_apple_x = 330
bad_apple_y = 240
# 吃屁股
eat_apple = 0
......@@ -39,7 +51,25 @@ position = [(180,120),(180,270),(210,120),(x,y)]
setheading = 'right'
snake_head = right
# 亿些封装
def move():
global apple_y,apple_x
apple_x = 30 * random.randint(0,21)
apple_y = 30 * random.randint(0,15)
def bad_apple_move():
global bad_apple_y,bad_apple_x
bad_apple_x = 30 * random.randint(0,21)
bad_apple_y = 30 * random.randint(0,15)
def cantKeyDonw():
global x,y
if x>630 or x<0 or y>480 or y<0:
return False
else:
return True
while True:
# global eat_apple
for event in pygame.event.get():
if event.type == 12:
# 接收到退出事件后退出程序
......@@ -48,19 +78,19 @@ while True:
# 按键设置
if event.type == locals.KEYDOWN:
# w键
if event.key == locals.K_w and setheading != 'down':
if event.key == locals.K_w and setheading != 'down' and cantKeyDonw():
setheading = 'up'
snake_head = up
# a键
if event.key == locals.K_a and setheading != 'right':
if event.key == locals.K_a and setheading != 'right' and cantKeyDonw():
setheading = 'left'
snake_head = left
# s键
if event.key == locals.K_s and setheading != 'up':
if event.key == locals.K_s and setheading != 'up' and cantKeyDonw():
setheading = 'down'
snake_head = down
# d键
if event.key == locals.K_d and setheading != 'left':
if event.key == locals.K_d and setheading != 'left' and cantKeyDonw():
setheading = 'right'
snake_head = right
......@@ -79,20 +109,40 @@ while True:
# 恰苹果
# 恰屁股 和 恰坏屁股
if (x,y) == (apple_x,apple_y):
apple_x = 30 * random.randint(0,16)
apple_x = 30 * random.randint(0,20)
move()
eat_apple += 1
elif (apple_x,apple_y) in position[0:-1]:
move()
elif (x,y) == (bad_apple_x,bad_apple_y):
bad_apple_move()
eat_apple -= 1
for i in range(2):
position.pop(0)
elif (apple_x,apple_y) in position[0:-1]:
bad_apple_move()
else:
position.pop(0)
# 碰墙退出
if x > 660 or y > 480 or x < 0 or y < 0:
exit()
# if x > 660 or y > 480 or x < 0 or y < 0:
# exit()
# 穿墙
if x < 0:
x = 630
if x > 630:
x = -30
if y < 0:
y = 480
if y > 480:
y = -30
# 分数退出
if eat_apple > 150:
if eat_apple > 15:
exit()
elif eat_apple < 0:
exit()
# 自杀退出
......@@ -108,11 +158,19 @@ while True:
# 屁股图片添加于屏幕
screen.blit(apple,(apple_x,apple_y))
# 坏屁股图片添加于屏幕
screen.blit(bad_food,(bad_apple_x,bad_apple_y))
# 身体图片添加于屏幕
for i in range(len(position)-1):
screen.blit(body,position[i])
# 绘制分数
info = 'Score' + str(eat_apple)
text = my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
# 刷新画面
pygame.display.update()
# 帧率
FPSCLICK.tick(5)
FPSCLICK.tick(6)
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