Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active March 13, 2024 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/d4b3528abe96fd7233771c21f6c6a7b1 to your computer and use it in GitHub Desktop.
Save horstjens/d4b3528abe96fd7233771c21f6c6a7b1 to your computer and use it in GitHub Desktop.
wiesberggasse
import turtle
import random
breite = 1200
höhe = 800
turtle.setup(breite,höhe)
turtle.speed(0)
stall = [turtle.Turtle(),
turtle.Turtle(),
]
namen = ["Alice",
"Bob",
]
farben = ["red",
"blue",
]
motoren = [(1,2,3,4,5,6),
(1,2,3,4,5,6),
]
# vorbereitung
for nummer,t in enumerate(stall):
#t.speed(0) # schnell
t.penup()
if nummer % 2 == 0:
raufrunter = 1
else:
raufrunter = -1
t.goto(-breite/2+20, (nummer+1) * 50 * raufrunter)
t.shape("turtle")
t.shapesize(5,5)
t.fillcolor(farben[nummer])
t.pencolor(farben[nummer])
t.motor = motoren[nummer]
t.name = namen[nummer]
t.pendown()
t.write(t.name)
# wettrennen
race = True
while race:
for t in stall:
kraft = random.choice(t.motor)
t.forward(kraft)
#t.write(kraft)
if t.xcor() > breite/2:
turtle.write(f"{t.name} wins the race")
race=False
turtle.exitonclick()
Python Workshop in Wiesberggasse
corridor = ".............$.......$.......$........"
corridor = list(corridor)
hero = "@"
hero_x = 0
princess = "P"
princess_x = len(corridor)-1
# main loop
while True:
# graphic engine
for x, char in enumerate(corridor):
tile = char
if x == hero_x:
tile = hero
if x == princess_x:
tile = princess
print(tile, end="")
print() # new line
# user input
command = input("Your command? >>>")
command = command.lower()
if command in ("exit","quit","q"):
break
if command == "a":
hero_x -= 1 # hero_x = hero_x - 1
if command == "d":
hero_x += 1 # hero_x = hero_x + 1
# collision detection
print("Game over")
corridor = ".............$.......$.......$......."
corridor = list(corridor)
hero = "@"
hero_x = 0
hero_gold = 0
princess = "P"
princess_x = len(corridor)-1
message = ""
# main loop
while True:
# graphic engine
for x, char in enumerate(corridor):
tile = char
if x == princess_x:
tile = princess
if x == hero_x:
tile = hero
print(tile, end="")
print() # new line
# user input
if len(message) > 0:
print(message)
message = ""
status = f"gold: {hero_gold}"
command = input(status + " Your command? >>>")
command = command.lower()
dx = 0
if command in ("exit","quit","q"):
break
if command == "a":
dx = -1
if command == "d":
dx = 1
# stay in corridor
if (hero_x + dx) < 0:
dx = 0
message = "end of corridor"
if (hero_x + dx) >= len(corridor):
dx = 0
message = "end of corridor"
# movement
hero_x += dx
# collision detection
if corridor[hero_x] == "$":
hero_gold += 1
message = "you find gold"
corridor[hero_x] = "."
if hero_x == princess_x:
message = "you found the princess"
print("Game over")
import os
corridor = ".+..........$.......$.......$......."
corridor = list(corridor)
hero = "@"
hero_x = 0
hero_gold = 0
hero_hp = 20
boss = "B"
boss_x = 15
boss_hp = 100
princess = "P"
princess_x = len(corridor)-1
message = ""
# main loop
while True:
# clear screen
os.system("cls") # linux: "clear"
# graphic engine
for x, char in enumerate(corridor):
tile = char
if x == princess_x:
tile = princess
if x == hero_x:
tile = hero
if x == boss_x and boss_hp > 0:
tile = boss
print(tile, end="")
print() # new line
# user input
if len(message) > 0:
print(message)
message = ""
status = f"$: {hero_gold} hp: {hero_hp}"
command = input(status + " Your command? >>>")
command = command.lower()
dx = 0
if command in ("exit","quit","q"):
break
if command == "a":
dx = -1
if command == "d":
dx = 1
# stay in corridor
if (hero_x + dx) < 0:
dx = 0
message = "end of corridor"
if (hero_x + dx) >= len(corridor):
dx = 0
message = "end of corridor"
# fight
if boss_hp > 0:
if hero_x + dx == boss_x:
dx = 0
message = "you fight the boss!"
boss_hp -= 4
hero_hp -= 1
if hero_hp <= 0:
print("you die!")
break
elif boss_hp <= 0:
message += " You win!"
else:
message += f"The boss has {boss_hp} hp left"
# movement
hero_x += dx
# collision detection
if corridor[hero_x] == "+":
message = "healing...you are at full health now"
hero_hp = 20
if corridor[hero_x] == "$":
hero_gold += 1
message = "you find gold"
corridor[hero_x] = "."
if hero_x == princess_x:
print("you found the princess")
break
print("Game over")
import os
import time
corridor = ".+..........$.......$.......$......."
corridor = list(corridor)
hero = "@"
hero_x = 0
hero_gold = 0
hero_hp = 20
boss = "B"
boss_x = 15
boss_hp = 100
princess = "P"
princess_x = len(corridor)-1
message = ""
spielstart = time.time()
zeitlimit = 20 # sekunden
# main loop
while (time.time() - spielstart) < zeitlimit:
# clear screen
os.system("cls") # linux: "clear"
# graphic engine
for x, char in enumerate(corridor):
tile = char
if x == princess_x:
tile = princess
if x == hero_x:
tile = hero
if x == boss_x and boss_hp > 0:
tile = boss
print(tile, end="")
print() # new line
# user input
if len(message) > 0:
print(message)
message = ""
spielzeit = time.time() - spielstart
status = f"$: {hero_gold} hp: {hero_hp} zeit übrig: {zeitlimit-spielzeit:.2f}"
command = input(status + " Your command? >>>")
command = command.lower()
dx = 0
if command in ("exit","quit","q"):
break
if command == "a":
dx = -1
if command == "d":
dx = 1
# stay in corridor
if (hero_x + dx) < 0:
dx = 0
message = "end of corridor"
if (hero_x + dx) >= len(corridor):
dx = 0
message = "end of corridor"
# fight
if boss_hp > 0:
if hero_x + dx == boss_x:
dx = 0
message = "you fight the boss!"
boss_hp -= 4
hero_hp -= 1
if hero_hp <= 0:
print("you die!")
break
elif boss_hp <= 0:
message += " You win!"
else:
message += f"The boss has {boss_hp} hp left"
# movement
hero_x += dx
# collision detection
if corridor[hero_x] == "+":
message = "healing...you are at full health now"
hero_hp = 20
if corridor[hero_x] == "$":
hero_gold += 1
message = "you find gold"
corridor[hero_x] = "."
if hero_x == princess_x:
print("you found the princess")
break
print("Game over")
import os
import time
import random
corridor = ".+..........$.......$.......$......."
corridor = list(corridor)
hero = "@"
hero_x = 0
hero_gold = 0
hero_hp = 20
boss = "B"
boss_x = 15
boss_hp = 100
princess = "P"
princess_x = len(corridor)-1
message = ""
spielstart = time.time()
zeitlimit = 200 # sekunden
# main loop
while (time.time() - spielstart) < zeitlimit:
# clear screen
os.system("cls") # linux: "clear"
# graphic engine
for x, char in enumerate(corridor):
tile = char
if x == princess_x:
tile = princess
if x == hero_x:
tile = hero
if x == boss_x and boss_hp > 0:
tile = boss
print(tile, end="")
print() # new line
# user input
if len(message) > 0:
print(message)
message = ""
spielzeit = time.time() - spielstart
status = f"$: {hero_gold} hp: {hero_hp} zeit übrig: {zeitlimit-spielzeit:.2f}"
command = input(status + " Your command? >>>")
command = command.lower()
dx = 0
if command in ("exit","quit","q"):
break
if command == "a":
dx = -1
if command == "d":
dx = 1
# stay in corridor
if (hero_x + dx) < 0:
dx = 0
message = "end of corridor"
if (hero_x + dx) >= len(corridor):
dx = 0
message = "end of corridor"
# fight
if boss_hp > 0:
if hero_x + dx == boss_x:
dx = 0
message = "you fight the boss!"
boss_hp -= 4 # soviel verliert der Boss
hero_hp -= 1 # soviel verliert der Hero
if hero_hp <= 0:
print("you die!")
break
elif boss_hp <= 0:
message += " You win!"
else:
message += f"The boss has {boss_hp} hp left"
# movement
hero_x += dx
# collision detection
if corridor[hero_x] == "+":
message = "healing...you are at full health now"
hero_hp = 20
if corridor[hero_x] == "$":
hero_gold += 1
message = "you find gold"
corridor[hero_x] = "."
if hero_x == princess_x:
print("you found the princess")
break
print("Game over")
import os
import time
import random
level1 = """
#######################################
#@....$.....$...$...............#.§+++#
#...............................#..+++#
#...§.............M.............###=###
#...........######=####...............#
#...........#.........#...............#
#...........#......P..#...............#
#######################################
"""
level = [list(line) for line in level1.splitlines() if len(line) > 0]
# read level
for y,line in enumerate(level):
for x, char in enumerate(line):
if char == "@":
hero_x = x
hero_y = y
level[y][x] = "."
if char == "M":
boss_x = x
boss_y = y
level[y][x] = "."
if char == "P":
princess_x = x
princess_y = y
level[y][x] = "."
hero = "@"
hero_gold = 0
hero_keys = 0
hero_hp = 20
boss = "B"
boss_hp = 100
princess = "P"
message = ""
spielstart = time.time()
zeitlimit = 2000 # sekunden
# main loop
#while (time.time() - spielstart) < zeitlimit:
while hero_hp > 0:
# clear screen
os.system("cls") # linux: "clear"
# graphic engine
for y, line in enumerate(level):
for x, char in enumerate(line):
tile = char
if x == princess_x and y==princess_y:
tile = princess
if x == boss_x and y == boss_y and boss_hp >0:
tile = boss
if x == hero_x and y == hero_y:
tile = hero
print(tile, end="")
print() # new line
# user input
if len(message) > 0:
print(message)
message = ""
spielzeit = time.time() - spielstart
status = f"keys: {hero_keys} $: {hero_gold} hp: {hero_hp}" # zeit übrig: {zeitlimit-spielzeit:.2f}"
command = input(status + " Your command? >>>")
command = command.lower()
dx = 0
dy = 0
if command in ("exit","quit","q"):
break
if command == "a":
dx = -1
if command == "d":
dx = 1
if command == "w":
dy = -1
if command == "s":
dy = 1
# legal move?
target = level[hero_y + dy][hero_x + dx]
if target == "#":
dx, dy = 0, 0
message = "mit dem Kopf durch die Wand?"
hero_hp -= 1
if target == "=":
# door
if hero_keys > 0:
hero_keys -= 1
message = "tür geöffnet, schlüssel verbraucht"
level[hero_y+dy][hero_x+dx]="."
dx, dy = 0,0
else:
message = "tür ist verschlossen, finde schlüssel"
dx, dy = 0,0
# princess?
if (hero_x +dx == princess_x) and (hero_y +dy == princess_y):
print("You found the princess")
break
# fight
if (boss_hp > 0) and (hero_x + dx == boss_x) and (hero_y + dy == boss_y):
dx, dy = 0,0
message = "you fight the boss!"
boss_hp -= 4 # soviel verliert der Boss
hero_hp -= 1 # soviel verliert der Hero
if hero_hp <= 0:
print("you die!")
break
elif boss_hp <= 0:
message += " You win!"
else:
message += f"The boss has {boss_hp} hp left"
# movement
hero_x += dx
hero_y += dy
# collision detection
if level[hero_y][hero_x] == "+":
message = "healing...you are at full health now"
hero_hp = 20
level[hero_y][hero_x] = "."
if level[hero_y][hero_x] == "$":
message = "you find gold!"
hero_gold += 1
level[hero_y][hero_x] = "."
if level[hero_y][hero_x] == "§":
message = "you find a key!"
hero_keys += 1
level[hero_y][hero_x] = "."
print("Game over")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment