无pygame写一个python贪吃蛇
生活随笔
收集整理的這篇文章主要介紹了
无pygame写一个python贪吃蛇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
無pygame寫一個python貪吃蛇
這個貪吃蛇是以pynput來監視鍵盤,os來刷新屏幕,以此實現的貪吃蛇
蛇的結構
貪吃蛇建立在block組成的二維列表里,block包含兩個屬性``,自己的身份和指向
身份包括:草地,蛇身,蛇頭,蛇尾巴,蘋果五種。
指向則是從尾巴一格一格地指向蛇頭。
這樣蛇的移動其實就是丟掉尾巴,頭長長一格。
代碼展示
import os import time import random from pynput.keyboard import Listenerdef bp (s): #用于輸出一個屏幕的內容os.system('cls') #本程序只適用于Windowsif s == 'normal':print('#'*(L+2))for i in range(L//2):print('#',end='')for j in range(L):if Map[i][j].status == 0:print(' ',end='')elif Map[i][j].status == 1:print('*',end='')elif Map[i][j].status == 2:print('O',end='')elif Map[i][j].status == 3:print('@',end='')elif Map[i][j].status == 4:print('$',end='')print('#')print('#'*(L+2))elif s == 'over':print(''' ############################################################## # # # # # # # # # # # ____ _ __ __ _____ _____ _______ ____ # # / ___| / \ | \/ | ____| / _ \ \ / / ____| _ \ # # | | _ / _ \ | |\/| | _| | | | \ \ / /| _| | |_) | # # | |_| |/ ___ \| | | | |___ | |_| |\ V / | |___| _ < # # \____/_/ \_\_| |_|_____| \___/ \_/ |_____|_| \_\ # # # # # ''' +'# 你吃到了'+str(speed-30)+'\t個蘋果,下次加油 #'+ ''' # # # # # # ############################################################## ''')elif s == 'welcome':print(''' ############################################################## # # # # # # # # # # # __ _______ _ ____ ___ __ __ _____ # # \ \ / / ____| | / ___/ _ \| \/ | ____| # # \ \ /\ / /| _| | | | | | | | | |\/| | _| # # \ V V / | |___| |__| |__| |_| | | | | |___ # # \_/\_/ |_____|_____\____\___/|_| |_|_____| # # # # # # # # 游戲中只能通過按wasd移動,按下其他非字母鍵游戲直接等死 # # 游戲難度 a 地獄 s簡單 d普通 w困難 # # # # 按下他們開始游戲 # # # # # ############################################################## ''')def on_press(key):global ditry:di = key.charexcept:return Falseclass block ():def __init__ (self, status, direction):self.status = statusself.chD(direction)def chD (self,b):if b == 'w':self.direction = (-1,0)elif b == 'a':self.direction = (0,-1)elif b == 's':self.direction = (1,0)elif b == 'd':self.direction = (0,1)else:self.direction = -1return 0return 1def gn (a,b):temp = Map[a][b].directionreturn a+temp[0],b+temp[1]di,L,S,Map = 30,30,30,30 if __name__ == '__main__':with Listener(on_press=on_press) as listener:bp('welcome')while not di in {'w','a','s','d'}:time.sleep(0.5)#初始化數據if di == 'w':L,S = 20,2elif di == 'a':L,S = 30,15elif di == 's':L,S = 30,15elif di == 'd':L,S = 20,4speed = 30head = (7,15)tail = (7,12)Map = [[block(0,0) for __ in range(L)] for _ in range(L//2)]print(len(Map),len(Map[0]))Map[7][12] = block(1,'d')Map[7][13] = block(2,'d')Map[7][14] = block(2,'d')Map[7][15] = block(3,'d')apple = Falseos.system('cls')while 1:#apple!while not apple:a = random.randint(0,L//2-1)b = random.randint(0,L-1)if Map[a][b].status == 0:Map[a][b].status = 4apple = 1breaktime.sleep(S/speed)fh = gn(*head)if fh[0]>=L//2 or fh[0]<0 or fh[1]>=L or fh[1]<0 or Map[fh[0]][fh[1]].status in {2,1}:bp('over')listener.join()breakelif Map[fh[0]][fh[1]].status == 4:apple = Falsespeed+=1Map[head[0]][head[1]].status = 2head = fhMap[head[0]][head[1]].status = 3Map[head[0]][head[1]].chD(di)elif Map[fh[0]][fh[1]].status == 0:Map[head[0]][head[1]].status = 2head = fhMap[head[0]][head[1]].status = 3Map[head[0]][head[1]].chD(di)Map[tail[0]][tail[1]].status = 0temp = gn(*tail)Map[tail[0]][tail[1]].direction = 0Map[temp[0]][temp[1]].status = 1tail = tempbp('normal')效果
總結
以上是生活随笔為你收集整理的无pygame写一个python贪吃蛇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vscode:四个乱码问题及解决方法
- 下一篇: FGSM代码理解