import sys
import msvcrt
import threading
import time
import map
import os
import player
global go_to,fresh_time
go_to="s"
fresh_time=0.1
def stop(x,y):
temp=map.map()
if temp.good_game(x,y):
sys.exit(0)
def Auto():
global go_to,fresh_time
MAP=map.map()
Person=player.player()
MAP.set_food()
while True:
MAP.refresh(Person.re_snack())
Person.move(go_to)
stop(Person.body[0][0],Person.body[0][1])
MAP.display(Person)
# print("方向:"+go_to)
time.sleep(fresh_time)
os.system('cls')
def use():
global go_to,fresh_time
while True:
cin=msvcrt.getch()
cin=str(cin)[2:3]
if cin == "w" or cin == "W":
if go_to != "s":
go_to="w"
elif cin == "a" or cin == "A":
if go_to != "d":
go_to="a"
elif cin == "s" or cin == "S":
if go_to != "w":
go_to="s"
elif cin == "d" or cin == "D":
if go_to != "a":
go_to="d"
elif cin == " ":
fresh_time=0.02
time.sleep(0.15)
fresh_time=0.1
if __name__ == '__main__':
auto=threading.Thread(target=Auto)
use=threading.Thread(target=use)
auto.start()
use.start()
import sys
import random
import player
class map:
foods=[[19,50]]
long=30
width=119
all=[]
def __init__(self):
for i in range(self.long):
self.all.append([])
for i in self.all:
for j in range(self.width):
i.append(" ")
def refresh(self,snack):
self.all=[]
for i in range(self.long):
self.all.append([])
for i in self.all:
for j in range(self.width):
i.append(" ")
for body_node in snack:
self.all[body_node[0]][body_node[1]]="*"
if len(self.foods) > 0:
for food in self.foods:
self.all[food[0]][food[1]]="#"
else:
self.set_food()
def display(self,snack):
for i in self.all:
for j in i:
sys.stdout.write(j)
print("")
for food in self.foods:
if snack.eat(food[0],food[1]) == True:
self.foods.remove([food[0],food[1]])
def set_food(self):
for i in range(0,10):
self.foods.append([random.randint(2,self.long-2),random.randint(2,self.width-2)])
pass
def good_game(self,x,y):
if x < 0 or y < 0 or x == self.long or y == self.width:
return True
else:
return False
import time
class player:
long=0
body=[]
def __init__(self):
self.body.append([15,50])
self.body.append([15,51])
self.long=2
def move(self,To_head):
#print(self.body)
if To_head == "w":
if self.body[0][1] == 0:
return
else:
self.body.append([0,0])
for i in range(len(self.body)):
k = len(self.body)-1-i
self.body[k]=self.body[k-1]
self.body[0]=[self.body[1][0]-1,self.body[1][1]]
self.body.pop(self.long)
elif To_head == "a":
if self.body[0][1] == 0:
return
else:
self.body.append([0,0])
for i in range(len(self.body)):
k = len(self.body)-1-i
self.body[k]=self.body[k-1]
self.body[0]=[self.body[1][0],self.body[1][1]-1]
self.body.pop(self.long)
elif To_head == "s":
if self.body[0][1] == 0:
return
else:
self.body.append([0,0])
for i in range(len(self.body)):
k = len(self.body)-1-i
self.body[k]=self.body[k-1]
self.body[0]=[self.body[1][0]+1,self.body[1][1]]
self.body.pop(self.long)
elif To_head == "d":
if self.body[0][1] == 0:
return
else:
self.body.append([0,0])
for i in range(len(self.body)):
k = len(self.body)-1-i
self.body[k]=self.body[k-1]
self.body[0]=[self.body[1][0],self.body[1][1]+1]
self.body.pop(self.long)
def eat(self,x,y):
if self.body[0][0] == x and self.body[0][1] == y:
self.long=self.long+1
self.body.insert(0,[x,y])
return True
else:
return False
def re_snack(self):
return self.body