Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 31 13:38:20 2020
  4.  
  5. @author: ssoni
  6.  
  7. Ha, just had a great bug.   Nothing was turning green after 10 seconds.  
  8. Turns out, the red would get reinfected and reset his timeInfected
  9. back to zero on each collision, while already red.  
  10. So, I turned the elapsed time down to 3, and some circles would heal to green.
  11.  
  12. Another bug was is that once green, there is no immunity yet,
  13. so green turns back to red when it hits another red.
  14. """
  15.  
  16.  
  17. from tkinter import *
  18. import random
  19. import time
  20. import sys
  21.  
  22. # Constants
  23. HEIGHT = 700
  24. WIDTH = 900
  25. TIME_TO_HEAL = 3
  26. SPEED = 10
  27. BALL_SIZE = 10
  28. NUM_BALLS = 100
  29. COLOR_START = 'blue'
  30. COLOR_INFECTED = 'red'
  31. COLOR_HEALED = 'green'
  32.  
  33. class Ball:
  34.    
  35.     # New BALL gnerates a random x,y location for the ball
  36.     # The internal canvas graphics variable is NAMED shape
  37.     # Also, init the x and y move increment (speed) as dx & dy
  38.     def __init__(self, color, size):
  39.         x1 = random.randrange(0,WIDTH)
  40.         y1 = random.randrange(0,HEIGHT)
  41.         self.shape = canvas.create_oval(x1, y1, x1+size, y1+size, fill=color)
  42.         self.dx = random.randrange(-SPEED,SPEED)
  43.         self.dy = random.randrange(-SPEED,SPEED)
  44.         self.infectedTime = 0
  45.         self.infected = False
  46.        
  47.     def checkHealed(self):
  48.         if self.infected == True:
  49.             totalTimeSick = time.time() - self.infectedTime
  50.  
  51.             if (totalTimeSick > TIME_TO_HEAL):
  52.                 canvas.itemconfig(self.shape, fill=COLOR_HEALED)
  53.                 self.infected = False
  54.                 self.infectedTime = 0
  55.        
  56.     def move(self):
  57.        
  58.         #Move the ball (shape variable in the ball instance
  59.         canvas.move(self.shape, self.dx, self.dy)
  60.        
  61.         #get location of the ball
  62.         pos = canvas.coords(self.shape)
  63.        
  64.         #Bounce off the edges of the window
  65.         if pos[3] > HEIGHT or pos[1] < 0:
  66.             self.dy = -self.dy
  67.         if pos[0] < 0 or pos[2] > WIDTH:
  68.             self. dx = -self.dx
  69.        
  70.         for b2 in balls:
  71.  
  72.             #ball can't hit itself, otherwise, you'll collide once in every loop
  73.             if (balls.index(b2) == balls.index(self)):
  74.                 continue
  75.            
  76.             # Check if rectangles are overlapping
  77.             # position = [x1,y1,x2,y2]
  78.             # position = [LEFT, TOP, RIGHT, BOTTOM]
  79.             pos2 = canvas.coords(b2.shape)
  80.             if (pos[2] > pos2[0]) and (pos[0] < pos2[2]) and \
  81.                 (pos[3] > pos2[1]) and (pos[1] < pos2[3])                                                                                    :
  82.                     self.dx = -self.dx
  83.                     self.dy = -self.dy
  84.                    
  85.                     # I should really check that if EITHER is red,
  86.                     # then they both become red.  Not just one direction.
  87.                     # You can't get infected if HEALED or already SICK
  88.                     # So, you can only get infected is color= START color
  89.                     if (canvas.itemcget(self.shape,"fill") == COLOR_START):
  90.                         if (canvas.itemcget(b2.shape,"fill") == COLOR_INFECTED):      
  91.                             canvas.itemconfig(self.shape, fill=COLOR_INFECTED)
  92.                             self.infected = True
  93.                             self.infectedTime = time.time()
  94.  
  95. # Check if there are no more infected balls in the list
  96. # Assume eradicated unless proved otherwise
  97. def checkEradicated(balls):
  98.     eradicated = True
  99.     for b in balls:
  100.         if (b.infected == True):
  101.             eradicated = False
  102.     return eradicated
  103.                
  104.  
  105. # Initialize the drawing graphics canvas
  106. tk = Tk()
  107. canvas = Canvas(tk, width=WIDTH, height=HEIGHT)
  108. tk.title("Virus!")
  109. canvas.pack()  
  110.  
  111. timeStart = time.time()
  112.  
  113. # Declare a list of balls                                
  114. balls = []
  115.  
  116. # Create balls and add to the list
  117. for i in range(NUM_BALLS):
  118.     b = Ball(COLOR_START, BALL_SIZE)
  119.     balls.append(b)
  120.    
  121. # Make the first ball infected
  122. canvas.itemconfig(balls[0].shape, fill=COLOR_INFECTED)
  123. balls[0].infected = True
  124. balls[0].infectedTime = time.time()
  125.  
  126. # Main loop to animate.  
  127. # Iterate thru ball list
  128. while True:
  129.     for ball in balls:
  130.         ball.checkHealed()
  131.         ball.move()
  132.     tk.update()
  133.     #time.sleep(.05)  
  134.    
  135.     if (checkEradicated(balls) == True):
  136.         elapsedTime = time.time() - timeStart
  137.         print("Elapsed time to eradicate virus: {0}".format(elapsedTime))
  138.         #break
  139.         #sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement