Advertisement
alexs77

Bubble Blaster.py

Oct 16th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. # Bubble Blaster
  2.  
  3. from Tkinter import *
  4. from random import randint
  5. from time import sleep, time
  6. from math import sqrt
  7.  
  8. HEIGHT = 500
  9. WIDTH = 800
  10.  
  11. SHIP_R = 15
  12. MID_X = WIDTH / 2
  13. MID_Y = HEIGHT / 2
  14.  
  15. SHIP_SPD = 10
  16.  
  17. bub_id = list()
  18. bub_r = list()
  19. bub_speed = list()
  20. MIN_BUB_R = 10
  21. MAX_BUB_R = 30
  22. MAX_BUB_SPD = 110
  23. GAP = 100
  24.  
  25. BUB_CHANCE = 10
  26. TIME_LIMIT = 30
  27. BONUS_SCORE = 1000
  28.  
  29. score = 0
  30. bonus = 0
  31. end = time() + TIME_LIMIT
  32.  
  33. def schiff_beweg(event):
  34.     if event.keysym == 'Up':
  35.         c.move(ship_id, 0,-SHIP_SPD)
  36.         c.move(ship_id2, 0, -SHIP_SPD)
  37.     elif event.keysym == 'Down':
  38.         c.move(ship_id, 0, SHIP_SPD)
  39.         c.move(ship_id2, 0, SHIP_SPD)
  40.     elif event.keysym == 'Left':
  41.         c.move(ship_id, 0, -SHIP_SPD)
  42.         c.move(ship_id2, 0, -SHIP_SPD)
  43.     elif event.keysym == 'Right':
  44.         c.move(ship_id, 0, SHIP_SPD)
  45.         c.move(ship_id2, 0, SHIP_SPD)
  46.  
  47. def erstelle_bubble():
  48.     x = WIDTH + GAP
  49.     y = randint(0, HEIGHT)
  50.     r = randint(MIN_BUB_R, MAX_BUB_R)
  51.     id1 = c.create_oval(x - r, y - r, x + r, y+r, outline='white')
  52.     bub_id.append(id1)
  53.     bub_r.append(r)
  54.     bub_speed.append(randint(1, MAX_BUB_SPD))
  55.  
  56. def bewege_bubbles():
  57.     for i in range(len(bub_id)):
  58.         c.move(bub_id[i], -bub_speed[i], 0)
  59.  
  60. def hole_koord(id_num):
  61.     pos = c.coords(id_num)
  62.     x = (pos[0] + pos[2])/2
  63.     y = (pos[1] + pos[3])/2
  64.     return x, y
  65.  
  66. def loesche_bubble(i):
  67.     del bub_r[i]
  68.     del bub_speed[i]
  69.     c.delete(bub_id[i])
  70.     del bub_id[i]
  71.  
  72. def entf_bubbles():
  73.     for i in range(len(bub_id)-1, -1, -1):
  74.         x, y = hole_koord(bub_id[i])
  75.         if x < -GAP:
  76.            loesche_bubble(i)
  77.  
  78. def distanz(id1, id2):
  79.     x1, y1 = hole_koord(id1)
  80.     x2, y2 = hole_koord(id2)
  81.     return sqrt ((x2 - x1)**2 + (y2 - y1)**2)
  82.  
  83. def kollision():
  84.     points = 0
  85.     for bub in range(len(bub_id)-1, -1, -1):
  86.         if distanz(ship_id2, bub_id[bub]) < (SHIP_R + bub_r[bub]):
  87.             points += (bub_r[bub] + bub_speed[bub])
  88.             loesche_buble(bub)
  89.     return points
  90.  
  91. def zeige_punkte(score):
  92.     c.itemconfig(score_text, text=str(score))
  93.  
  94. def zeige_zeit(time_left):
  95.     c.itemconfig(time_text, text=str(time_left))
  96.  
  97. window = Tk()
  98. window.title('Bubble Blaster')
  99. c = Canvas(window, width=WIDTH, height=HEIGHT, bg='darkblue')
  100. c.pack()
  101. ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill='red')
  102. ship_id2 = c.create_oval(0, 0, 30, 30, outline='green')
  103.  
  104. c.bind_all('<Key>', schiff_beweg)
  105.  
  106. c.move(ship_id, MID_X, MID_Y)
  107. c.move(ship_id2, MID_X, MID_Y)
  108.  
  109. c.create_text(50, 30, text='ZEIT', fill='white')
  110. c.create_text(150, 30, text='PUNKTE', fill='white')
  111. time_text = c.create_text(50, 50, fill='white')
  112. score_text = c.create_text(150, 50, fill='white')
  113.  
  114. #HAUPTSCHLEIFE
  115. while time() < end:
  116.     if randint(1, BUB_CHANCE) == 1:
  117.         erstelle_bubble()
  118.     bewege_bubbles
  119.     entf_bubbles()
  120.     score += kollision()
  121.     if (int(score / BONUS_SCORE)) > bonus:
  122.         bonus += 1
  123.         end += TIME_LIMIT
  124.     zeige_punkte(score)
  125.     zeige_zeit(int(end - time()))
  126.     window.update()
  127.     sleep(0.01)
  128.  
  129. c.create_text(MID_X, MID_Y, \
  130.              text='GAME OVER', fill='white', font=('Helvtica' ,30))
  131. c.create_text(MID_X, MID_Y, + 30, \
  132.              text='Punkte: '+ str(score), fill=white)
  133. c.create_text(MID_X, MID_Y, + 40, \
  134.              text='Bonus-Zeit: '+ str(bonus*TIME_LIMIT), fill='white')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement