Advertisement
Guest User

Code for Game

a guest
Nov 2nd, 2018
2,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.72 KB | None | 0 0
  1. #Vooperia Invader
  2. #Original Code by Christian Thompson
  3. #Edits by Eric Pham
  4.  
  5. import turtle
  6. import os
  7. import math
  8. import random
  9. import tkinter as tk
  10.  
  11. n=-200
  12. p=-200
  13. g=200
  14. r=200
  15. j=200
  16. d=0
  17.  
  18. a=0
  19.  
  20. #Set up the screen
  21. wn = turtle.Screen()
  22. wn.bgcolor("black")
  23. wn.title("Vooperia Invaders")
  24. wn.bgpic("VooperiaBackground.png")
  25.  
  26. #Play Music
  27. os.system("afplay Vooperian_Anthem.mp3&")
  28.  
  29. #Register the shapes
  30. turtle.register_shape("Yam.gif")
  31. turtle.register_shape("SpikeDab.gif")
  32. turtle.register_shape("potato.gif")
  33.  
  34. #Set the score to 0
  35. score = 0
  36.  
  37. #Draw the score
  38. score_pen = turtle.Turtle()
  39. score_pen.speed(0)
  40. score_pen.color("white")
  41. score_pen.penup()
  42. score_pen.setposition(-290, 280)
  43. scorestring = "Score: %s" %score
  44. score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  45. score_pen.hideturtle()
  46.  
  47. #Create the player turtle
  48. player = turtle.Turtle()
  49. player.color("blue")
  50. player.shape("SpikeDab.gif")
  51. player.penup()
  52. player.speed(0)
  53. player.setposition(0, -250)
  54. player.setheading(90)
  55.  
  56. playerspeed = 15
  57.  
  58. #Choose a number of enemies
  59. number_of_enemies = 23
  60. #Create an empty list of enemies
  61. enemies = []
  62.  
  63. #Add enemies to list
  64. for i in range(number_of_enemies):
  65.     #Create the enemy
  66.     enemies.append(turtle.Turtle())
  67.  
  68. for enemy in enemies:
  69.     enemy.color("red")
  70.     enemy.shape("Yam.gif")
  71.     enemy.penup()
  72.     enemy.speed(0)
  73.     d= d+1
  74.     if d % 6==0:
  75.         n=p
  76.         j= r-(10*d)
  77.         g=j
  78.         x = n
  79.         y = g
  80.         enemy.setposition(x, y)
  81.     else:
  82.         x = n
  83.         y = g
  84.     enemy.setposition(x, y)
  85.     n= n+80
  86.    
  87.    
  88. enemyspeed = 2
  89.  
  90.  
  91.  
  92. #Create the player's bullet
  93. bullet = turtle.Turtle()
  94. bullet.color("yellow")
  95. bullet.shape("potato.gif")
  96. bullet.penup()
  97. bullet.speed(0)
  98. bullet.setheading(90)
  99. bullet.shapesize(0.5, 0.5)
  100. bullet.hideturtle()
  101.  
  102. bulletspeed = 25
  103.  
  104. #Define bullet state
  105. #ready - ready to fire
  106. #fire - bullet is firing
  107. bulletstate = "ready"
  108.  
  109. #Move the player left and right
  110. def move_left():
  111.     x = player.xcor()
  112.     x -= playerspeed
  113.     if x < -280:
  114.         x = - 280
  115.     player.setx(x)
  116.    
  117. def move_right():
  118.     x = player.xcor()
  119.     x += playerspeed
  120.     if x > 280:
  121.         x = 280
  122.     player.setx(x)
  123.    
  124. def fire_bullet():
  125.     #Declare bulletstate as a global if it needs changed
  126.     global bulletstate
  127.     if bulletstate == "ready":
  128.         os.system("afplay laser.wav&")
  129.         bulletstate = "fire"
  130.         #Move the bullet to the just above the player
  131.         x = player.xcor()
  132.         y = player.ycor() +10
  133.         bullet.setposition(x, y)
  134.         bullet.showturtle()
  135.        
  136. def isCollision(t1, t2):
  137.     distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
  138.     if distance < 25:
  139.             return True
  140.     else:
  141.             return False           
  142.  
  143. def mute():
  144.     os.system("killall afplay")
  145.  
  146. #Create keyboard bindings
  147. turtle.listen()
  148. turtle.onkey(move_left, "Left")
  149. turtle.onkey(move_right, "Right")
  150. turtle.onkey(fire_bullet, "space")
  151.  
  152. while True:
  153.     m = input("\n")
  154.     if (m =='enter'):
  155.         a=1+a
  156.     else:
  157.         continue
  158. #Main game loop
  159. while True:
  160.     char = input("\n")
  161.     if a==0:
  162.         break
  163.     else:
  164.         continue
  165.    
  166.     for enemy in enemies:
  167.         #Move the enemy
  168.         x = enemy.xcor()
  169.         x += enemyspeed
  170.         enemy.setx(x)
  171.    
  172.         #Move the enemy back and down
  173.         if enemy.xcor() > 280:
  174.             for e in enemies:
  175.                 y = e.ycor()
  176.                 y -= 40
  177.                 e.sety(y)
  178.             enemyspeed *= -1
  179.        
  180.         if enemy.xcor() < -280:
  181.             for e in enemies:
  182.                 y = e.ycor()
  183.                 y -= 40
  184.                 e.sety(y)
  185.             enemyspeed *= -1    
  186.            
  187.         #Check for a collision between the bullet and the enemy
  188.         if isCollision(bullet, enemy):
  189.             os.system("afplay WilhelmScream.wav&")
  190.             #Reset the bullet
  191.             bullet.hideturtle()
  192.             bulletstate = "ready"
  193.             bullet.setposition(0, -400)
  194.             #Reset the enemy
  195.             enemies.remove(enemy)
  196.             enemy.hideturtle()
  197.             #Update the score
  198.             score += 10
  199.             scorestring = "Score: %s" %score
  200.             score_pen.clear()
  201.             score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
  202.            
  203.        
  204.         if isCollision(player, enemy):
  205.             os.system("afplay OOF.wav&")
  206.             player.hideturtle()
  207.             enemy.hideturtle()
  208.             print("Game Over")
  209.             score_pen.setposition(-300, 0)
  210.             scorestring = "YOU LOSE"
  211.             os.system("killall afplay")
  212.             score_pen.write(scorestring, False, align="left", font=("Arial", 100, "normal"))
  213.             score_pen.hideturtle()
  214.             turtle.done()
  215.             break
  216.        
  217.         if score == 230:
  218.             print("YOU WON!")
  219.             score_pen.setposition(-300, 0)
  220.             scorestring = "YOU WON!"
  221.             score_pen.write(scorestring, False, align="left", font=("Arial", 100, "normal"))
  222.             score_pen.hideturtle()
  223.             os.system("killall afplay")
  224.             turtle.done()
  225.             break
  226.        
  227.         if (char == 'ESC'):
  228.             mute()
  229.            
  230.     #Move the bullet
  231.     if bulletstate == "fire":
  232.         y = bullet.ycor()
  233.         y += bulletspeed
  234.         bullet.sety(y)     
  235.  
  236.     #Check to see if the bullet has gone to the top
  237.     if bullet.ycor() > 275:
  238.         bullet.hideturtle()
  239.         bulletstate = "ready"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement