Question
Problem 3 (50 XP): Modify the Asteroids game so that there are three different asteroids, a large, a medium and a small asteroid, that move
Problem 3 (50 XP): Modify the Asteroids game so that there are three different asteroids, a large, a medium and a small asteroid, that move in random directions at the beginning of the game. Do so by using three distinct images, creating 15 random goals (change maxGoals to 15), and making sure that there are 5 of each small, medium and large size asteroids by using three distinct image sizes. [Hint: Find .gif images files online and register them with the turtle) Problem 4 (50 XP): Modify the Asteroids game so that the game ends after the playes loses 3 lives. Create a lives counter, and substract a life every time the players touches a boundary. # Turtle Graphics Game import turtle import math import random import os # Set up screen wn = turtle.Screen() wn.bgcolor("black") wn.bgpic("bgpic.gif") wn.tracer(3) # Draw border mypen = turtle.Turtle() mypen.color("white") mypen.penup() mypen.setposition(-300, -300) mypen.pendown() mypen.pensize(3) for side in range(4): mypen.forward(600) mypen.left(90) mypen.hideturtle() # mypen can be reused... will be used to draw score on the screen # Create player turtle player = turtle.Turtle() player.color("blue") player.shape("triangle") player.penup() player.speed(0) # Create the score variable score = 0 # Create goals maxGoals = 10 goals = [] for count in range(maxGoals): goals.append(turtle.Turtle()) goals[count].color("red") goals[count].shape("turtle") goals[count].penup() goals[count].speed(0) goals[count].setposition(random.randint(-300, 300), random.randint(-300, 300)) # Set speed variable speed = 1 # Define functions def turnleft(): player.left(30) def turnright(): player.right(30) def increasespeed(): global speed speed += 1 def decreasespeed(): global speed if speed > 1: speed -= 1 def isCollision(t1, t2): d = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2)) if d < 20: return True else: return False # Set keyboard bindings turtle.listen() turtle.onkey(turnleft, "Left") turtle.onkey(turnright, "Right") turtle.onkey(increasespeed, "Up") turtle.onkey(decreasespeed, "Down") while True: player.forward(speed) # Boundary Checking if player.xcor() > 300 or player.xcor() < -300: player.goto(0, 0) #player.right(180) os.system("afplay bounce.mp3&") # Boundary Checking if player.ycor() > 300 or player.ycor() < -300: player.goto(0, 0) #player.right(180) os.system("afplay bounce.mp3&") # Move the goal for count in range(maxGoals): goals[count].forward(3) # Boundary Checking if goals[count].xcor() > 290 or goals[count].xcor() < -290: goals[count].right(180) os.system("afplay bounce.mp3&") # Boundary Checking if goals[count].ycor() > 290 or goals[count].ycor() < -290: goals[count].right(180) os.system("afplay bounce.mp3&") # Collision checking if isCollision(player, goals[count]): goals[count].setposition(random.randint(-300, 300), random.randint(-300, 300)) goals[count].right(random.randint(0, 360)) os.system("afplay collision.mp3&") score += 1 # Draw the score on the screen mypen.undo() mypen.penup() mypen.hideturtle() mypen.setposition(-290, 310) scorestring = "Score: {0}".format(score) mypen.write(scorestring, False, align="left", font=("Arial", 14, "normal")) #delay = input("Press Enter to finish.") Problem 5 (50 XP): Modify the spaceinvaders game so that the LaserCanon() object moves sideways using the arrow keys for left and right, without going past the screen boundaries. Prevent the LaserCanon() object from changing direction. It should only fire a Bomb in a straight vertical direction. Use the up arrow key rathen than the s key to fire a bomb. This modification changes the game dynamic so that it mimic space invaders more accurately. Problem 6 (50 XP): Modify the spaceinvades game so that the LaserCanon() object automatically fires a Bomb onclik, rather than with the use of the s key. The LaserCanon() still changes direction towards the direction of the (x,y) coordinate you click. This modification makes the game easier to play since it does not require the player to press s after changing direction to fire a Bomb. # screen - manage the laser cannon # bounded turtle - superclass for all turtles (drawable objects) on the screen # Bomb, Alien - inherit from bounded turtle # Game engine from turtle import Turtle, mainloop import random import math class LaserCannon(Turtle): def __init__(self, xmin, xmax, ymin, ymax): super().__init__() self.screen = self.getscreen() self.screen.bgcolor("light green") self.screen.setworldcoordinates(xmin, ymin, xmax, ymax) self.screen.onclick(self.aim) #we need to create a method for aim self.screen.onkey(self.shoot, "s") #we need to create a method for shoot self.screen.onkey(self.quit, "q") #we need to create a method for quit def aim(self, x, y): heading = self.towards(x, y) self.setheading(heading) def shoot(self): Bomb(self.heading(), 15) #we do not know at this point what this does, but we know we are creating a Bomb object #first argument is the direction, the second argument is the speed def quit(self): self.screen.bye() class BoundedTurtle(Turtle): #any object bounded by the screen def __init__(self, speed, xmin=-200, xmax=200, ymin=0, ymax=400): #the bounded turtle object has default values super().__init__() self.xmin = xmin self.xmax = xmax self.ymin = ymin self.ymax = ymax self.speed = speed def outOfBounds(self): xpos, ypos = self.position() #tuple indicating the current position of the BoundedTurtle out = False # assume the BoundedTurtle is inside of the bounds if xpos < self.xmin or xpos > self.xmax: out = True if ypos < self.ymin or ypos > self.ymax: out = True return out def move(self): self.forward(self.speed) if self.outOfBounds(): self.remove() #we need a method to remove the object from the screen else: self.getscreen().ontimer(self.move, 200) #this method .ontimer creates a callback to .move in the current instance # every 200 seconds def remove(self): self.hideturtle() class Bomb(BoundedTurtle): def __init__(self, initHeading, speed): super().__init__(speed) self.initHeading = initHeading self.shape("circle") self.color('red', 'red') self.resizemode('user') self.setheading(initHeading) self.up() self.turtlesize(.25) self.getscreen().ontimer(self.move, 100) def distance(self, other): #calculate distance between two BoundedTurtles for collision detection p1 = self.position() p2 = other.position() a = p1[0] - p2[0] b = p1[1] - p2[1] dist = math.sqrt(a**2 + b**2) return dist #overriding superclass move def move(self): exploded = False self.forward(self.speed) #collision detection for i in Alien.getAliens(): # this assumes that Alien in a class that can be iterated and that contains a method # .getAliens if self.distance(i) < 5:# if bomb and alien are within 5 units then the bomb should trigger and explode i.remove() # i is an Alien object, thus Alien should contain a .remove method exploded = True if self.outOfBounds() or exploded: self.remove() else: self.getscreen().ontimer(self.move, 100) class Alien(BoundedTurtle): alienList = [] #class global variable to hold all objects that we identify as "aliens" @staticmethod def getAliens(): return [x for x in Alien.alienList if x.alive] def __init__(self, speed, xmin, xmax, ymin, ymax): super().__init__(speed, xmin, xmax, ymin, ymax) self.getscreen().tracer(0) self.up() if 'PurpleAlien.gif' not in self.getscreen().getshapes(): self.getscreen().addshape("PurpleAlien.gif") #this is needed especially when the game starts self.shape('PurpleAlien.gif') self.goto(random.randint(xmin+1, xmax-1), ymax-20) self.setheading(random.randint(250, 290)) self.getscreen().tracer(1) Alien.alienList = [x for x in Alien.alienList if x.alive] Alien.alienList.append(self) self.alive = True self.getscreen().ontimer(self.move, 200) #rely on the move method from the superclass #override remove method def remove(self): self.alive = False self.hideturtle() #Game engine class AlienInvaders: def __init__(self, xmin, xmax, ymin, ymax): self.xmin = xmin self.xmax = xmax self.ymin = ymin self.ymax = ymax def play(self): self.mainWin = LaserCannon(self.xmin, self.xmax, self.ymin, self.ymax).getscreen() self.mainWin.ontimer(self.addAlien, 1000) # this means we need to code a .addAlien method self.mainWin.listen() #we add this so that all event handlers are running (or threads are running) mainloop() def addAlien(self): if len(Alien.getAliens()) < 7: Alien(10, self.xmin, self.xmax, self.ymin, self.ymax) self.mainWin.ontimer(self.addAlien, 1000) game = AlienInvaders(-200, 200, 0, 400) #matches the default values for the BoundedTurtle object game.play() #game engine at work
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started