Python Forum
Python 25 Line Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 25 Line Challenge
#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


Messages In This Thread
Python 25 Line Challenge - by codingCat - May-02-2022, 01:08 PM
RE: Python 25 Line Challenge - by codingCat - May-02-2022, 01:11 PM
RE: Python 25 Line Challenge - by menator01 - May-02-2022, 08:02 PM
RE: Python 25 Line Challenge - by codingCat - May-03-2022, 11:23 AM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 12:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:52 PM
RE: Python 25 Line Challenge - by menator01 - May-03-2022, 07:49 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 06:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-03-2022, 09:24 PM
RE: Python 25 Line Challenge - by codingCat - May-04-2022, 07:06 PM
RE: Python 25 Line Challenge - by menator01 - May-05-2022, 07:10 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-09-2022, 01:11 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 02:27 PM
RE: Python 25 Line Challenge - by menator01 - May-06-2022, 07:33 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 11:49 AM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 12:17 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:04 PM
RE: Python 25 Line Challenge - by codingCat - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:36 PM
RE: Python 25 Line Challenge - by Coricoco_fr - May-11-2022, 05:34 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:30 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 06:33 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 06:51 PM
RE: Python 25 Line Challenge - by menator01 - May-09-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-09-2022, 07:07 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 01:43 PM
RE: Python 25 Line Challenge - by codingCat - May-10-2022, 02:55 PM
RE: Python 25 Line Challenge - by Gribouillis - May-10-2022, 07:03 PM
RE: Python 25 Line Challenge - by Gribouillis - May-11-2022, 05:51 AM
RE: Python 25 Line Challenge - by codingCat - May-11-2022, 11:33 AM
RE: Python 25 Line Challenge - by codingCat - May-12-2022, 07:10 PM
RE: Python 25 Line Challenge - by codingCat - May-13-2022, 07:00 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 01:50 PM
RE: Python 25 Line Challenge - by codingCat - May-17-2022, 06:24 PM
RE: Python 25 Line Challenge - by codingCat - May-18-2022, 07:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 100 line Challenge codingCat 9 1,438 Jun-20-2022, 07:18 AM
Last Post: Coricoco_fr
  Zen Python Challenge ichabod801 3 3,349 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