Cool_Breeze 发表于 2020-12-18 20:17:54

python 简易贪吃蛇

本帖最后由 Cool_Breeze 于 2020-12-21 08:36 编辑

# coding=utf-8
# author Cool_Breeze

import ctypes
import msvcrt
from time import sleep
from os import system
from random import randrange


# 位置信息结构体
class point(ctypes.Structure):
    _fields_ = [('x', ctypes.c_short),
                ('y', ctypes.c_short)]

def gotoXYPrint(coord, char):
    global MAINHANDLE
    global HEADICON
    global FOODICON
    ctypes.windll.kernel32.SetConsoleCursorPosition(
      MAINHANDLE, coord)
    if char == HEADICON:
      ctypes.windll.kernel32.SetConsoleTextAttribute(
      MAINHANDLE, 14)
    elif char == FOODICON:
      ctypes.windll.kernel32.SetConsoleTextAttribute(
      MAINHANDLE, 12)
    else:
      ctypes.windll.kernel32.SetConsoleTextAttribute(
      MAINHANDLE, 11)
    print(char, end='', flush=True)


# 边框
def side():
    for n in range(WIDTH-1):
      gotoXYPrint(point(n,0), '+')
      gotoXYPrint(point(n,HIGHT-1), '+')
    for n in range(HIGHT-1):
      gotoXYPrint(point(0,n), '+')
      gotoXYPrint(point(WIDTH-1,n), '+')

def createFood():
    global SNAKE
    global FOODPOINT
    off = False
    while True:
      x = randrange(1, WIDTH-1)
      y = randrange(1, HIGHT-1)
      for n in SNAKE:
            if n.x == x and n.y == y:
                continue
            else:
                FOODPOINT.x = x
                FOODPOINT.y = y
                gotoXYPrint(FOODPOINT, FOODICON)
                off = True
      if off: break

def createSnake():
    global SNAKE
    x, y = WIDTH//2, HIGHT//2
    for n in range(3):
      t = point(x+n, y)
      SNAKE.append(t)
      gotoXYPrint(t, HEADICON)

def update():
    for i in SNAKE:
      gotoXYPrint(i, HEADICON)

def _exit(info):
    input(info)
    exit()
   
def collision():
    global SNAKE
    head = SNAKE
    count = 0
    for n in SNAKE:
      count += 1
      if count == 1: continue
      if n.x == head.x and n.y == head.y:
            _exit('游戏结束!')
            
    if head.x == 0 or head.y == 0 or \
       head.x == WIDTH-1 or head.y == HIGHT-1:
       _exit('游戏结束!')

def moveSnake():
    '''
    K == ←
    M == →
    H == ↑
    P == ↓
    '''
    global DIRECTION
    global SNAKE
    global FOODPOINT
   
    if DIRECTION == 'K':
      SNAKE.insert(0, point(SNAKE.x-1, SNAKE.y))
    elif DIRECTION == 'M':
      SNAKE.insert(0, point(SNAKE.x+1, SNAKE.y))
    elif DIRECTION == 'H':
      SNAKE.insert(0, point(SNAKE.x, SNAKE.y-1))
    elif DIRECTION == 'P':
      SNAKE.insert(0, point(SNAKE.x, SNAKE.y+1))
    # 其他按键不做任何动作
    else: return None
    collision()
    # 是否吃到食物
    if SNAKE.x != FOODPOINT.x or SNAKE.y != FOODPOINT.y:
      gotoXYPrint(SNAKE.pop(), ' ')
    else:
      createFood()
   
    update()
   
HIGHT = 26
WIDTH = 60
MAINHANDLE = ctypes.windll.kernel32.GetStdHandle(-11)
SNAKE = []
HEADICON = 'O'
FOODICON = "$"
FOODPOINT = point()

DIRECTION = 'K'

system(f'mode con cols={WIDTH} lines={HIGHT}')
system("title 贪吃蛇游戏")
# 隐藏光标
ctypes.windll.kernel32.SetConsoleCursorInfo(
    MAINHANDLE, ctypes.byref(point(1,0)))

def main():
    global DIRECTION
    side()
    createSnake()
    createFood()
    while True:
      moveSnake()
      sleep(0.1)
      if msvcrt.kbhit():
            inputKey = msvcrt.getwch()
            # 特殊按键
            if inputKey == '\000' or inputKey == '\xe0':
                inputKey = msvcrt.getwch()
                if DIRECTION == 'K' and inputKey == 'M': continue
                elif DIRECTION == 'M' and inputKey == 'K': continue
                elif DIRECTION == 'H' and inputKey == 'P': continue
                elif DIRECTION == 'P' and inputKey == 'H': continue
            DIRECTION = inputKey
            

if __name__ == '__main__':
    main()
换汤不换药!
**** Hidden Message *****

宋宇儿 发表于 2020-12-18 20:51:05

tryhi 发表于 2020-12-20 22:41:50

本帖最后由 tryhi 于 2020-12-20 22:43 编辑

125行
FOODICON = '
报错

Cool_Breeze 发表于 2020-12-21 07:55:49

本帖最后由 Cool_Breeze 于 2020-12-21 08:38 编辑

tryhi 发表于 2020-12-20 22:41
125行
FOODICON = '
报错

好了,排版失误!
不是排版失误, 代码内容包含< '$' > 好像会调用后面的隐藏内容!像是个BUG

hrp 发表于 2020-12-21 08:18:12

优秀

Cool_Breeze 发表于 2020-12-21 09:20:43

hrp 发表于 2020-12-21 08:18
优秀

还是python写着舒服!C好久都没有用了!

Cool_Breeze 发表于 2020-12-21 19:01:03

from collections import deque
这里使用比列表快很多

kobefeng 发表于 2021-1-2 15:29:20

666

闪闪呀ya 发表于 2021-3-30 11:20:12

nbbnb

cong_zai 发表于 2021-3-30 21:22:28

666 大神

hellogoodbye 发表于 2021-5-17 15:46:00

1

a1503818992 发表于 2021-5-17 20:56:26

厉害的

龙舞九天 发表于 2021-5-18 05:52:02

{:5_95:}

tyh1995 发表于 2021-5-18 09:27:11

查看

lishudong 发表于 2022-2-16 15:43:19

学习一下下

sdjnszqst 发表于 2022-2-16 16:07:30

来学习一下

syjk 发表于 2022-7-28 09:04:52

1

jsdbb007 发表于 2023-5-2 14:05:52

1

andy大宝 发表于 2023-5-3 15:59:17

{:10_257:}

敬老院的大帅比 发表于 2023-5-15 19:45:03

1
页: [1]
查看完整版本: python 简易贪吃蛇