Python Forum
Python 25 Line Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 25 Line Challenge
#31
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()
Reply
#32
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")
    
Reply
#33
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)
Reply
#34
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. Tongue 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
Reply
#35
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 3,264 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Zen Python Challenge ichabod801 3 4,107 Aug-13-2018, 12:02 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020