Code: Alles auswählen
import os
import time
import random
import msvcrt
# play ground
WIDTH = 50
HEIGHT = 20
PLAYER = "U"
BOMB= "*"
EMPTY= "."
FOOD="$"
FREEZ="§"
follow_person="V"
live=2
bombs=[]
player_pos = [HEIGHT - 2, WIDTH // 2]
freeZ=[]
nfood=[]
# Directions (dy, dx)
UP = (-1, 0)
DOWN = (1, 0)
LEFT = (0, -1)
RIGHT = (0, 1)
running =True
def getkey(screen):
key = screen.getch()
if key != -1:
return chr(key)
return None
def game():
global running
global live
global player_pos
global follow_pers_pos2
direction=None
score=0
max_score=5
food=gift()
bombs=bom()
freeZ=frez()
follow_pers_pos2=sec_player()
score_player2=0
max_score_player2=10
screen = Screen()
#curses.curs_set(0)
Screen.keypad(True)
screen.timeout(50)
try:
while True and live>0:
clear_screen()
border(screen)
time.sleep(0.2)
field(screen)
screen.addch(player_pos[0], player_pos[1], PLAYER)
screen.addch(follow_pers_pos2[0], follow_pers_pos2[1], follow_person)
screen.addch(food[0], food[1],FOOD)
# ChatGPt hat geholfen um das food zu generiern bzw. platzieren
screen.addstr(0,25,f"Dein Score: {score}")
screen.addstr(0,10,f"Leben: {live}")
screen.addstr(HEIGHT,0,"Seien Sie schneller als 'Golosino'und sammeln mind. 3$")
for bomb in bombs:
screen.addch(bomb[0],bomb[1],"*")
for fres in freeZ:
screen.addch(fres[0],fres[1],FREEZ)
screen.refresh()
key = getkey(screen)
if key:
if key == "w":
direction = UP
elif key == "s":
direction = DOWN
elif key == "a":
direction = LEFT
elif key == "d":
direction = RIGHT
# Z.87 bis 96 Quelle Herr Prof.Dok.Nierhoff und so geaendert dass es fuers game passt
new_pos = player_pos.copy()
if direction:
new_pos = [player_pos[0] + direction[0],player_pos[1] + direction[1]]
# Z. 99 bis Z. 101 Loesung dieses Problems von ChatGPT
if 0 < new_pos[0] < HEIGHT - 1 and 0 < new_pos[1] < WIDTH - 1:
player_pos = new_pos
for food in nfood:
if food==player_pos:
nfood.remove(food)
score=score+1
food=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
nfood.append(food)
for food in nfood:
dis=[abs(follow_pers_pos2[0]-food[0]),abs(follow_pers_pos2[1]-food[1])]
if dis[0] != 0 or dis[1] != 0:
if follow_pers_pos2[0] < food[0]:
follow_pers_pos2[0] += 1
elif follow_pers_pos2[0] > food[0]:
follow_pers_pos2[0] -= 1
if follow_pers_pos2[1] < food[1]:
follow_pers_pos2[1] +=1
elif follow_pers_pos2[1] > food[1]:
follow_pers_pos2[1] -= 1
dis=[abs(follow_pers_pos2[0]-food[0]),abs(follow_pers_pos2[1]-food[1])]
if dis[0]==0 and dis[1]==0:
nfood.remove(food)
score_player2=score_player2+1
food=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
nfood.append(food)
if score_player2==max_score_player2:
clear_screen()
print(" BOOOOOH Golosino won , you are Bot! ")
break
if score>=3:
screen.timeout(25)
if score == max_score:
break
for nbombs in bombs:
if nbombs ==player_pos:
bombs.remove(nbombs)
live-=1
score-=1
nbombs=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
bombs.append(nbombs)
if score <0:
break
if live==0:
break
for fres in freeZ:
if fres!=(HEIGHT - 2, WIDTH // 2):
if fres ==player_pos:
freeZ.remove(fres)
fres = [random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
freeZ.append(fres)
time.sleep(5)
new_pos2 = [follow_pers_pos2[0],follow_pers_pos2[1]]
if 0 < new_pos2[0] < HEIGHT - 2 and 0 < new_pos2[1] < WIDTH - 2:
follow_pers_pos2 = new_pos2
finally:
clear_screen()
if score<3 and live==0:
print("Game Over!")
elif 3<=score<5 and live==0:
print(f"You won level_1 ! your Score: {score}")
elif score==5:
print(f"You won last-level ! your Score: {score}")
def clear_screen():
os.system('cls')
def get_key():
if msvcrt.kbhit():
return msvcrt.getch().decode("utf-8").lower()
return None
def border(screen):
for x in range(1,WIDTH):
screen.addch(1, x, "#")
screen.addch(HEIGHT - 1, x, "#")
for y in range(1,HEIGHT):
screen.addch(y,0,"#")
screen.addch(y,WIDTH-1,"#")
def field(screen):
for y in range(2,HEIGHT-1):
for x in range(1,WIDTH-1):
screen.addch(y, x, EMPTY)
def gift():
while True:
food=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
nfood.append(food)
if food !=(HEIGHT - 2, WIDTH // 2):
return food
def bom():
while True:
for _ in range(20):
nbombs=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
bombs.append(nbombs)
return bombs
# def bom funktion von ChatGPT
def sec_player():
while True:
follow_pers_pos2=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
if follow_pers_pos2 !=(HEIGHT - 2, WIDTH // 2) and follow_pers_pos2!=bombs:
return follow_pers_pos2
def frez():
while True:
for _ in range(5):
freez=[random.randint(2, HEIGHT - 2), random.randint(2, WIDTH - 2)]
freeZ.append(freez)
return freeZ
class Screen:
def render(self):
# Simuliert das Darstellen auf dem Bildschirm
print("\033[H", end="") # Cursor zurücksetzen (ANSI)
for row in self.buffer:
print("".join(row))
print("\033[H", end="")
if __name__ == "__main__":
game()