Advertisement
Guest User

Virus Ninja Game

a guest
Sep 5th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.70 KB | None | 0 0
  1. import turtle
  2. import random
  3. import time
  4.  
  5. is_paused = False
  6.  
  7. screen = turtle.Screen ()
  8. screen.setup (500, 650)
  9. screen.title ('Virus Ninja Game')
  10. screen.bgpic ('lungs3.gif')
  11. screen.tracer (0)
  12.  
  13. for images in range (4):
  14.     screen.register_shape ('virus' + str (images + 1) + '.gif')
  15.     screen.register_shape ('thermometer' + str (images + 1) + '.gif')
  16.  
  17. screen.register_shape ('knife1.gif')
  18. screen.register_shape ('thermometer5.gif')
  19. screen.register_shape ('thermometer6.gif')
  20. screen.register_shape ('thermometer7.gif')
  21. screen.register_shape ('thermometer8.gif')
  22. screen.register_shape ('first_aid.gif')
  23. screen.register_shape ('capsule.gif')
  24. screen.register_shape ('mask.gif')
  25.  
  26. pen = turtle.Turtle ()
  27. pen.penup ()
  28. pen.hideturtle ()
  29.  
  30. wait = False
  31. virus = []
  32. virus_state = []
  33. knife_animate = []
  34. knife_intensity = 6
  35. score = 0
  36. damage = 0
  37. speed = 3
  38. active_virus_no = 3
  39. booster_activate = [False, False, False]
  40. kill_state = []
  41. kill_time = []
  42. virus_health = []
  43. virus_permeability = []
  44. virus_limit = []
  45. permissible_virus = ['virus1.gif', 'virus2.gif']
  46. allowed_boosters = ['first_aid.gif', 'capsule.gif', 'mask.gif']
  47. boosters = []
  48. current_booster = None
  49. capsule_timer = time.time ()
  50. mask_timer = time.time ()
  51.  
  52. for i in range (20):
  53.     temp = turtle.Turtle ()
  54.     temp.shape (random.choice (permissible_virus))
  55.     virus.append (temp)
  56.     virus_state.append (False)
  57.     virus_health.append (100)
  58.     virus_permeability.append (20)
  59.     virus_limit.append (-500)
  60.     temp.penup ()
  61.     temp.goto (random.randint (-200, 200), 500)
  62.  
  63. for i in range (3):
  64.     temp = turtle.Turtle ()
  65.     temp.shape (allowed_boosters [i])
  66.     temp.penup ()
  67.     temp.sety (500)
  68.     boosters.append (temp)
  69.  
  70. for i in range (knife_intensity):
  71.     temp = turtle.Turtle ()
  72.     temp.shape ('square')
  73.     temp.shapesize (0.2, 0.2, 0.2)
  74.     temp.color ('white')
  75.     temp.penup ()
  76.     knife_animate.append (temp)
  77.  
  78. knife = turtle.Turtle ()
  79. knife.penup ()
  80. knife.shape ('knife1.gif')
  81.  
  82. knife_animate = [knife] + knife_animate
  83.  
  84. knife_prev_pos_x = knife.xcor ()
  85. knife_prev_pos_y = knife.ycor ()
  86. knife_previous_time = time.time ()
  87. knife_velocity_x = 0
  88. knife_velocity_y = 0
  89.  
  90.  
  91. def slash (x, y):
  92.     global current_booster
  93.     if not is_paused:
  94.         knife.goto (x + 25, y + 25)
  95.         for i in boosters:
  96.             if i.distance ((x, y)) <= 20:
  97.                 current_booster = i
  98.                 booster_active (i)
  99.  
  100.  
  101. def slash_animation ():
  102.     global knife_animate, knife_intensity
  103.     for i in range (len (knife_animate) - 1, knife_intensity - 1, -1):
  104.         knife_animate [i].hideturtle ()
  105.     for i in range (knife_intensity - 1, 0, -1):
  106.         if i == 1:
  107.             knife_animate [i].showturtle ()
  108.             knife_animate [i].goto (knife_animate [i - 1].xcor () + 53,
  109.                                     knife_animate [i - 1].ycor () + 43)
  110.         else:
  111.             knife_animate [i].showturtle ()
  112.             knife_animate [i].goto (knife_animate [i - 1].pos ())
  113.  
  114.  
  115. def booster_active (booster):
  116.     booster.sety (500)
  117.     global damage, knife_intensity, wait, capsule_timer, mask_timer, virus_limit
  118.     if booster.shape () == 'first_aid.gif':
  119.         booster_activate [0] = False
  120.         damage -= 5
  121.         if damage < 0:
  122.             damage = 0
  123.     elif booster.shape () == 'capsule.gif':
  124.         booster_activate [1] = False
  125.         capsule_timer = time.time ()
  126.         knife_intensity = 6
  127.     elif booster.shape () == 'mask.gif':
  128.         booster_activate [2] = False
  129.         mask_timer = time.time ()
  130.         wait = True
  131.         for i in range (len (virus_limit)):
  132.             virus_limit [i] = random.randint (-200, 300)
  133.  
  134.  
  135. thermometer = turtle.Turtle ()
  136. thermometer.penup ()
  137. thermometer.setpos (200, -170)
  138. thermometer.shape ('thermometer1.gif')
  139. thermometer.hideturtle ()
  140.  
  141.  
  142. def main_game ():
  143.     screen.onclick (slash)
  144.     knife.ondrag (knife.goto)
  145.  
  146.     screen.bgpic ('lungs3.gif')
  147.  
  148.     global knife_animate, knife_intensity, knife_velocity_y, knife_velocity_x
  149.     global wait, damage, score, mask_timer, capsule_timer, knife_previous_time
  150.     global knife_prev_pos_x, knife_prev_pos_y, active_virus_no, permissible_virus
  151.     global virus_limit, virus, virus_health, virus_state, virus_permeability
  152.     global booster_activate, boosters, current_booster, allowed_boosters
  153.     global speed
  154.  
  155.     permissible_virus = ['virus1.gif', 'virus2.gif']
  156.  
  157.     thermometer.showturtle ()
  158.     knife.showturtle ()
  159.     knife.setpos (0, 0)
  160.  
  161.     active_virus_no = 3
  162.  
  163.     for i in range (len (virus)):
  164.         virus [i].showturtle ()
  165.         virus [i].sety (random.randint (500, 300 + active_virus_no * 100))
  166.         virus_state [i] = False
  167.  
  168.     for j in range (len (boosters)):
  169.         boosters [j].showturtle ()
  170.         boosters [j].sety (random.randint (500, 300 + active_virus_no * 100))
  171.         booster_activate [j] = False
  172.  
  173.     pen.clear ()
  174.     pen.hideturtle ()
  175.     pen.goto (80, 250)
  176.  
  177.     knife_intensity = 4
  178.     damage = 0
  179.     score = 0
  180.     speed = 3
  181.     wait = False
  182.  
  183.     while True:
  184.         pen.pendown ()
  185.         pen.clear ()
  186.         pen.pencolor ('white')
  187.         pen.write ('Score : ' + str (score), font = ('Comic Sans Ms', 20, 'normal'))
  188.         pen.penup ()
  189.  
  190.         if time.time () - capsule_timer >= 10:
  191.             knife_intensity = 4
  192.         if time.time () - mask_timer >= 5:
  193.             wait = False
  194.  
  195.         thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
  196.  
  197.         for i in range (len (virus)):
  198.             if virus [i].shape () == 'virus1.gif':
  199.                 virus_permeability [i] = 10
  200.             elif virus [i].shape () == 'virus2.gif':
  201.                 virus_permeability [i] = 10
  202.             elif virus [i].shape () == 'virus3.gif':
  203.                 virus_permeability [i] = 3
  204.             elif virus [i].shape () == 'virus4.gif':
  205.                 virus_permeability [i] = 2
  206.  
  207.         if time.time () - knife_previous_time >= 0.2:
  208.             knife_velocity_x = ((knife.xcor () - knife_prev_pos_x) / (0.2)) / 10
  209.             knife_velocity_y = ((knife.ycor () - knife_prev_pos_y) / (0.2)) / 10
  210.             knife_previous_time = time.time ()
  211.             knife_prev_pos_x = knife.xcor ()
  212.             knife_prev_pos_y = knife.ycor ()
  213.         if (abs (knife_velocity_x) > 50 and abs (knife_velocity_x) < 150) or (abs (
  214.             knife_velocity_y) > 50 and abs (knife_velocity_y) < 150):
  215.             for i in range (len (virus)):
  216.                 if virus [i].distance (knife.pos ()) < 100:
  217.                     virus_health [i] -= knife_intensity * virus_permeability [i]
  218.                 if virus_health [i] <= 0:
  219.                     virus_health [i] = 100
  220.                     virus [i].sety (random.randint (500, 300 + 100 * (active_virus_no)))
  221.                     score += 1
  222.                     if score % 10 == 0:
  223.                         if speed <= 9:
  224.                             speed += 0.2
  225.                     if score % 10 == 0:
  226.                         active_virus_no += 1
  227.                         if score % 5 * (active_virus_no - 2) == 0:
  228.                             booster_activate [random.randint (0, 2)] = True
  229.                         if active_virus_no >= 19:
  230.                             active_virus_no = 20
  231.                     if score % 15 == 0:
  232.                         permissible_virus.append ('virus3.gif')
  233.                     if score % 25 == 0:
  234.                         permissible_virus.append ('virus4.gif')
  235.                     virus [i].setx (random.randint (-150, 150))
  236.                     virus [i].shape (random.choice (permissible_virus))
  237.         slash_animation ()
  238.         for i in virus:
  239.             if i.ycor () <= -500:
  240.                 i.sety (random.randint (500, 300 + active_virus_no * 100))
  241.                 if damage <= 6:
  242.                     damage += 1
  243.                     thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
  244.                 if damage >= 7:
  245.                     thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
  246.                     screen.update ()
  247.                     end_screen ()
  248.  
  249.         for i in range (active_virus_no):
  250.             virus_state [i] = True
  251.  
  252.         for i in range (len (virus)):
  253.             if virus_state [i]:
  254.                 if not (wait and virus [i].ycor () <= virus_limit [i]):
  255.                     virus [i].sety (virus [i].ycor () - speed)
  256.  
  257.         for i in range (len (booster_activate)):
  258.             if booster_activate [i]:
  259.                 boosters [i].sety (boosters [i].ycor () - speed / 2)
  260.             if boosters [i].ycor () <= -500:
  261.                 boosters [i].sety (500)
  262.                 booster_activate [i] = False
  263.         screen.update ()
  264.         time.sleep (0.01)
  265.  
  266.  
  267. def rect (t, l, b):
  268.     t.begin_fill ()
  269.     t.pendown ()
  270.     for i in range (2):
  271.         t.forward (l)
  272.         t.right (90)
  273.         t.forward (b)
  274.         t.right (90)
  275.     t.end_fill ()
  276.     t.penup ()
  277.  
  278.  
  279. def start_game (x, y):
  280.     if x >= -100 and x <= 100:
  281.         if y <= -240 and y >= -290:
  282.             main_game ()
  283.  
  284.  
  285. def help_screen ():
  286.     pen.clear ()
  287.     pen.hideturtle ()
  288.  
  289.     screen.bgpic ('lungs2.gif')
  290.     pen.goto (-90, 190)
  291.     pen.write ('Instantly Cools \nBody \nTemperature', font = ('ravie', 15, 'normal'))
  292.     pen.goto (-130, 30)
  293.     pen.write ('Prevents Germs \nfrom entering \nBody', font = ('ravie', 15, 'normal'))
  294.     pen.goto (-100, -160)
  295.     pen.write ('Makes Destroying \nGerms \nEasier', font = ('ravie', 15, 'normal'))
  296.     pen.pencolor ('white')
  297.     pen.goto (-200, -220)
  298.     pen.write ('Swipe Knife over Germs to Destroy germs', font = ('Comic Sans Ms', 15,
  299.                                                                   'normal'))
  300.     pen.pencolor ('black')
  301.     pen.goto (-100, -240)
  302.     pen.seth (0)
  303.     rect (pen, 200, 50)
  304.     pen.goto (-53, -287)
  305.     pen.write ('START', font = ('Bauhaus 93', 30, 'normal'))
  306.     while True:
  307.         screen.onclick (start_game)
  308.         screen.update ()
  309.         time.sleep (0.01)
  310.  
  311.  
  312. def menu (x, y):
  313.     if x >= -100 and x <= 100:
  314.         if y <= -25 and y >= -75:
  315.             main_game ()
  316.         elif y <= -100 and y >= -150:
  317.             help_screen ()
  318.  
  319.  
  320. def welcome_screen ():
  321.     global knife_animate
  322.     pen.pencolor ('black')
  323.     pen.clear ()
  324.     screen.bgpic ('bg1.gif')
  325.     for i in knife_animate:
  326.         i.hideturtle ()
  327.     pen.goto (-100, -25)
  328.     pen.fillcolor ('white')
  329.     rect (pen, 200, 50)
  330.     pen.goto (-100, -100)
  331.     rect (pen, 200, 50)
  332.     pen.goto (-50, 100)
  333.  
  334.     pen.goto (-53, -72)
  335.     pen.write ('START', font = ('Bauhaus 93', 30, 'normal'))
  336.     pen.goto (-40, -147)
  337.     pen.write ('HELP', font = ('Bauhaus 93', 30, 'normal'))
  338.  
  339.     pen.pencolor ('white')
  340.     pen.goto (-200, 180)
  341.     pen.write ('VIRUS NINJA', font = ('ravie', 35, 'normal'))
  342.     pen.pencolor ('black')
  343.  
  344.     pen.goto (-50, 75)
  345.     pen.shape ('virus3.gif')
  346.     pen.showturtle ()
  347.     x = 0
  348.     dir = +1
  349.     while True:
  350.         pen.forward (1)
  351.         x += 1 * dir
  352.         if x >= 100:
  353.             pen.setheading (pen.heading () + 180)
  354.             dir = -1
  355.         if x <= 0:
  356.             dir = +1
  357.             pen.setheading (pen.heading () + 180)
  358.         screen.update ()
  359.         screen.onclick (menu)
  360.         time.sleep (0.01)
  361.  
  362.  
  363. def end_game (x, y):
  364.     if x >= -100 and x <= 100:
  365.         if y <= 100 and y >= 50:
  366.             welcome_screen ()
  367.         elif y <= 0 and y >= -50:
  368.             screen.bye ()
  369.             exit (0)
  370.  
  371.  
  372. def end_screen ():
  373.     global score, virus, knife_animate, boosters
  374.     pen.clear ()
  375.     thermometer.hideturtle ()
  376.     for i in range (len (virus)):
  377.         virus [i].hideturtle ()
  378.     for j in range (len (knife_animate)):
  379.         knife_animate [j].hideturtle ()
  380.     for k in range (len (boosters)):
  381.         boosters [k].hideturtle ()
  382.     screen.bgpic ('bg1.gif')
  383.     pen.setheading (0)
  384.     pen.goto (-100, 100)
  385.     rect (pen, 200, 50)
  386.     pen.goto (-100, 0)
  387.     rect (pen, 200, 50)
  388.     pen.goto (-63, 53)
  389.     pen.pencolor ('black')
  390.     pen.write ('REPLAY', font = ('Bauhaus 93', 30, 'normal'))
  391.     pen.goto (-50, -47)
  392.     pen.write ('QUIT', font = ('Bauhaus 93', 30, 'normal'))
  393.     pen.pencolor ('white')
  394.     pen.goto (-180, 150)
  395.     pen.write ('Game Over', font = ('ravie', 40, 'normal'))
  396.     if score < 100:
  397.         pen.goto (-100, -120)
  398.     elif score >= 100:
  399.         pen.goto (-110, -120)
  400.     pen.write ('Score : ' + str (score), font = ('ravie', 25, 'normal'))
  401.     while True:
  402.         screen.onclick (end_game)
  403.         screen.update ()
  404.         time.sleep (0.01)
  405.  
  406.  
  407. def finish_game ():
  408.     screen.bye ()
  409.     exit (0)
  410.  
  411.  
  412. def pause ():
  413.     global is_paused
  414.     pos = pen.pos ()
  415.  
  416.     if is_paused:
  417.         is_paused = False
  418.     elif not is_paused:
  419.         is_paused = True
  420.  
  421.     if is_paused:
  422.         pos = pen.pos ()
  423.         pen.penup ()
  424.         pen.goto (-80, -250)
  425.         pen.pencolor ('white')
  426.         pen.write ('Game Paused', font = ('Comic Sans Ms', 20, 'normal'))
  427.         pen.goto (pos)
  428.         for i in range (1, len (knife_animate)):
  429.             knife_animate [i].hideturtle ()
  430.         while True:
  431.             screen.update ()
  432.             time.sleep (0.01)
  433.             if not is_paused:
  434.                 pen.undo ()
  435.                 pen.undo ()
  436.                 pen.undo ()
  437.                 pen.undo ()
  438.                 pen.undo ()
  439.                 break
  440.  
  441.  
  442. screen.onkey (finish_game, 'Escape')
  443. screen.onkey (pause, 'space')
  444. screen.listen ()
  445.  
  446. # main_game_code
  447.  
  448. print ('Welcome to the Virus Ninja Game!')
  449. print ('Use the mouse to drag the knife and slash virus')
  450. print ('The ability to kill viruses depends of the virus')
  451. print ('The order of killing difficulty is : ')
  452. print ('Green < Dark_Green < Blue < Pink')
  453. print ('Score as many points you can and prevent body')
  454. print ('temperature from getting maxed up')
  455.  
  456. welcome_screen ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement