Advertisement
here2share

# Tk_Bubble_Blaster.py

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