game development
Game Development instructions:
- Modify the Asteroids game so that there are three different asteroid sizes, a large, a medium and a small asteroid. Do so by using three distinct images. Find .gif images files online and register them with the turtle.
- Modify the Asteroid game so that new asteroids appear at time intervals. (see missilecommand game in D2L) (see chapter 13 of the textbook) (see ontimer() in https://docs.python.org/3/library/turtle.html#turtle.ontimer)
- Modify the Asteroids game by adding functionality to shoot bullets at asteroids and completely removing asteroids if shot at. Use the collision mechanism to determine if the bullet collides with the asteroid. (HINT: bullets are new turtles as circles that are created and shot in the same direction the player is moving towards.)
- Modify the Asteroids game so that the game ends after the player loses 3 lives, but only if player collides with an asteroid.
- Modify the Asteroids game so that the player bounces off the edges.
Asteroids.py
import turtle, math, random, os
# Set up screen
wn = turtle.Screen()
wn.bgcolor(“black”)
wn.bgpic(“bgpic.gif”)
wn.tracer(5)
# Draw border
mypen = turtle.Turtle()
mypen.hideturtle()
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)
# Create player turtle
player = turtle.Turtle()
player.color(“blue”) # you may use an image to show the player as a ship
player.shape(“triangle”)
player.penup()
player.speed(0)
lives = 3
# Create a score variable
score = 0
# Create Asteroids
maxAsteroids = 10
asteroids = []
for count in range(maxAsteroids):
asteroids.append(turtle.Turtle())
asteroids[count].color(“red”) # can be changed to an image of an asteroid
asteroids[count].penup()
asteroids[count].shape(“circle”)
asteroids[count].setposition(random.randint(-290, 290), random.randint(-290, 290))
asteroids[count].right(random.randint(0, 360))
# Set speed variable
speed = 1
# Define callback functions
def turnLeft():
player.left(30)
def turnRight():
player.right(30)
def increaseSpeed():
global speed
speed += 1
def decreaseSpeed():
global speed
if speed > 0:
speed -= 1
# Collision mechanism
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
# Event Handlers
wn.listen()
wn.onkey(turnLeft, “Left”)
wn.onkey(turnRight, “Right”)
wn.onkey(increaseSpeed, “Up”)
wn.onkey(decreaseSpeed, “Down”)
# Game engine
def printStatus():
global lives
global score
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(-290, 310)
scorestring = “Score: {0} Lives: {1}”.format(score, lives)
mypen.write(scorestring, False, align=”left”, font=(“Arial”, 14, “normal”))
printStatus()
while lives > 0:
player.forward(speed)
# Boundary check
if player.xcor() > 300 or player.xcor() < -300:
player.goto(0, 0)
os.system(“afplay bounce.mp3”)
lives -= 1
printStatus()
if player.ycor() > 300 or player.ycor() < -300:
player.goto(0, 0)
os.system(“afplay bounce.mp3”)
lives -= 1
printStatus()
# move asteroids
for i in range(len(asteroids)):
asteroids[i].forward(3)
# Boundary checking
if asteroids[i].xcor() > 290 or asteroids[i].xcor() < -290:
asteroids[i].right(180)
if asteroids[i].ycor() > 290 or asteroids[i].ycor() < -290:
asteroids[i].right(180)
# Collision checking
if isCollision(player, asteroids[i]):
asteroids[i].setposition(random.randint(-290, 290), random.randint(-290, 290))
asteroids[i].right(random.randint(0, 360))
score += 1
# To draw the score on the screen
printStatus()
wn.exitonclick()