Commit 9fdaa4c4 by BellCodeEditor

save project

parent c9c8c75a
Showing with 57 additions and 5 deletions
# 勇于挑战的创造师,请运行以下代码,看看能不能删掉水果列表中的香蕉,运行结果是什么呢?
list=['苹果','香蕉','西瓜','香蕉','桃子','香蕉']
list.remove('香蕉')
print(list)
\ No newline at end of file
import pygame,sys,time,random;pygame.init()#初始化
x = [];y = [];rec = [(302,272),(302,302),(302,332),(302,362)];recx = [];recy = [];to= 'up';to_='up'#方向
size = width,height = 28,28;block = pygame.Surface(size);block.fill((0,0,0));rect = block.get_rect();score = 0#精灵
snake = pygame.Surface(size);snake.fill((255,0,0));rect_snake = snake.get_rect()
food = pygame.Surface(size);food.fill((0,255,0));rect_food = food.get_rect();food_count=0;time1=time.time();gamein_ = 0#游戏状态,0为运行中,1为失败状态
while True:#主循环
if gamein_ == 0:times = time.time()-time1#时间
time.sleep(0.3*0.995**round(times))#逐渐加快速度
def around(to_):#移动
global to
if to_ == 'left' and(to == 'up'or to == 'left' or to == 'down'):to='left';rec.pop(-1);rec.insert(0,tuple((rec[0][0]-30,rec[0][1])))
elif to_ == 'right' and(to == 'up'or to == 'right' or to == 'down'):to='right';rec.pop(-1);rec.insert(0,tuple((rec[0][0]+30,rec[0][1])))
elif to_ == 'up' and(to == 'right'or to == 'left' or to == 'up'):to='up';rec.pop(-1);rec.insert(0,tuple((rec[0][0],rec[0][1]-30)))
elif to_ == 'down' and(to == 'down'or to == 'right' or to == 'left'):to='down';rec.pop(-1);rec.insert(0,tuple((rec[0][0],rec[0][1]+30)))
elif (to_ == 'down' and to == 'up') or (to_ == 'left' and to == 'right') or (to_ == 'up' and to == 'down') or (to_ == 'right' and to == 'left'):break_()#如果移动方向与头向相反,则失败
def foods():#随机食物的出现
global food_count,set_,food_x,food_y,score
if round(times) % 5 == 0 and food_count == 0:food_count += 1;set_ = 2+30*random.randint(1,19),2+30*random.randint(1,18)#最多5秒
elif food_count == 1:#至多出现一个食物
main.blit(food,set_);food_x = set_[0];food_y = set_[1]
for snake_body in range(len(rec)):
if rec[snake_body][0] == food_x and rec[snake_body][1] == food_y:
food_count -= 1;set_ = 2+30*random.randint(1,19),2+30*random.randint(1,18)
if to == 'up':rec.append((rec[-1][0],rec[-1][1]+30));score += 10#根据方向在末尾加方块
elif to == 'down':rec.append((rec[-1][0],rec[-1][1]-30));score += 10
elif to == 'left':rec.append((rec[-1][0]+30,rec[-1][1]));score += 10
elif to == 'right':rec.append((rec[-1][0]-30,rec[-1][1]));score += 10
def break_():#游戏结束
global rec,gamein_;rec = [(2000,2000)];gamein_ = 1
def game():#死亡检测:方向与撞击位置需同步
for snake_body in range(len(rec)):
if gamein_ == 0:recx.append(rec[snake_body][0]);recy.append(rec[snake_body][1])#分别记录所有的x值和y值
if (to == 'left' and min(recx) <= -28) or (to == 'right' and max(recx) >= 602) or (to == 'up' and min(recy) <= -28) or (to == 'down' and max(recy) >= 602):break_()
elif snake_body < len(rec)-1 and (rec[0] == rec[-snake_body-1]):break_()#如果头撞尾,则失败
for event in pygame.event.get():
if event.type == pygame.QUIT:pygame.quit();sys.exit()
if event.type == pygame.KEYDOWN:#按键
if event.key == pygame.K_a:to_='left'
elif event.key == pygame.K_d:to_='right'
elif event.key == pygame.K_w:to_='up'
elif event.key == pygame.K_s:to_='down'
elif gamein_ == 1 and event.key == pygame.K_q:gamein_ = 0;x = [];y = [];rec = [(302,272),(302,302),(302,332),(302,362)];recx = [];recy = [];to= 'up';to_='up';score = 0;time1=time.time()
def snakes():
for xy in rec:main.blit(snake,xy)
main = pygame.display.set_mode((602,572));main.fill((255,255,255))
for i in range(2,602,30):#背景黑块,相邻2格间隙
for a in range(2,602,30):x.append(i);y.append(a);main.blit(block,(i,a))
if gamein_ == 0:snakes();around(to_);foods();game()
elif gamein_ == 1:
text = pygame.font.SysFont("华文楷体",50).render("游戏结束",1,(255,255,255));main.blit(text,(180,160));scores = score+int(times);file = open("C:/best.txt","r+t");\
textscore = pygame.font.SysFont("华文楷体",40).render("你的分数:%d"%scores,1,(255,255,255));main.blit(textscore,(180,240));f = file.read();\
textbest = pygame.font.SysFont("华文楷体",40).render("历史最高分数:"+f,1,(255,255,255));main.blit(textbest,(180,310));\
textagain = pygame.font.SysFont("华文楷体",40).render("按q再来一次",1,(255,255,255));main.blit(textagain,(180,380)) #游戏结束显示的文字
if scores > int(f):file.seek(0);file.truncate();file.write(str(scores))#存储最高分数
file.close()
pygame.display.flip();pygame.time.delay(5)#屏幕刷新,载帧
\ 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