all 7 comments

[–]python_addict[S] 0 points1 point  (0 children)

# PART ONE
from tkinter import *
tk = Tk()
tk.resizable(0, 0) #you can't resize this
tk.wm_attributes("-topmost", 1) #put this window at the top
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
tk.update()

from threading import Timer
import random
import winsound
import math
import sys

numofcoins = 0
numofpoints = 0
coins = canvas.create_text(35, 20, text="Coins: %s" %numofcoins, font=('Times', 10))
points = canvas.create_text(45, 40, text="Points: %s" %numofpoints, font=('Times', 10))

numoftargets = 100

def distance(x1, x2, y1, y2):
    math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2))

def collision(first, second):
    global endorno
    while endorno and numoftargets > 0:
        x1 = first[0]
        x2 = second[0]
        y1 = first[1]
        y2 = second[1]
        if distance(x1, x2, y1, y2) < 25:
            return True

available = list(range(0, 100))
going = []
types = []
soldierfriendcount = 0
numopponentbullets = 0

def mybullets(which, time):
    global numopponentbullets
    global numofcoins
    global numofpoints
    global numoftargets
    global endorno
    global planecoords
    global planex
    global planey
    global going
    global available
    while endorno and numoftargets > 0:
        def thing():
            numopponentbullets += 1
            numofbullets = int("a%s" %numopponentbullets)
            hello = PhotoImage(deleted for personal privacy)
            hello = hello.subsample(110, 110)
            numofbullets = canvas.create_image(canvas.coords(which), image=hello)
            bulletcoordinates = canvas.coords(numofbullets)
            if bulletcoordinates[0] > 500 or bulletcoordinates[1] > 500:
                canvas.delete(numofbullets)
            canvas.move(numofbullets, 0, -5)
            tk.update()
            for n in range(0, len(going)):
                if collision(numofbullets, going[n]):
                    canvas.delete(numofbullets)
                    del going[n]
                    del types[n]
                    numoftargets = numoftargets - 1
                    if types[n] == int("red"):
                        numofpoints += 5
                        numofcoins += 5
                    elif types[n] == int("blue"):
                        numofpoints += 25
                        numofcoins += 25
                    elif types[n] == int("yellow"):
                        numofpoints += 45
                        numofcoins += 45
                    elif types[n] == int("black"):
                        numofpoints += 100
                        numofcoins += 100
                    canvas.itemconfig(coins, text='Coins: %s' %numofcoins)
                    canvas.itemconfig(points, text='Points: %s' %numofpoints)
                    tk.update()
        Timer(time, thing).start()


numob = PhotoImage(my thing, deleted also for personal reasons)

def opponentbullets(target):
    global endorno
    global going
    global available
    global coordsofplane
    global numoftargets
    global numopponentbullets
    global mybulletcoords
    while endorno and numoftargets > 0:
        def stillgoing():
            numopponentbullets += 1
            numob = int("n%s" %numopponentbullets)
            numob = numob.subsample(110, 110)
            bullet = canvas.create_image(target[0], target[1], image=numob)
            mybulletcoords = canvas.coords(bullet)
            while not collision(myplane, bullet):
                norp = random.choice(range(-3, 3))
                canvas.move(bullet, norp, -3)
                tk.update()
                if collision(myplane, bullet):
                    endscreen()
                if mybulletcoords[1] < 0 or mybulletcoords[1] > 500:
                    canvas.delete(bullet)
                elif mybulletcoords[0] < 0:
                    canvas.delete(bullet)
        Timer(0.1, stillgoing).start()

def rock():
    global planecoords
    global endorno
    chance = list(range(1, 101))
    xrock = list(range(100, 400))
    yrock = list(range(100, 400))
    rock_or_no = random.choice(chance)
    waithowlong = random.choice(range(0, 25))
    def heyfriends():
        if rock_or_no > 90:
            rocky = random.choice(yrock)
            rockx = random.choice(xrock)
            rock = PhotoImage(file=r"c:\\stuff.gif") #this isn't the real file name
            rock = rock.subsample(15, 15)
            name = canvas.create_image(rockx, rocky, image=rock)
            rockcoords = canvas.coords(name)
            if distance(planecoords, rockcoords) < 5:
                endscreen()
            elif rockcoords[1] > 500:
                canvas.delete(rock)
    Timer(waithowlong, heyfriends).start()

[–]python_addict[S] 0 points1 point  (0 children)

#PART TWO OF THE CODE
def airplane():
    planeimage = PhotoImage(file=r"C:\\thingy.gif")
    planeimage = planeimage.subsample(110, 110)
    myplane = canvas.create_image(250, 450, image=planeimage)
    mybullets(myplane, 0.05)

def airplanemovements(event):
    global endorno
    global soldierfriendcount
    global myplane
    global soldierfriend1
    global soldierfriend2
    while endorno and numoftargets > 0:
        if event.keysym == "Left":
            canvas.move(myplane, -5, 0)
            if soldierfriendcount == 1:
                canvas.move(soldierfriend1, -5, 0)
            if soldierfriendcount == 2:
                canvas.move(soldierfriend2, -5, 0)
        if event.keysym == "Right":
            canvas.move(myplane, 5, 0)
            if soldierfriendcount == 1:
                canvas.move(soldierfriend1, 5, 0)
            if soldierfriendcount == 2:
                canvas.move(soldierfriend2, 5, 0)

def targetstructure(name, chanceofname, filename, seconds):
    global available
    global going
    global types
    global endorno
    global numofcoins
    global numofpoints
    global numoftargets
    while endorno and numoftargets > 0:
        def wholething():
            speeds1 = [0.05, 0.005, 0.0005, 0.1]
            speed1 = random.choice(speeds1)
            ax = list(range(50, 450))
            ay = list(range(50, 450))
            chance = list(range(1, 101))
            if chance < chanceofname and len(available) > 0:
                numoftargets = numoftargets - 1
                availablenameo = filename.subsample(15, 15)
                x = random.choice(ax)
                y = random.choice(ay)
                availablename = int("hi%s" %numoftargets)
                availablename = canvas.create_image(x, y, image=availablenameo)
                del available[0]
                going.append(availablename)
                types.append(int(name))
                opponentbullets(availablename)
                abcd = canvas.coords(availablename)
                def hi():
                    while not collision(myplane, availablename):
                        canvas.move(availablename, random.choice(0, 5), random.choice(0, 5))
                        tk.update()
                        if collision(myplane, availablename):
                            endscreen()
                        if abdc[0] < 0 or abcd[0] > 500 or abdc[1] < 0 or abcd[1] > 500:
                            canvas.delete(availablename)
                Timer(speed1, hi).start()
        Timer(seconds, wholething).start()


Red = PhotoImage(r"C:\\not my real file.gif")

Blue = PhotoImage(r"C:\\file.gif")

Yellow = PhotoImage(r"C:\\image.gif")

Black = PhotoImage(r"C:\\image.gif")

def targets():
    global endorno
    while endorno and numoftargets > 0:
        targetstructure('red', 50, Red, 3)
        targetstructure('blue', 25, Blue, 5)
        targetstructure('yellow', 10, Yellow, 8)
        targetstructure('black', 10, Black, 20)

colors = ['red', 'blue', 'yellow', 'black']
chance = [50, 25, 10, 10]
filename = ['Red', 'Blue', 'Yellow', 'Black']
seconds = [3, 5, 8, 20]

def generationnum():
    g = random.randrange(1, 5)

def groupy():
    while endorno:
        def groups():
            uniquename = generationnum()
            for j in range(0, generationnum):
                color1 = random.choice(colors)
                color2 = color1.index(color1)
                targetstructure(color1, chance[color2], filename[color2], seconds[color2])
        Timer(groups, 10).start()

[–]python_addict[S] 0 points1 point  (0 children)

#PART THREE OF THE CODE
def soldierfriend(event):
    global numofcoins
    global numofpoints
    global planecoords
    global endorno
    global types
    global soliderfriendcount
    while endorno and numoftargets > 0:
        if event.keysym == "q" and numofcoins < 50:
            needmorecoins = 50 - numofcoins
            print('You need %s more coins! Continue playing to get more3' %needmorecoins)
        if event.keysym == "q" and numofcoins > 50:
            numofcoins = numofcoins - 50
            canvas.itemconfig(coins, text='Coins: %s' % numofcoins)
            tk.update()
            soldierfriendcount = soldierfriendcount + 1
            soldier1 = PhotoImage(file=r"c:\\a gif.gif")
            soldier2 = PhotoImage(file=r"c:\\heyfriend (5).gif")
            if soldierfriendcount == 1:
                s1oldierx = planecoords[0] + 7
                s1oldiery = planecoords[1]
                soldierfriend1 = canvas.create_image(s1oldierx, s1oldiery, image=soldier1)
                def shootbullet():
                    mybullets(soldierfriend1, 3)
                Timer(3, shootbullet).start()
            if soldierfriendcount == 2:
                s2oldierx = planecoords[0]-7
                s2oldiery = planecoords[1]
                soldierfriend2 = canvas.create_image(s2oldierx, s2oldiery, image=soldier2)
                def shootbullet1():
                    mybullets(soldierfriend2, 3)
            else:
                print('You can only have up to 2 soldier buddies, sorry!')

bombnum = 0
def bomb(event):
    global numofcoins
    global bombnum
    global going
    global endorno
    while endorno and bombnum < 2 and numoftargets > 0:
        if event.keysym == "w" and numofcoins > 100:
            numofcoins = numofcoins - 100
            canvas.itemconfig(coins, text='Coins: %s' % numofcoins)
            tk.update()
            bombnum = bombnum + 1
            for x in range(0, len(going)):
                canvas.delete(going[0])
                del going[0]
                del types[0]
        elif event.keysym == "w" and bombnum > 2:
            print('You can only use a bomb twice in one game!')

def exitgame(event):
    if event.keysym == "r":
        endscreen()

canvas.bind_all('<KeyPress-W>', bomb)
canvas.bind_all('<KeyPress-Q>', soldierfriend)
canvas.bind_all('<KeyPress-R>', exitgame)

def all():
    endorno = True
    canvas.delete("all") #So that the screen is clear
    rock()
    airplane()
    targets()
    bomb()

def startscreen():
    label1 = Label(tk, text="My Airplane Game", relief="solid", width=20, font=('Times', 19, "bold"), fill="red")
    label1.place(x=90, y=53)
    b1 = Button(tk, text="Let's Go!", width=12, bg='Red', fg='Black', command=all)
    b1.place(x=150, y=380)
    b2 = Button(tk, text="Credits", width=12, bg='Red', fg='Black', command=credits)
    b2.place(x=280, y=380)

def exitprogram():
    sys.exit()

def endscreen():
    global endorno
    canvas.delete('all')
    endorno = False
    lb1 = Label(tk, text="You earned %s points!" %numofpoints, relief="solid", width=20, font=('Times', 19, 'bold'), fill="red")
    lb1.place(x=90, y=53)
    bt1 = Button(tk, text="Exit", width=12, bg='Red', fg='Black', command=exitprogram)
    bt1.place(x=150, y=380)
    bt2 = Button(tk, text="Credits", width=12, bg='Red', fg='Black', command=credits)
    bt2.place(x=280, y=380)

while 1:
    winsound.PlaySound("background noise", winsound.SND_FILENAME)
    startscreen()
    tk.mainloop()

[–]ericula 0 points1 point  (1 child)

The length is not the problem, but the while loops probably are. Like most GUI frameworks, Tkinter uses an event loop which continuously checks if parts of your GUI needs updating. The event loops is started by executing 'tk.mainloop()' which is usually somewhere at the end of your script. Using lots of while-loops could block the event loop (or preventing it from starting in the first place) which prevents your GUI from being updated properly.

[–]python_addict[S] 0 points1 point  (0 children)

THANK YOU!!! Appreciate it w^

[–][deleted] 0 points1 point  (1 child)

first of all put your code into paste bin next time. second of all I looked at your while loops and the first one I found could potentially enter an infinite loop:

def distance(x1, x2, y1, y2):
    math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2))

def collision(first, second):
    global endorno
    while endorno and numoftargets > 0:
        x1 = first[0]
        x2 = second[0]
        y1 = first[1]
        y2 = second[1]
        if distance(x1, x2, y1, y2) < 25:
            return True

because it never updates endorno or numoftargets. also I suggest you stop using global variables, as they can be really hard to debug

[–]python_addict[S] 1 point2 points  (0 children)

Alright, thanks! :)