来一些数额的起头化,蛇用列表表示,凯旋门074网址 ,蛇的头的是列表的最后一项,速度用time.sleep()表示,速度越小,越快

澳门凯旋门游戏网址 1

import time,curses,random,threading

pos = {
    'UP': (-1,0),
    'DOWN':(+1,0),
    'LEFT':(0,-1),
    'RIGHT':(0,+1),
}
current = 'RIGHT'
snake = [[1,1],[1,2],[1,3],[1,4],[1,5]]   #ex: [[1,1],[1,2],[1,3]]  [y,x]
head = snake[-1]
snake_speed = 0.1
rany, ranx = -1, -1

def isover(head):
    if snake.count(head) > 1:  #碰到自己身体
        return True
    if (head[0] == y-1) or (head[1] == x-1) or (head[0] == 0) or (head[1] == 0):    #碰到墙
        return True
    return False

def randomyx():
    global rany,ranx
    ranflag = False
    while ranflag is False:
        rany, ranx = random.randint(1,y-2) , random.randint(1,x-2)
        if [rany,ranx] not in snake:
            ranflag = True

def draw():
    stdscr.erase()
    stdscr.border()
    for si in snake:
        stdscr.addstr(si[0],si[1],'@',curses.color_pair(2))
    stdscr.addstr(rany,ranx,' ',curses.color_pair(1))
    stdscr.refresh()

def listening(head):
    global current
    while True:
        c = stdscr.getch()
        if c == curses.KEY_UP and current != 'DOWN':
            current = 'UP'
        elif c == curses.KEY_DOWN and current != 'UP':
            current = 'DOWN'
        elif c == curses.KEY_LEFT and current != 'RIGHT':
            current = 'LEFT'
        elif c == curses.KEY_RIGHT and current != 'LEFT':
            current = 'RIGHT'

def loop(stdscr):
    global head,snake_speed
    randomyx()    
    t1 = threading.Thread(target=listening, args=(head,), name='listenkeyboard')
    t1.start()
    while isover(head) is False:
        draw()
        head = [ head[0] + pos[current][0] , head[1] + pos[current][1]] 
        snake.append(head)
        if head[0] == rany and head[1] == ranx:
            randomyx()
            snake_speed = snake_speed - 0.01
        else:
            snake.pop(0)
        time.sleep(snake_speed)

stdscr = curses.initscr()
curses.noecho()     #不输出- -
curses.cbreak()     #立刻读取:暂不清楚- -
curses.start_color()  #开启颜色
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_RED)#定义点的颜色
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLUE) #定义蛇的颜色
#init_pair(n,f,b)修改颜色对n,让f为前景色,b为背景色。颜色对0天生的黑白色,不允许改。
stdscr.keypad(1)    #开启keypad
stdscr.border()
curses.curs_set(0)
(y,x) = stdscr.getmaxyx()
curses.wrapper(loop)
curses.endwin()

澳门凯旋门游戏网址澳门凯旋门注册网址 ,2.然后思量current <– 当前蛇走的势头,怎么样去改造

def isover(head):
    if snake.count(head) > 1:  #碰到自己身体
        return True
    if (head[0] == y-1) or (head[1] == x-1) or (head[0] == 0) or (head[1] == 0):    #碰到墙
        return True
    return False

随意变化贪吃蛇的点,注意:生成的点:需在框内而且不能够在蛇的身上

那样,三个贪吃蛇小游戏就完结了,下边是落到实处的万事代码,python3
snake.py,自个儿玩一下吧~~

地点是贯彻的截图,废话不说,直接起始说一下代码

上面是主旨的逻辑代码,主要考虑是:

此间首先就思虑是一个异步的景观!利用异步监听鼠标时间蛇的头的是列表的末段一项。!

def randomyx():
    ranflag = False
    while ranflag is False:
        rany, ranx = random.randint(1,y-2) , random.randint(1,x-2)
        if [rany,ranx] not in snake:
            ranflag = True
    while isover(head) is False:
        draw()
        head = [ head[0] + pos[current][0] , head[1] + pos[current][1]] 
        snake.append(head)
        if head[0] == rany and head[1] == ranx:
            randomyx()
            snake_speed = snake_speed - 0.01
        else:
            snake.pop(0)
        time.sleep(snake_speed)

蛇的头的是列表的末段一项。此间必要留意的是:    在日前大势为上的时候,不能够按下—> 
按了蛇不就本身撞本身了嘛蛇的头的是列表的末段一项。~

蛇的头的是列表的末段一项。1.只要蛇未有吃到东西,就头进入进来,尾掉(变成蛇走步的感到);只要蛇吃到了东西,就头加进去,尾不出去(产生蛇长大的痛感) 
<—  标准队列

认清游戏是不是甘休,遭逢自身身体停止可能境遇墙甘休

curses.start_color()  #开启颜色
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_RED)#定义点的颜色
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLUE) #定义蛇的颜色

写完重要逻辑,之后  美化以下界面->

pos = {
    'UP': (-1,0),
    'DOWN':(+1,0),
    'LEFT':(0,-1),
    'RIGHT':(0,+1),
}
current = 'RIGHT'
snake = [[1,1],[1,2],[1,3],[1,4],[1,5]]   #ex: [[1,1],[1,2],[1,3]]  [y,x]
head = snake[-1]
snake_speed = 0.1
rany, ranx = -1, -1

 

    t1 = threading.Thread(target=listening, args=(head,), name='listenkeyboard')
    t1.start()

def listening(head):
    while True:
        c = stdscr.getch()
        if c == curses.KEY_UP and current != 'DOWN':
            current = 'UP'
        elif c == curses.KEY_DOWN and current != 'UP':
            current = 'DOWN'
        elif c == curses.KEY_LEFT and current != 'RIGHT':
            current = 'LEFT'
        elif c == curses.KEY_RIGHT and current != 'LEFT':
            current = 'RIGHT'

for si in snake:
  stdscr.addstr(si[0],si[1],’@’,curses.color_pair(2))
  stdscr.addstr(rany,ranx,’ ‘,curses.color_pair(1))

相关文章