Code: Alles auswählen
#BubbleBlaster
from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('Bubble Blaster')
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
c.pack()
schiff_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
schiff_id2 = c.create_oval(0, 0, 30, 30, outline='red')
SCHIFF_R = 15
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(schiff_id, MID_X, MID_Y)
c.move(schiff_id2, MID_X, MID_Y)
SCHIFF_GESCHW = 10
#SCHIFF
def schiff_beweg(event):
if event.keysym == 'Up':
c.move(schiff_id, 0, -SCHIFF_GESCHW)
c.move(schiff_id2, 0, -SCHIFF_GESCHW)
elif event.keysym == 'Down':
c.move(schiff_id, 0, SCHIFF_GESCHW)
c.move(schiff_id2, 0, SCHIFF_GESCHW)
elif event.keysym == 'Left':
c.move(schiff_id, -SCHIFF_GESCHW, 0)
c.move(schiff_id2, -SCHIFF_GESCHW, 0)
elif event.keysym == 'Right':
c.move(schiff_id, SCHIFF_GESCHW, 0)
c.move(schiff_id2, SCHIFF_GESCHW, 0)
c.bind_all('<Key>', schiff_beweg)
#BUBBLES (normal)
from random import randint
bub_id = list()
bub_r = list()
bub_geschw = list()
MIN_BUB_R = 10
MAX_BUB_R = 30
MAX_BUB_GESCHW = 10
GAP = 100
def erstelle_bubble():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_BUB_R, MAX_BUB_R)
id1 = c.create_oval(x - r,y - r,x + r,y + r, outline='white')
bub_id.append(id1)
bub_r.append(r)
bub_geschw.append(randint(1, MAX_BUB_GESCHW))
def bewege_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], -bub_geschw[i], 0)
def hole_koord(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def lösche_bubble(i):
del bub_r[i]
del bub_geschw[i]
c.delete(bub_id[i])
del bub_id[i]
def entf_bubbles():
for i in range(len(bub_id)-1, -1, -1):
x, y = hole_koord(bub_id[i])
if x < -GAP:
lösche_bubble(i)
#FASTBUBBLES
from random import randint
fastbub_id3 = list()
fastbub_r = list()
fastbub_geschw = list()
MIN_FASTBUB_R = 10
MAX_FASTBUB_R = 30
MAX_FASTBUB_GESCHW = 10
GAP = 100
def erstelle_fastbubbles():
x = WIDTH + GAP
y = randint(0, HEIGHT)
r = randint(MIN_FASTBUB_R, MAX_FASTBUB_R)
id3 = c.create_oval(x - r,y - r,x + r,y + r, outline='green')
fastbub_id3.append(id3)
fastbub_r.append(r)
fastbub_geschw.append(randint(1, MAX_FASTBUB_GESCHW))
def bewege_fastbubbles():
for i in range(len(fastbub_id3)):
c.move(fastbub_id3[i], -fastbub_geschw[i], 0)
def hole_fastkoord(id3_num):
pos = c.coords(id3_num)
x = (pos[0] + pos[2])/2
y = (pos[1] + pos[3])/2
return x, y
def lösche_fastbubbles(i):
del fastbub_r[i]
del fastbub_geschw[i]
c.delete(fastbub_id3[i])
del fastbub_id3[i]
def entf_fastbubbles():
for i in range(len(fastbub_id3)-1, -1, -1):
x, y = hole_fastkoord(fastbub_id3[i])
if x < -GAP:
lösche_fastbubbles(i)
#BUBBLES
from math import sqrt
def distanz(id1, id2):
x1, y1 = hole_koord(id1)
x2, y2 = hole_koord(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
def kollision():
points = 0
for bub in range(len(bub_id)-1, -1, -1):
if distanz(schiff_id2, bub_id[bub]) < (SCHIFF_R + bub_r[bub]):
points += (bub_r[bub] + bub_geschw[bub])
lösche_bubble(bub)
return points
c.create_text(50, 30, text='ZEIT', fill='white' )
c.create_text(150, 30, text='PUNKTE', fill='white' )
time_text = c.create_text(50, 50, fill='white' )
score_text = c.create_text(150, 50, fill='white' )
def zeige_punkte(score):
c.itemconfig(score_text, text=str(score))
def zeige_zeit(time_left):
c.itemconfig(time_text, text=str(time_left))
def distanz(id3, id2):
x1, y1 = hole_fastkoord(id3)
x2, y2 = hole_fastkoord(id2)
return sqrt((x2 - x1)**2 + (y2 - y1)**2)
#FASTBUBBLES
def fastcounter():
SCHIFF_GESCHW = 15
fastboost_text = c.create_text(250, 30, text='FASTBOOST', fill='white' )
from time import sleep, time
fastnum_text10 = c.create_text(250, 50, text='10', fill='white' )
sleep(1)
c.itemconfig(fastnum_text10, fill='darkblue')
fastnum_text9 = c.create_text(250, 50, text='9', fill='white' )
sleep(1)
c.itemconfig(fastnum_text9, fill='darkblue')
fastnum_text8 = c.create_text(250, 50, text='8', fill='white' )
sleep(1)
c.itemconfig(fastnum_text8, fill='darkblue')
fastnum_text7 = c.create_text(250, 50, text='7', fill='white' )
sleep(1)
c.itemconfig(fastnum_text7, fill='darkblue')
fastnum_text6 = c.create_text(250, 50, text='6', fill='white' )
sleep(1)
c.itemconfig(fastnum_text6, fill='darkblue')
fastnum_text5 = c.create_text(250, 50, text='5', fill='white' )
sleep(1)
c.itemconfig(fastnum_text5, fill='darkblue')
fastnum_text4 = c.create_text(250, 50, text='4', fill='white' )
sleep(1)
c.itemconfig(fastnum_text4, fill='darkblue')
fastnum_text3 = c.create_text(250, 50, text='3', fill='white' )
sleep(1)
c.itemconfig(fastnum_text3, fill='darkblue')
fastnum_text2 = c.create_text(250, 50, text='2', fill='white' )
sleep(1)
c.itemconfig(fastnum_text2, fill='darkblue')
fastnum_text1 = c.create_text(250, 50, text='1', fill='white' )
sleep(1)
c.itemconfig(fastnum_text1, fill='darkblue')
fastnum_text0 = c.create_text(250, 50, text='0', fill='white' )
sleep(1)
c.itemconfig(fastnum_text0, fill='darkblue')
c.itemconfig(fastboost_text, fill='darkblue')
SCHIFF_GESCHW = 10
def fastkollision():
for bubi in range(len(fastbub_id3)-1, -1, -1):
if distanz(schiff_id2, fastbub_id3[bubi]) < (SCHIFF_R + fastbub_r[bubi]):
lösche_fastbubbles(bubi)
fastcounter()
if fastcounter():
for bubi in range(len(fastbub_id3)-1, -1, -1):
if distanz(schiff_id2, fastbub_id3[bubi]) < (SCHIFF_R + fastbub_r[bubi]):
lösche_fastbubbles(bubi)
score += 20
from time import sleep, time
BUB_CHANCE = 10
TIME_LIMIT = 30
BONUS_SCORE = 1000
score = 0
bonus = 0
ende = time() +TIME_LIMIT
#HAUPTSCHLEIFE
while time() < ende:
if randint(1, BUB_CHANCE) == 1:
erstelle_bubble()
if randint(1, 50) == 6:
erstelle_fastbubbles()
bewege_bubbles()
bewege_fastbubbles()
entf_bubbles()
entf_fastbubbles()
fastkollision()
score += kollision()
if (int(score / BONUS_SCORE)) > bonus:
bonus += 1
ende += TIME_LIMIT
zeige_punkte(score)
zeige_zeit(int(ende - time()))
window.update()
sleep(0.01)
c.create_text(MID_X, MID_Y, \
text='GAME OVER', fill='white', font=('Helvetica',30))
c.create_text(MID_X, MID_Y + 30, \
text='Punkte: '+ str(score), fill='white')
c.create_text(MID_X, MID_Y + 45, \
text='Bonus-Zeit: '+str(bonus*TIME_LIMIT), fill='white')
Also ... es ist ein Spiel bei dem man mit einem U-Boot Blasen zerplatzen lassen muss, ich habe es aus einem Lehrbuch abgeschrieben und soweit alles ganz gut verstanden. Dann hab ich mich daran versucht, eine andere Art von Blasen hinzuzufügen, die Fastbubbles, sie sollen bei Kontakt mit dem Boot verschwinden und dann das Boot schneller machen. Dazu hab ich einen Counter erstellt, der den Boost anzeigt (wahrscheinlich unnötig kompliziert, aber hey). Das Problem ist, dass das Boot in 10 Sekunden mehr als eine von den speziellen Blasen trifft - das müsste dann ja eigentlich zu einem Fehler führen und deshalb suche ich einen Code, der erkennt ob der Counter läuft und wenn ja, für eine zusätzliche Fastbubble +20 Punkte gibt.
Allerdings bin ich mir nicht ganz sicher, ob das der Grundfehler im Programm ist, aber ich werd's dann ja sehen
.