使用python,85行代碼實(shí)現(xiàn)貪吃蛇和關(guān)卡升級(jí)你覺(jué)得你能堅(jiān)持到第10關(guān)嗎? 一、實(shí)現(xiàn)效果 二、環(huán)境要求 1.python 3+ 三、源碼分享 import pygameimport sysimport random SCREEN = {'x': 600, 'y': 600} # 全局定義屏幕尺寸class Snake(object): # 蛇類def __init__(self): # 初始化各種需要的屬性 [開(kāi)始時(shí)默認(rèn)向右/身體塊x5]self.dirction, self.body = pygame.K_RIGHT, [][self.add_node() for x in range(5)]def add_node(self):node = pygame.Rect(((self.body[0].left, self.body[0].top) if self.body else (0, 0)) + (25, 25)) # 隨時(shí)增加蛇塊if self.dirction == pygame.K_LEFT:node.left -= 25elif self.dirction == pygame.K_RIGHT:node.left += 25elif self.dirction == pygame.K_UP:node.top -= 25elif self.dirction == pygame.K_DOWN:node.top += 25self.body.insert(0, node)def del_node(self):self.body.pop()def is_dead(self):body_h = self.body[0]# 撞墻、撞自己則死亡if body_h.x not in range(SCREEN['x']) or body_h.y not in range(SCREEN['y']) or body_h in self.body[1:]:return Truedef move(self):self.add_node()self.del_node()def change_direction(self, curkey): # 改變方向 但是左右、上下不能被逆向改變LR, UD = [pygame.K_LEFT, pygame.K_RIGHT], [pygame.K_UP, pygame.K_DOWN]if curkey in LR + UD:if (curkey in LR) and (self.dirction in LR) or (curkey in UD) and (self.dirction in UD):returnself.dirction = curkeyclass Food: # 食物類def __init__(self):self.rect = pygame.Rect(-25, 0, 25, 25)def remove(self):self.rect.x = -25def set(self):if self.rect.x == -25:allpos = [pos for pos in range(75, SCREEN['x'] - 75, 25)] # 生成的食物距離墻在75 ~ SCREEN['x']-55 之間self.rect.left, self.rect.top = random.choice(allpos), random.choice(allpos)def show_text(screen, pos, text, color, font_bold=False, font_size=30, font_italic=False):cur_font = pygame.font.SysFont("SimHei", font_size, True) # 設(shè)置文字樣式cur_font.set_bold(font_bold) # 設(shè)置是否加粗屬性cur_font.set_italic(font_italic) # 設(shè)置是否斜體屬性text_fmt = cur_font.render(text, True, color) # 設(shè)置文字內(nèi)容screen.blit(text_fmt, pos) # 繪制文字def main():pygame.init()screen = pygame.display.set_mode((SCREEN['x'], SCREEN['y']))pygame.display.set_caption('貪吃蛇:是男人就堅(jiān)持到第10關(guān)!')clock, scores, isdead = pygame.time.Clock(), 0, Falsesnake, food = Snake(), Food() # 蛇/食物while True:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()if event.type == pygame.KEYDOWN:snake.change_direction(event.key)if event.key == pygame.K_SPACE and isdead: # 死后按space重新return main()screen.fill((255, 255, 255))if not isdead: # 畫蛇身 / 每一步+1分snake.move()for rect in snake.body:pygame.draw.rect(screen, (144, 238, 144), rect)isdead = snake.is_dead() # 顯示死亡文字if isdead:show_text(screen, (150, 200), '翻車了!', (227, 29, 18), False, 80)show_text(screen, (50, 320), '是男人就超過(guò)60KM/h,按空格鍵重試...', (0, 0, 22))if food.rect == snake.body[0]: # 當(dāng)食物rect與蛇頭重合,吃掉 -> Snake增加一個(gè)Nodescores += 1 # 食物處理 / 吃到+1分food.remove()snake.add_node()food.set() # 食物投遞pygame.draw.rect(screen, (233, 150, 122), food.rect)speed = 10 + scores * 5 if scores else 10 # 蛇移動(dòng)速度# 顯示得分和速度show_text(screen, (20, 550), '關(guān)卡:' + str(scores) + ' 速度:' + str(speed) + 'KM/h', (0, 0, 205))pygame.display.update()clock.tick(speed)main() 在盡可能的保證代碼易用性的情況下,減少代碼行數(shù)! 這只是隨便弄的一個(gè)小玩意,如果真要做游戲還是建議使用游戲引擎。 市面上常見(jiàn)的cocos、unity都是不錯(cuò)的游戲引擎,能夠讓你開(kāi)發(fā)游戲的效率達(dá)到事半功倍的效果! |
|