Posts: 23
Threads: 2
Joined: May 2022
To keep the ball rolling, here is my Python interpretation of one of the three programs that got this ball rolling so many years ago. Can anyone get it down to a single line?
import turtle,random
turtle.hideturtle()
turtle.colormode(255)
turtle.tracer(0,0)
turtle.setup(0.75,0.80, starty = 25)
maxx = turtle.screensize()[0] #doesn't match the actually window width for some reason
maxy = turtle.screensize()[1]
while True:
turtle.color((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
turtle.width(random.randint(1,10))
x = random.randint(-maxx,maxx)
y = random.randint(-maxy,maxy)
turtle.goto(x,y)
turtle.update()
Posts: 23
Threads: 2
Joined: May 2022
I'm still looking forward to others contributing to the thread. Until that moment, here is another interpretation of an original entry into that contest. A sub hunt game in 24 lines. I think the next version will need graphics.
import random
print("There is a sub in your patrol area, a 10x10x10 grid numbered 0-9\nFlush it out by launching depth chargers. Bullseye the sub and you")
print("will be the hero of the day. Allow the sub to escape and you will\nswabbing the decks for the rest of your career.")
sub = {"x": random.randint(0,9), "y": random.randint(0,9), "depth": random.randint(0,9)}
charges, sunk = 20, False
print("You have",charges,"charges. Good luck!")
print(sub)
while charges > 0 and not sunk:
chg = {"x": int(input(" x: ")), "y": int(input(" y: ")), "depth": int(input(" depth: "))}
dist = ((sub["x"]-chg["x"])**2 + (sub["y"]-chg["y"])**2 + (sub["depth"]-chg["depth"])**2)**0.5
if chg == sub:
print("\nA massive explosion breaks the surface. Debis of the destroyed sub floats above the bubbles.")
sunk = True
elif dist <= 4:
print("\nThe sub was rocked by the charge before slipping away\nBubbles in the water show the sub was at",sub,"\nwas rocked by the charge before slipping away\n")
sub = {"x": sub["x"] + random.randint(-1,1), "y": sub["y"] + random.randint(-1,1), "depth": sub["depth"] + random.randint(-1,1)}
else:
print("The sub silently waits. You have",charges,"charges remaining.\n")
charges = charges - 1
if sunk == True:
print("You sunk the sub. Lunch with the president and the metal of hour awaits")
else:
print("The sub escaped. Your commander hands you a mop and tells you to get started")
Posts: 23
Threads: 2
Joined: May 2022
May-17-2022, 01:50 PM
(This post was last modified: May-17-2022, 01:50 PM by codingCat.)
This is another classic. From the pages of Creative Computing, and the book I taught myself programming with, Games in BASIC, I present: Sine Wave:
#From the pages of Creative Computing, converted from BASIC to Python
from math import *
msg, s = "g", 1.5
while True:
s = 1.5 if s >= 7.5 else s + 1/6
msg = "Creative" if msg[-1] == "g" else "Computing"
print(" "*int(51 + (50*sin(s))),msg)
Posts: 23
Threads: 2
Joined: May 2022
Mini space invaders
This one went a little long. The main issues is that, leaning into dictionaries, the readability went in the opposite direction as expected and I lost the will to kibitz.  With a redesign for the shapes, and a little bit of thought I'm sure I could get it one down below 50 lines. Can anyone envision a way of dropping it below 25?
import tkinter, time
def onTimer():
global score, lives
shapes["bad"]["x"] = shapes["bad"]["x"] + shapes["bad"]["spdx"]
if shapes["bad"]["x"] >= winWid-badiam or shapes["bad"]["x"] <= 0:
shapes["bad"]["y"] = shapes["bad"]["y"] + 25
shapes["bad"]["spdx"] = -shapes["bad"]["spdx"]
if shapes["bad"]["y"] > winHei - badiam:
lives = lives - 1
window.title("Score = " + str(score) + " -- Lives = " + str(lives))
shapes["bad"]["y"] = 0
if lives == 0: return
if lastkey == "Left": shapes["base"]["spdx"] = -1
if lastkey == "Right": shapes["base"]["spdx"] = 1
if shapes["base"]["x"] > winWid-badiam or shapes["base"]["x"] < 0:
if shapes["base"]["x"] > winWid-badiam: shapes["base"]["x"] = winWid-badiam
if shapes["base"]["x"] < 0: shapes["base"]["x"] = 0
shapes["base"]["spdx"] = 0
shapes["base"]["x"] = shapes["base"]["x"] + shapes["base"]["spdx"]
shapes["shot"]["y"] = shapes["shot"]["y"] + shapes["shot"]["spdy"]
if shapes["shot"]["x"] > shapes["bad"]["x"] and shapes["shot"]["x"] < shapes["bad"]["x"] + badiam and shapes["shot"]["y"] < shapes["bad"]["y"] + badiam:
score = score + 100
shapes["shot"]["y"], shapes["shot"]["spdy"] = winHei,0
shapes["bad"]["x"],shapes["bad"]["y"],shapes["bad"]["spdx"] = badiam,0,abs(shapes["bad"]["spdx"]) + 1
window.title("Score = " + str(score) + " -- Lives = " + str(lives))
if shapes["shot"]["y"]+shotlen < shapes["bad"]["y"]:
shapes["shot"]["y"], shapes["shot"]["spdy"] = winHei,0
canvas.coords(shapes["bad"]["obj"],shapes["bad"]["x"],shapes["bad"]["y"], shapes["bad"]["x"]+badiam,shapes["bad"]["y"]+badiam)
canvas.coords(shapes["base"]["obj"],shapes["base"]["x"],shapes["base"]["y"], shapes["base"]["x"]+badiam,shapes["base"]["y"]+badiam)
canvas.coords(shapes["shot"]["obj"],shapes["shot"]["x"],shapes["shot"]["y"], shapes["shot"]["x"],shapes["shot"]["y"]+shotlen)
timerhandle = window.after(10,onTimer)
def onKeyDown(event):
global lastkey
lastkey = event.keysym
if lastkey == "space":
lastkey, shapes["shot"]["x"], shapes["shot"]["y"], shapes["shot"]["spdy"] = "", shapes["base"]["x"]+badiam/2, winHei-badiam-shotlen, -3
winWid, winHei = 640,480
badiam = 25
shotlen = 10
lastkey = ""
score, lives = 0, 3
window = tkinter.Tk()
canvas = tkinter.Canvas(window, width=winWid, height=winHei, bg="black")
canvas.pack()
canvas.focus_set()
shapes = {}
shapes["bad"] = ({"obj": canvas.create_oval(0,badiam, badiam,badiam+badiam, outline="white", fill="white"), "x":2,"y":badiam, "spdx":2, "spdy":0})
shapes["base"] = ({"obj": canvas.create_rectangle(winWid / 2,winHei - 25, badiam,winHei - 25+badiam, outline="white", fill="white"), "x":winWid / 2,"y":winHei - 25, "spdx":0, "spdy":0})
shapes["shot"] = ({"obj": canvas.create_line(0,winHei, 0,winHei+shotlen, fill="white"), "x":0,"y":winHei, "spdx":0, "spdy":0})
canvas.bind("<KeyPress>", onKeyDown)
timerhandle = window.after(2,onTimer)
window.mainloop()
menator01 likes this post
Posts: 23
Threads: 2
Joined: May 2022
This one is just a little artwork using the turtle, although the idea of double spiral goes back to one of my first Turtle programs. Looking at the results... tkinter and turtle would benefit from some anti-aliasing.
import random,turtle,time
while True:
dist = 2 ** random.randint(0,7)
limit = (360 / 0.15625) / dist
size = 64 / dist
turtle.width(dist / 2)
turtle.penup()
turtle.setheading(90)
turtle.goto(0,0)
turtle.pendown()
turtle.speed("fastest")
turtle.color(["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])])
for i in range(0,int(limit)):
turtle.right(360/(360/10))
turtle.forward(i/size)
turtle.penup()
turtle.goto(0,0 - (35 / (32 / dist)))
turtle.setheading(270)
turtle.color(["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])])
turtle.pendown()
for i in range(0,int(limit)):
turtle.right(360/(360/10))
turtle.forward(i/size)
time.sleep(10)
turtle.clear()
Posts: 1
Threads: 0
Joined: Dec 2024
Here is A* in 25 lines with pygame for the display:
from mazelib import Maze
from mazelib.generate.Prims import Prims
import numpy as np, pygame
from ordered_set import OrderedSet
maze = Maze(); maze.generator = Prims(100, 100); maze.generate()
maze = maze.grid; maze[1][1] = 2
x, y, path, deadends = 1, 1, OrderedSet([(1, 1)]), OrderedSet()
getopts = lambda x, y: [(x, y + 1), (x, y - 1), (x + 1, y), (x - 1, y)]
def astar():
global x, y, path, deadends
opts = [(nx, ny) for nx, ny in getopts(x, y) if maze[nx][ny] != 1 and (nx, ny) not in path | deadends]
if opts: x, y = min(opts, key=lambda p: -(p[0] + p[1])); path.add((x, y))
else: deadends.add((x, y)); path.pop(); x, y = path[-1]
pygame.init()
win = pygame.display.set_mode((700, 350))
colors = np.array([[120, 250, 90], [250, 90, 120], [75, 75, 75], [0, 0, 0], [255, 255, 255]])
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: exit()
astar()
if (x, y) == (199, 199): break
for px, py in path: maze[px][py] = 2
for px, py in deadends: maze[px][py] = 4
win.blit(pygame.transform.scale(pygame.surfarray.make_surface(colors[maze]), (350, 350)), (0, 0))
pygame.display.flip()
Posts: 4
Threads: 0
Joined: Dec 2024
1 import time
2 import random
3
4 def challenge():
5 number_of_needed_numbers = 60
6 count = 0
7
8 while count < number_of_needed_numbers:
9 start_time = time.time() # get first time
10 time.sleep(0.00000000000001) # wait
11 end_time = time.time() # get second time
12 low_time = ((end_time + start_time) / 2) # covert to one time
13 start_time1 = time.time() # get third time
14 time.sleep(0.00000000000001) # wait
15 end_time1 = time.time() # get fourth time
16 high_time = ((end_time1 + start_time1) / 2) # convert to one time
17 random.seed((high_time + low_time) / 2)
18 random_number = random.randint(0, 1)
19 count += 1
20 print(random_number)
21
22
23 if __name__ == '__main__':
24 challenge()
Simple real random number generator. Please read both files, by Bruce first.
|