Python简单游戏练习——贪吃蛇BFS

2020.7.21

  1. 参考
    Python简单游戏练习——贪吃蛇

  2. 运行环境
    Win10子系统Ubuntu 18.04 LTS,miniconda+python 3.8.3

  3. 代码

# -*- coding: utf-8 -*-
#!/usr/bin/env python3

import random
import curses
from curses import textpad
import time

class GameSnake(object):
    def __init__(self):
        self.score = 0
        self.ROW = 20
        self.COL = 48

        self.snake = [[int(self.ROW / 2), int(self.COL / 2)], [int(self.ROW / 2), int(self.COL / 2) + 1], [int(self.ROW / 2), int(self.COL / 2) + 2]] # 左是头,右是尾
        self.food = [int(self.ROW / 2) + 2, int(self.COL / 2)]
        self.blank = []
        for row in range(1, self.ROW - 1):
            for col in range(1, self.COL - 1):
                self.blank.append([row, col])
        for node in self.snake:
            self.blank.remove(node)
        self.direction = ord("a")
     
    def MoveSnake(self, nowwin):
        next_head = self.path.pop(0)
        
        if next_head == self.food: # 吃到食物了
            self.score += 1
            nowwin.addstr(0, 2, (" Score: %d " % self.score))
            self.snake.insert(0, next_head)
            self.blank.remove(next_head)
            self.food = random.choice(self.blank)
            nowwin.addstr(self.food[0], self.food[1], "*")
            nowwin.refresh()
            return True
        else:
            tail = self.snake.pop()
            self.blank.append(tail)
            nowwin.addstr(tail[0], tail[1], " ")
            self.snake.insert(0, next_head)
            self.blank.remove(next_head)
            nowwin.addstr(next_head[0], next_head[1], "*")
            nowwin.refresh()
            return False

    def GetPath(self):
        self.path = []
        visited = []
        pre = dict()
        origin_head = self.snake[0][:]
        head_queue = [origin_head] #左是头,右是尾
        snake_queue = [self.snake[:]]

        while head_queue:
            now_len = len(head_queue)
            for i in range(now_len):
                now_head = head_queue.pop(0)
                now_snake = snake_queue.pop(0)
                if now_head == self.food:
                    while now_head != origin_head:
                        self.path.insert(0, now_head)
                        now_head = pre[tuple(now_head)]
                    return True
                if now_head[0] in [0, self.ROW - 1] or now_head[1] in [0, self.COL - 1] or now_head in now_snake[1:]: # 撞墙或撞蛇身了
                    continue

                dx = [0, 0, 1, 1]
                dy = [-1, 1, -1, 1]
                for i in range(4):
                    next_head = now_head[:]
                    next_head[dx[i]] = now_head[dx[i]] + dy[i]
                    if next_head not in visited:
                        visited.append(next_head)
                        pre[tuple(next_head)] = now_head
                        head_queue.append(next_head)
                        next_snake = [next_head] + now_snake[1:-1]
                        snake_queue.append(next_snake)

        return False

    def Play(self, stdscr):
        curses.curs_set(0)
        nowwin = curses.newwin(self.ROW + 3, self.COL + 2, 0, 0)              
        
        nowwin.clear()
        for node in self.snake:
            nowwin.addstr(node[0], node[1], "*")
        nowwin.addstr(self.food[0], self.food[1], "*")
        textpad.rectangle(nowwin, 0, 0, self.ROW - 1, self.COL - 1)
        nowwin.addstr(0, 2, " Score: 0 ")
        nowwin.addstr(self.ROW, 0, " You can press CTRL+Z to exit ")
        nowwin.refresh()

        while self.GetPath():
            while not self.MoveSnake(nowwin):
                time.sleep(0.1)
            
        return self.score


game = GameSnake()
score = curses.wrapper(game.Play)
print("Game Over! Your total score is : %d" % score)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值