hier mal mein code:
Code: Alles auswählen
import msvcrt as n
import random as rand
import time as t
def render (poss,posz,length,width,first=0):
if not first:
#hier soll der untere inhalt der cmd (also das spielfeld) gelöscht werden, damit man es dann neu gezeichnet werden kann, aber das funzt einfach nicht ....
print chr(8)*((length+2)*(width+3)-1)#<-- das hier war meine idee für eine lösung, aber das funzt leider nicht
text=chr(201)+chr(205)*width+chr(187)+"\n"
text+=(chr(186)+"-"*width+chr(186)+"\n")*length
text+=chr(200)+chr(205)*width+chr(188)
for x,y,z in [posz,poss]:
text=text[0:(y+1)*(width+3)+x+1]+z+text[(y+1)*(width+3)+x+2:]
print text
def move (poss,posz,length,width,randmove):
if poss==posz:
return ['won']
if n.kbhit():
eingabe=ord(n.getch())
if eingabe==224:
eingabe=ord(n.getch())
if eingabe==77:#rechts
if poss[0]!=width-1:
poss[0]+=1
elif eingabe==72:#oben
if poss[1]!=0:
poss[1]-=1
elif eingabe==75:#links
if poss[0]!=0:
poss[0]-=1
elif eingabe==80:#unten
if poss[1]!=length-1:
poss[1]+=1
elif eingabe==27:
return [0]
while randmove:
erg=rand.random()
if erg<=0.25:#rechts
if posz[0]!=width-1:
posz[0]+=1
randmove=0
elif erg<=0.5:#oben
if posz[1]!=0:
posz[1]-=1
randmove=0
elif erg<=0.75:#links
if posz[0]!=0:
posz[0]-=1
randmove=0
else:#unten
if posz[1]!=length-1:
posz[1]+=1
randmove=0
return [poss,posz]
def get_start_point(length,width):
return [[width/3,length/3],[(width*2)/3,(length*2)/3]]
speed=1.0/input('Speed (zw. 1 und 10):')
width=input('Breite (zw. 1 und 20):')
length=input('Laenge (zw. 1 und 20):')
pos=get_start_point(length,width)
render(pos[0]+['S'],pos[1]+['X'],length,width,first=1)
start=t.time()
randmove=0
while len(pos)>1:
if start+speed<t.time():
start=t.time()
randmove=1
render(pos[0]+['S'],pos[1]+['X'],length,width)
pos=move(pos[0],pos[1],length,width,randmove)
randmove=0
if pos[0]=='won':
print 'Sie haben das Ziel erwischt! Glueckwunsch!'
n.getch()
mfg und thx im voraus
Edit (Leonidas): Code in Python-Tags gesetzt.