import icons
import random

class Target:
	def __init__(self):
		size = random.randint(8, 16)
		self.image = icons.Angry(height=size, width=size)
		self.image.height = size
		self.image.width  = size
		self.image.color = random.choice(["lightgreen", "deepskyblue", "gold",
                                          "plum", "fuchsia", "darkorange"])
		self.image.x = random.randint(size,60)
		self.image.y = random.randint(size,70)
		self.dx = random.randint(1,5)
        
	def target_enlarge(self):
		self.image.height = 100
		self.image.width =  100
		self.dx = 0
        
	def target_right(self):
		self.image.x = self.image.x + self.dx
    
	def target_position(self):
		return (self.image.x - (self.image.width  // 2),		# left
				self.image.x + (self.image.width  // 2),		# right
				self.image.y - (self.image.height // 2),		# top
				self.image.y + (self.image.height // 2))		# bottom 
        
class Ammo:
	def __init__(self, x, y):
		self.image = icons.CaretUp(color='red')
		size = 6
		self.image.height = size
		self.image.width = size
		self.image.x = x
		self.image.y = y - self.image.height // 2
       
	def ammo_off_screen(self):
		return self.image.y < 0
        
	def ammo_up(self, dy):
		self.image.y = self.image.y + dy 
        
	def ammo_position(self):
		return (self.image.x - (self.image.width  // 2),		# left
				self.image.x + (self.image.width  // 2),		# right
				self.image.y - (self.image.height // 2),		# top
				self.image.y + (self.image.height // 2)) 		# bottom           
class Tank:    
    def __init__(self):
		self.base = Rectangle(color='blue')
		self.top =  Rectangle(color='blue')
		self.base.width = 16
		self.base.height = 6
		self.top.width = 10
		self.top.height = 4
		self.base.x = -self.base.width //2
		self.base.y = 100 - self.base.height // 2
		self.top.x = self.base.x
		self.top.y = self.base.y - (self.base.height + self.top.height)//2
                
    def tank_right(self, dx):
		self.base.x = self.base.x + dx
		self.top.x = self.top.x + dx
        
    def get_x_location(self):
        return self.base.x
    
    def get_tank_height(self):
        return self.base.height + self.top.height
    
def any_hits():
	for a in ammos:
		(aLeft, aRight, aTop, aBtm) = a.ammo_position()
		for t in targets:
			(tLeft, tRight, tTop, tBtm) = t.target_position()
			xRange = range(tLeft, tRight+1)
			yRange = range(tTop, tBtm+1)
			if  (aLeft in xRange and aTop in yRange) or \
				(aLeft in xRange and aBtm in yRange) or \
				(aRight in xRange and aTop in yRange) or \
				(aRight in xRange and aBtm in yRange):
					t.target_enlarge()

Rectangle(color='mintcream')
targets = [Target(), Target(), Target(), Target(), Target()]
tank = Tank()
ammos = []
shots = [4 * random.randint(5, 11), 4 * random.randint(12, 22) ]
dur = random.uniform(.4, .8)

for i in range(50):
    with animation(duration=dur):        
		any_hits()
        
		for a in ammos:
			a.ammo_up(-8) 
            
		tank.tank_right(4)
		
		for t in targets:
			t.target_right()

		for s in shots:
			if tank.get_x_location() == s:
				Text("")	# causes delay that makes ammo appear correctly
				ammos.append(Ammo(s, 100 - tank.get_tank_height()))

		# The random number sequence appears to be the same for each cyle.
		# Refreshing the page resets the random number sequence so that
		# a new scenerio can be displayed instead of repeating the old one
		# over and over and over.
		if len(ammos) == len(shots):
			showMsg = True
			for a in ammos:
				if not a.ammo_off_screen():
					showMsg = False
			if showMsg:
				Text('Refresh page for new "game"', font_size=6, color='indigo')

Shooting Gallery

by carl

Created 3 months ago.