Advertisement
tokyoedtech

Space War with Multiple Missiles

Jul 4th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.07 KB | None | 0 0
  1. #SpaceWar by S Rogers / Python 3.6 & @TokyoEdTech
  2.  
  3. import turtle as t
  4. import os
  5. import random
  6.  
  7. #reqiured by mac os to show objects
  8. t.fd(0)
  9. #set animation speed to maximum
  10. t.speed(0)
  11. t.bgcolor("black")
  12. t.ht()
  13. #saves memory
  14. t.setundobuffer(1)
  15. #this speeds up drawing
  16. t.tracer(3)
  17.  
  18. class Sprite(t.Turtle):
  19.     def __init__(self, spriteshape, color, startx, starty):
  20.        t.Turtle.__init__(self,  shape = spriteshape)
  21.        self.speed(0)
  22.        self.penup()
  23.        self.color(color)
  24.        self.fd(0)
  25.        self.goto(startx, starty)
  26.        self.speed = 1
  27.    
  28.     def move(self):
  29.         self.fd(self.speed)
  30.  
  31.    
  32.         #Boundary detection
  33.         if self.xcor() > 290:
  34.             self.rt(60)
  35.             self.setx(290)
  36.         if self.xcor() < -290:
  37.             self.rt(60)
  38.             self.setx(-290)
  39.         if self.ycor() > 290:
  40.             self.sety(290)
  41.             self.rt(60)
  42.         if self.ycor() < -290:
  43.             self.sety(-290)
  44.             self.rt(60)
  45.  
  46.     def is_collision(self, other):
  47.         if (self.xcor()>= (other.xcor() -20)) and\
  48.            (self.xcor()<= (other.xcor() + 20)) and\
  49.            (self.ycor()>= (other.ycor() -20)) and\
  50.            (self.ycor()<= (other.ycor() +20)):
  51.             return True
  52.         else:
  53.             return False
  54.    
  55.    
  56. #creating a player sprite using the Sprite Class
  57. class Player(Sprite):
  58.     def __init__(self, spriteshape, color, startx, starty):
  59.        Sprite.__init__(self,spriteshape, color, startx, starty)
  60.        self.speed = 4
  61.        self.lives = 3
  62.        self.ammo = 3
  63.        
  64.        
  65.     def turn_left(self):
  66.          self.lt(45)
  67.    
  68.     def turn_rt(self):
  69.          self.rt(45)
  70.  
  71.     def accelerate(self):
  72.          self.speed += 1
  73.  
  74.     def decelerate(self):
  75.          self.speed -= 1
  76.     #Method that controls the player shooting
  77.     def shoot(self):
  78.         #Selection statement that only allows the player to shoot if they have ammo
  79.         if self.ammo > 0:
  80.             # Use the index of the missiles list to fire the missile
  81.             missiles[self.ammo].fire()
  82.             self.ammo-=1
  83.             print(self.ammo)
  84.         #statement that moves the ammo drop onto the screen when ammo is zero
  85.         if self.ammo == 0:
  86.             x = random.randint(-250,250)
  87.             y = random.randint(-250,250)
  88.             ammoDrop.goto(x,y)
  89.             self.ammo = -1
  90.             print(self.ammo)
  91.            
  92.     # def ammo(self):
  93.     #     self.ammo
  94.  
  95. #Creating an enemy using the Sprite Superclass
  96. class Enemy(Sprite):
  97.     def __init__(self, spriteshape, color, startx, starty):
  98.        Sprite.__init__(self,spriteshape, color, startx, starty)
  99.        self.speed = 6
  100.        self.setheading(random.randint(0,360))
  101.  
  102. #Creating an ally into the game
  103. class Ally(Sprite):
  104.     def __init__(self, spriteshape, color, startx, starty):
  105.        Sprite.__init__(self,spriteshape, color, startx, starty)
  106.        self.speed = 6
  107.        self.setheading(random.randint(0,360))
  108.  
  109.     def move(self):
  110.         self.fd(self.speed)
  111.  
  112.    
  113.         #Boundary detection
  114.         if self.xcor() > 290:
  115.             self.lt(60)
  116.             self.setx(290)
  117.         if self.xcor() < -290:
  118.             self.lt(60)
  119.             self.setx(-290)
  120.         if self.ycor() > 290:
  121.             self.sety(290)
  122.             self.lt(60)
  123.         if self.ycor() < -290:
  124.             self.sety(-290)
  125.             self.lt(60)
  126.        
  127.  
  128. class Missile(Sprite):
  129.     def __init__(self, spriteshape, color, startx, starty):
  130.        Sprite.__init__(self,spriteshape, color, startx, starty)
  131.        self.shapesize(stretch_wid = 0.3, stretch_len = 0.4, outline = None)
  132.        self.speed = 20
  133.        self.status = "ready"
  134.        self.goto(-1000,1000)
  135.  
  136.     def fire(self):
  137.         if self.status == "ready":
  138.             self.goto(player.xcor(), player.ycor())
  139.             self.setheading(player.heading())
  140.             self.status = "firing"
  141.        
  142.            
  143.     def move(self):
  144.         if self.status == "ready":
  145.             self.goto(-1000,1000)
  146.            
  147.         if self.status == "firing":
  148.             self.fd(self.speed)
  149.            
  150.         if self.xcor() > 290 or \
  151.            self.xcor() < -290 or \
  152.            self.ycor() > 290 or \
  153.            self.ycor() < -290:
  154.             self.status = "ready"
  155.             self.goto(-1000,1000)
  156.  
  157. #Creates an ammo drop sprite to re-fill player ammo
  158. class AmmoDrop(Sprite):
  159.     def __init__(self, spriteshape, color, startx, starty):
  160.        Sprite.__init__(self,spriteshape, color, startx, starty)
  161.        self.shapesize(stretch_wid = 1.5, stretch_len = 1.5, outline = None)
  162.        self.speed = 0
  163.  
  164.    
  165.    
  166. class Game():
  167.     def __init__(self):
  168.         self.level = 1
  169.         self.score = 0
  170.         self.state = "playing"
  171.         self.pen = t.Turtle()
  172.         self.lives = 3
  173.  
  174.     def draw_border(self):
  175.         #Draw Border
  176.         self.pen.speed(0)
  177.         self.pen.color("white")
  178.         self.pen.pensize(3)
  179.         self.pen.penup()
  180.         self.pen.goto(-300, 300)
  181.         self.pen.pendown()
  182.         for side in range(4):
  183.             self.pen.fd(600)
  184.             self.pen.rt(90)
  185.         self.pen.penup()
  186.         self.pen.ht()
  187.         self.pen.pendown()
  188.  
  189.     def show_score(self):
  190.         self.pen.undo()
  191.         msg = "Score: %s" %(int(self.score))
  192.        
  193.         self.pen.penup()
  194.         self.pen.goto(-300,310)
  195.         self.pen.write(msg, font=("Arial", 16, "normal"))
  196.  
  197.     def show_ammo(self):
  198.         self.pen.penup()
  199.         ammo = "Ammo: %s" %(Player.ammo)
  200.         self.pen.goto(-300,330)
  201.         self.pen.write(ammo, font=("Arial", 16, "normal"))
  202.    
  203.  
  204. game = Game()
  205. game.draw_border()
  206. game.show_score()
  207. #game.show_ammo()
  208.  
  209. #create sprites
  210. player = Player("triangle", "white", 0,0)
  211.  
  212. # Create missile instances
  213. missiles = []
  214. for _ in range(6):
  215.     missiles.append(Missile("triangle", "yellow", 1000, 1000))
  216.  
  217. ammoDrop = AmmoDrop("square","green",-1000,1000)
  218.  
  219. #Keyboard Bindings
  220. t.onkey(player.turn_left,"Left")
  221. t.onkey(player.turn_rt,"Right")
  222. t.onkey(player.accelerate,"Up")
  223. t.onkey(player.decelerate,"Down")
  224. t.onkey(player.shoot,"space")
  225. t.listen()
  226. #main game loop
  227.  
  228. x = random.randint(-250,250)
  229. y = random.randint(-250,250)
  230.  
  231. enemies = []
  232. for i in range(10):
  233.     enemies.append(Enemy("circle", "red", x, y))
  234.  
  235. allies = []
  236. for i in range(10):
  237.     allies.append(Ally("square", "blue", x, y))
  238.    
  239. while True:
  240.     t.update()
  241.    
  242.     player.move()
  243.     # Multiple missiles
  244.     for missile in missiles:
  245.         missile.move()
  246.    
  247.    
  248.     for enemy in enemies:
  249.         enemy.move()
  250.         #check collision player to ememy
  251.         if player.is_collision(enemy):
  252.             x = random.randint(-250,250)
  253.             y = random.randint(-250,250)
  254.             enemy.goto(x,y)
  255.             game.score -=100
  256.             game.show_score()
  257.             #game.show_ammo()
  258.  
  259.         #check collision missile to ememy
  260.         # Note, we now have multiple missiles so we need to iterate through the loop
  261.         for missile in missiles:
  262.             if missile.is_collision(enemy):
  263.                 x = random.randint(-250,250)
  264.                 y = random.randint(-250,250)
  265.                 enemy.goto(x,y)
  266.                 #stops missile continuing across the screenafter hitting enemy
  267.                 missile.goto(-1000,1000)
  268.                 missile.status = "ready"
  269.                 game.score +=100
  270.                 game.show_score()
  271.                 #game.show_ammo()
  272.  
  273.     for ally in allies:
  274.         ally.move()
  275.         #check collision missile to ally
  276.         #Note: Multiple missiles so we need to iterate
  277.         for missile in missiles:
  278.             if missile.is_collision(ally):
  279.                 x = random.randint(-250,250)
  280.                 y = random.randint(-250,250)
  281.                 ally.goto(x,y)      
  282.                 game.score -= 100
  283.                 game.show_score()
  284.                 #game.show_ammo()
  285.  
  286.     #collision detection for ammo drop that re-fills player ammo and moves\
  287.             #ammo drop sprite off screen
  288.     if player.is_collision(ammoDrop):
  289.             player.ammo = 5
  290.             ammoDrop.goto(-1000,1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement