Commit cf26a0f8 by BellCodeEditor

auto save

parent 4512d6c8
Showing with 401 additions and 0 deletions
import pygame
from pygame import locals
import random
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
my_font=pygame.font.Font('neuropol.ttf',18)
q=15
w=pygame.font.Font('neuropol.ttf',q)
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading ="right"
snake_head =right
score=0
apple_x=360
apple_y=300
qw_1=random.randint(1,22)*30-30
qw_2=random.randint(1,15)*30-30
qw_3=random.randint(1,22)*30-30
qw_4=random.randint(1,15)*30-30
qw_5=random.randint(1,22)*30-30
qw_6=random.randint(1,15)*30-30
qw_7=random.randint(1,22)*30-30
qw_8=random.randint(1,15)*30-30
qw_9=random.randint(1,22)*30-30
qw_10=random.randint(1,15)*30-30
qw_11=random.randint(1,22)*30-30
qw_12=random.randint(1,15)*30-30
qw_13=random.randint(1,22)*30-30
qw_14=random.randint(1,15)*30-30
qw_15=random.randint(1,22)*30-30
qw_16=random.randint(1,15)*30-30
qw_17=random.randint(1,22)*30-30
qw_18=random.randint(1,15)*30-30
qw_19=random.randint(1,22)*30-30
qw_20=random.randint(1,15)*30-30
qw_21=random.randint(1,22)*30-30
qw_22=random.randint(1,15)*30-30
qw_23=random.randint(1,22)*30-30
qw_24=random.randint(1,15)*30-30
apple_x_1=random.randint(1,22)*30-30
apple_y_1=random.randint(1,15)*30-30
apple_x_2=random.randint(1,22)*30-30
apple_y_2=random.randint(1,15)*30-30
while True:
for event in pygame.event.get():
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
position.append((x, y))
if x==apple_x and y==apple_y:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
score+=10
elif x==apple_x_1 and y==apple_y_1:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
score+=10
elif x==apple_x_2 and y==apple_y_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
score+=10
elif apple_x_1==apple_x_2 and apple_y_1==apple_y_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
score+=10
elif apple_x==apple_x_2 and apple_y==apple_y_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
score+=10
else:
position.pop(0)
if x<0 or x>630 or y<0 or y>450:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
for i in range(len(position)-1):
if position[i]==position[-1]:
exit()
# 将果实画上去
screen.blit(food, (apple_x, apple_y))
info="Score:"+str(score)
text=my_font.render(info,True,(0,0,0))
screen.blit(text,(540,10))
screen.blit(body,(qw_1,qw_2))
screen.blit(body,(qw_3,qw_4))
screen.blit(body,(qw_5,qw_6))
screen.blit(body,(qw_7,qw_8))
screen.blit(body,(qw_9,qw_10))
screen.blit(body,(qw_11,qw_12))
screen.blit(body,(qw_13,qw_14))
screen.blit(body,(qw_15,qw_16))
screen.blit(body,(qw_17,qw_18))
screen.blit(body,(qw_19,qw_20))
screen.blit(body,(qw_21,qw_22))
screen.blit(body,(qw_23,qw_24))
screen.blit(food, (apple_x_1, apple_y_1))
screen.blit(food, (apple_x_2, apple_y_2))
if apple_x==qw_1 and apple_y==qw_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_3 and apple_y==qw_4:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_5 and apple_y==qw_6:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_7 and apple_y==qw_8:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_9 and apple_y==qw_10:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_11 and apple_y==qw_12:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_13 and apple_y==qw_14:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_15 and apple_y==qw_16:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_17 and apple_y==qw_18:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_19 and apple_y==qw_20:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_21 and apple_y==qw_22:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
elif apple_x==qw_23 and apple_y==qw_24:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x=30*num_1-30
apple_y=30*num_2-30
if apple_x_1==qw_1 and apple_y_1==qw_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_3 and apple_y_1==qw_4:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_5 and apple_y_1==qw_6:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_7 and apple_y_1==qw_8:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_9 and apple_y_1==qw_10:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_11 and apple_y_1==qw_12:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_13 and apple_y_1==qw_14:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_15 and apple_y_1==qw_16:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_17 and apple_y_1==qw_18:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_19 and apple_y_1==qw_20:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_21 and apple_y_1==qw_22:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
elif apple_x_1==qw_23 and apple_y_1==qw_24:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_1=30*num_1-30
apple_y_1=30*num_2-30
if apple_x_2==qw_1 and apple_y_2==qw_2:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_3 and apple_y_2==qw_4:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_5 and apple_y_2==qw_6:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_7 and apple_y_2==qw_8:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_9 and apple_y_2==qw_10:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_11 and apple_y_2==qw_12:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_13 and apple_y_2==qw_14:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_15 and apple_y_2==qw_16:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_17 and apple_y_2==qw_18:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_19 and apple_y_2==qw_20:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_21 and apple_y_2==qw_22:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
elif apple_x_2==qw_23 and apple_y_2==qw_24:
num_1=random.randint(1,22)
num_2=random.randint(1,16)
apple_x_2=30*num_1-30
apple_y_2=30*num_2-30
if qw_1==x and qw_2==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_3==x and qw_4==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_5==x and qw_6==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_7==x and qw_8==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_9==x and qw_10==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_11==x and qw_12==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_13==x and qw_14==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_15==x and qw_16==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_17==x and qw_18==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_19==x and qw_20==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_21==x and qw_22==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
elif qw_23==x and qw_24==y:
s="Game Over"
text_1=w.render(s,True,(1,1,1))
screen.blit(text_1,(30*11-30,30*8-30))
exit()
# 刷新画面
pygame.display.update()
FPSCLOCK.tick(10)
\ 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