Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SpaceWar by S Rogers / Python 3.6 & @TokyoEdTech
- import turtle as t
- import os
- import random
- #reqiured by mac os to show objects
- t.fd(0)
- #set animation speed to maximum
- t.speed(0)
- t.bgcolor("black")
- t.ht()
- #saves memory
- t.setundobuffer(1)
- #this speeds up drawing
- t.tracer(3)
- class Sprite(t.Turtle):
- def __init__(self, spriteshape, color, startx, starty):
- t.Turtle.__init__(self, shape = spriteshape)
- self.speed(0)
- self.penup()
- self.color(color)
- self.fd(0)
- self.goto(startx, starty)
- self.speed = 1
- def move(self):
- self.fd(self.speed)
- #Boundary detection
- if self.xcor() > 290:
- self.rt(60)
- self.setx(290)
- if self.xcor() < -290:
- self.rt(60)
- self.setx(-290)
- if self.ycor() > 290:
- self.sety(290)
- self.rt(60)
- if self.ycor() < -290:
- self.sety(-290)
- self.rt(60)
- def is_collision(self, other):
- if (self.xcor()>= (other.xcor() -20)) and\
- (self.xcor()<= (other.xcor() + 20)) and\
- (self.ycor()>= (other.ycor() -20)) and\
- (self.ycor()<= (other.ycor() +20)):
- return True
- else:
- return False
- #creating a player sprite using the Sprite Class
- class Player(Sprite):
- def __init__(self, spriteshape, color, startx, starty):
- Sprite.__init__(self,spriteshape, color, startx, starty)
- self.speed = 4
- self.lives = 3
- self.ammo = 3
- def turn_left(self):
- self.lt(45)
- def turn_rt(self):
- self.rt(45)
- def accelerate(self):
- self.speed += 1
- def decelerate(self):
- self.speed -= 1
- #Method that controls the player shooting
- def shoot(self):
- #Selection statement that only allows the player to shoot if they have ammo
- if self.ammo > 0:
- # Use the index of the missiles list to fire the missile
- missiles[self.ammo].fire()
- self.ammo-=1
- print(self.ammo)
- #statement that moves the ammo drop onto the screen when ammo is zero
- if self.ammo == 0:
- x = random.randint(-250,250)
- y = random.randint(-250,250)
- ammoDrop.goto(x,y)
- self.ammo = -1
- print(self.ammo)
- # def ammo(self):
- # self.ammo
- #Creating an enemy using the Sprite Superclass
- class Enemy(Sprite):
- def __init__(self, spriteshape, color, startx, starty):
- Sprite.__init__(self,spriteshape, color, startx, starty)
- self.speed = 6
- self.setheading(random.randint(0,360))
- #Creating an ally into the game
- class Ally(Sprite):
- def __init__(self, spriteshape, color, startx, starty):
- Sprite.__init__(self,spriteshape, color, startx, starty)
- self.speed = 6
- self.setheading(random.randint(0,360))
- def move(self):
- self.fd(self.speed)
- #Boundary detection
- if self.xcor() > 290:
- self.lt(60)
- self.setx(290)
- if self.xcor() < -290:
- self.lt(60)
- self.setx(-290)
- if self.ycor() > 290:
- self.sety(290)
- self.lt(60)
- if self.ycor() < -290:
- self.sety(-290)
- self.lt(60)
- class Missile(Sprite):
- def __init__(self, spriteshape, color, startx, starty):
- Sprite.__init__(self,spriteshape, color, startx, starty)
- self.shapesize(stretch_wid = 0.3, stretch_len = 0.4, outline = None)
- self.speed = 20
- self.status = "ready"
- self.goto(-1000,1000)
- def fire(self):
- if self.status == "ready":
- self.goto(player.xcor(), player.ycor())
- self.setheading(player.heading())
- self.status = "firing"
- def move(self):
- if self.status == "ready":
- self.goto(-1000,1000)
- if self.status == "firing":
- self.fd(self.speed)
- if self.xcor() > 290 or \
- self.xcor() < -290 or \
- self.ycor() > 290 or \
- self.ycor() < -290:
- self.status = "ready"
- self.goto(-1000,1000)
- #Creates an ammo drop sprite to re-fill player ammo
- class AmmoDrop(Sprite):
- def __init__(self, spriteshape, color, startx, starty):
- Sprite.__init__(self,spriteshape, color, startx, starty)
- self.shapesize(stretch_wid = 1.5, stretch_len = 1.5, outline = None)
- self.speed = 0
- class Game():
- def __init__(self):
- self.level = 1
- self.score = 0
- self.state = "playing"
- self.pen = t.Turtle()
- self.lives = 3
- def draw_border(self):
- #Draw Border
- self.pen.speed(0)
- self.pen.color("white")
- self.pen.pensize(3)
- self.pen.penup()
- self.pen.goto(-300, 300)
- self.pen.pendown()
- for side in range(4):
- self.pen.fd(600)
- self.pen.rt(90)
- self.pen.penup()
- self.pen.ht()
- self.pen.pendown()
- def show_score(self):
- self.pen.undo()
- msg = "Score: %s" %(int(self.score))
- self.pen.penup()
- self.pen.goto(-300,310)
- self.pen.write(msg, font=("Arial", 16, "normal"))
- def show_ammo(self):
- self.pen.penup()
- ammo = "Ammo: %s" %(Player.ammo)
- self.pen.goto(-300,330)
- self.pen.write(ammo, font=("Arial", 16, "normal"))
- game = Game()
- game.draw_border()
- game.show_score()
- #game.show_ammo()
- #create sprites
- player = Player("triangle", "white", 0,0)
- # Create missile instances
- missiles = []
- for _ in range(6):
- missiles.append(Missile("triangle", "yellow", 1000, 1000))
- ammoDrop = AmmoDrop("square","green",-1000,1000)
- #Keyboard Bindings
- t.onkey(player.turn_left,"Left")
- t.onkey(player.turn_rt,"Right")
- t.onkey(player.accelerate,"Up")
- t.onkey(player.decelerate,"Down")
- t.onkey(player.shoot,"space")
- t.listen()
- #main game loop
- x = random.randint(-250,250)
- y = random.randint(-250,250)
- enemies = []
- for i in range(10):
- enemies.append(Enemy("circle", "red", x, y))
- allies = []
- for i in range(10):
- allies.append(Ally("square", "blue", x, y))
- while True:
- t.update()
- player.move()
- # Multiple missiles
- for missile in missiles:
- missile.move()
- for enemy in enemies:
- enemy.move()
- #check collision player to ememy
- if player.is_collision(enemy):
- x = random.randint(-250,250)
- y = random.randint(-250,250)
- enemy.goto(x,y)
- game.score -=100
- game.show_score()
- #game.show_ammo()
- #check collision missile to ememy
- # Note, we now have multiple missiles so we need to iterate through the loop
- for missile in missiles:
- if missile.is_collision(enemy):
- x = random.randint(-250,250)
- y = random.randint(-250,250)
- enemy.goto(x,y)
- #stops missile continuing across the screenafter hitting enemy
- missile.goto(-1000,1000)
- missile.status = "ready"
- game.score +=100
- game.show_score()
- #game.show_ammo()
- for ally in allies:
- ally.move()
- #check collision missile to ally
- #Note: Multiple missiles so we need to iterate
- for missile in missiles:
- if missile.is_collision(ally):
- x = random.randint(-250,250)
- y = random.randint(-250,250)
- ally.goto(x,y)
- game.score -= 100
- game.show_score()
- #game.show_ammo()
- #collision detection for ammo drop that re-fills player ammo and moves\
- #ammo drop sprite off screen
- if player.is_collision(ammoDrop):
- player.ammo = 5
- ammoDrop.goto(-1000,1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement