Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import random
- import time
- is_paused = False
- screen = turtle.Screen ()
- screen.setup (500, 650)
- screen.title ('Virus Ninja Game')
- screen.bgpic ('lungs3.gif')
- screen.tracer (0)
- for images in range (4):
- screen.register_shape ('virus' + str (images + 1) + '.gif')
- screen.register_shape ('thermometer' + str (images + 1) + '.gif')
- screen.register_shape ('knife1.gif')
- screen.register_shape ('thermometer5.gif')
- screen.register_shape ('thermometer6.gif')
- screen.register_shape ('thermometer7.gif')
- screen.register_shape ('thermometer8.gif')
- screen.register_shape ('first_aid.gif')
- screen.register_shape ('capsule.gif')
- screen.register_shape ('mask.gif')
- pen = turtle.Turtle ()
- pen.penup ()
- pen.hideturtle ()
- wait = False
- virus = []
- virus_state = []
- knife_animate = []
- knife_intensity = 6
- score = 0
- damage = 0
- speed = 3
- active_virus_no = 3
- booster_activate = [False, False, False]
- kill_state = []
- kill_time = []
- virus_health = []
- virus_permeability = []
- virus_limit = []
- permissible_virus = ['virus1.gif', 'virus2.gif']
- allowed_boosters = ['first_aid.gif', 'capsule.gif', 'mask.gif']
- boosters = []
- current_booster = None
- capsule_timer = time.time ()
- mask_timer = time.time ()
- for i in range (20):
- temp = turtle.Turtle ()
- temp.shape (random.choice (permissible_virus))
- virus.append (temp)
- virus_state.append (False)
- virus_health.append (100)
- virus_permeability.append (20)
- virus_limit.append (-500)
- temp.penup ()
- temp.goto (random.randint (-200, 200), 500)
- for i in range (3):
- temp = turtle.Turtle ()
- temp.shape (allowed_boosters [i])
- temp.penup ()
- temp.sety (500)
- boosters.append (temp)
- for i in range (knife_intensity):
- temp = turtle.Turtle ()
- temp.shape ('square')
- temp.shapesize (0.2, 0.2, 0.2)
- temp.color ('white')
- temp.penup ()
- knife_animate.append (temp)
- knife = turtle.Turtle ()
- knife.penup ()
- knife.shape ('knife1.gif')
- knife_animate = [knife] + knife_animate
- knife_prev_pos_x = knife.xcor ()
- knife_prev_pos_y = knife.ycor ()
- knife_previous_time = time.time ()
- knife_velocity_x = 0
- knife_velocity_y = 0
- def slash (x, y):
- global current_booster
- if not is_paused:
- knife.goto (x + 25, y + 25)
- for i in boosters:
- if i.distance ((x, y)) <= 20:
- current_booster = i
- booster_active (i)
- def slash_animation ():
- global knife_animate, knife_intensity
- for i in range (len (knife_animate) - 1, knife_intensity - 1, -1):
- knife_animate [i].hideturtle ()
- for i in range (knife_intensity - 1, 0, -1):
- if i == 1:
- knife_animate [i].showturtle ()
- knife_animate [i].goto (knife_animate [i - 1].xcor () + 53,
- knife_animate [i - 1].ycor () + 43)
- else:
- knife_animate [i].showturtle ()
- knife_animate [i].goto (knife_animate [i - 1].pos ())
- def booster_active (booster):
- booster.sety (500)
- global damage, knife_intensity, wait, capsule_timer, mask_timer, virus_limit
- if booster.shape () == 'first_aid.gif':
- booster_activate [0] = False
- damage -= 5
- if damage < 0:
- damage = 0
- elif booster.shape () == 'capsule.gif':
- booster_activate [1] = False
- capsule_timer = time.time ()
- knife_intensity = 6
- elif booster.shape () == 'mask.gif':
- booster_activate [2] = False
- mask_timer = time.time ()
- wait = True
- for i in range (len (virus_limit)):
- virus_limit [i] = random.randint (-200, 300)
- thermometer = turtle.Turtle ()
- thermometer.penup ()
- thermometer.setpos (200, -170)
- thermometer.shape ('thermometer1.gif')
- thermometer.hideturtle ()
- def main_game ():
- screen.onclick (slash)
- knife.ondrag (knife.goto)
- screen.bgpic ('lungs3.gif')
- global knife_animate, knife_intensity, knife_velocity_y, knife_velocity_x
- global wait, damage, score, mask_timer, capsule_timer, knife_previous_time
- global knife_prev_pos_x, knife_prev_pos_y, active_virus_no, permissible_virus
- global virus_limit, virus, virus_health, virus_state, virus_permeability
- global booster_activate, boosters, current_booster, allowed_boosters
- global speed
- permissible_virus = ['virus1.gif', 'virus2.gif']
- thermometer.showturtle ()
- knife.showturtle ()
- knife.setpos (0, 0)
- active_virus_no = 3
- for i in range (len (virus)):
- virus [i].showturtle ()
- virus [i].sety (random.randint (500, 300 + active_virus_no * 100))
- virus_state [i] = False
- for j in range (len (boosters)):
- boosters [j].showturtle ()
- boosters [j].sety (random.randint (500, 300 + active_virus_no * 100))
- booster_activate [j] = False
- pen.clear ()
- pen.hideturtle ()
- pen.goto (80, 250)
- knife_intensity = 4
- damage = 0
- score = 0
- speed = 3
- wait = False
- while True:
- pen.pendown ()
- pen.clear ()
- pen.pencolor ('white')
- pen.write ('Score : ' + str (score), font = ('Comic Sans Ms', 20, 'normal'))
- pen.penup ()
- if time.time () - capsule_timer >= 10:
- knife_intensity = 4
- if time.time () - mask_timer >= 5:
- wait = False
- thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
- for i in range (len (virus)):
- if virus [i].shape () == 'virus1.gif':
- virus_permeability [i] = 10
- elif virus [i].shape () == 'virus2.gif':
- virus_permeability [i] = 10
- elif virus [i].shape () == 'virus3.gif':
- virus_permeability [i] = 3
- elif virus [i].shape () == 'virus4.gif':
- virus_permeability [i] = 2
- if time.time () - knife_previous_time >= 0.2:
- knife_velocity_x = ((knife.xcor () - knife_prev_pos_x) / (0.2)) / 10
- knife_velocity_y = ((knife.ycor () - knife_prev_pos_y) / (0.2)) / 10
- knife_previous_time = time.time ()
- knife_prev_pos_x = knife.xcor ()
- knife_prev_pos_y = knife.ycor ()
- if (abs (knife_velocity_x) > 50 and abs (knife_velocity_x) < 150) or (abs (
- knife_velocity_y) > 50 and abs (knife_velocity_y) < 150):
- for i in range (len (virus)):
- if virus [i].distance (knife.pos ()) < 100:
- virus_health [i] -= knife_intensity * virus_permeability [i]
- if virus_health [i] <= 0:
- virus_health [i] = 100
- virus [i].sety (random.randint (500, 300 + 100 * (active_virus_no)))
- score += 1
- if score % 10 == 0:
- if speed <= 9:
- speed += 0.2
- if score % 10 == 0:
- active_virus_no += 1
- if score % 5 * (active_virus_no - 2) == 0:
- booster_activate [random.randint (0, 2)] = True
- if active_virus_no >= 19:
- active_virus_no = 20
- if score % 15 == 0:
- permissible_virus.append ('virus3.gif')
- if score % 25 == 0:
- permissible_virus.append ('virus4.gif')
- virus [i].setx (random.randint (-150, 150))
- virus [i].shape (random.choice (permissible_virus))
- slash_animation ()
- for i in virus:
- if i.ycor () <= -500:
- i.sety (random.randint (500, 300 + active_virus_no * 100))
- if damage <= 6:
- damage += 1
- thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
- if damage >= 7:
- thermometer.shape ('thermometer' + str (damage + 1) + '.gif')
- screen.update ()
- end_screen ()
- for i in range (active_virus_no):
- virus_state [i] = True
- for i in range (len (virus)):
- if virus_state [i]:
- if not (wait and virus [i].ycor () <= virus_limit [i]):
- virus [i].sety (virus [i].ycor () - speed)
- for i in range (len (booster_activate)):
- if booster_activate [i]:
- boosters [i].sety (boosters [i].ycor () - speed / 2)
- if boosters [i].ycor () <= -500:
- boosters [i].sety (500)
- booster_activate [i] = False
- screen.update ()
- time.sleep (0.01)
- def rect (t, l, b):
- t.begin_fill ()
- t.pendown ()
- for i in range (2):
- t.forward (l)
- t.right (90)
- t.forward (b)
- t.right (90)
- t.end_fill ()
- t.penup ()
- def start_game (x, y):
- if x >= -100 and x <= 100:
- if y <= -240 and y >= -290:
- main_game ()
- def help_screen ():
- pen.clear ()
- pen.hideturtle ()
- screen.bgpic ('lungs2.gif')
- pen.goto (-90, 190)
- pen.write ('Instantly Cools \nBody \nTemperature', font = ('ravie', 15, 'normal'))
- pen.goto (-130, 30)
- pen.write ('Prevents Germs \nfrom entering \nBody', font = ('ravie', 15, 'normal'))
- pen.goto (-100, -160)
- pen.write ('Makes Destroying \nGerms \nEasier', font = ('ravie', 15, 'normal'))
- pen.pencolor ('white')
- pen.goto (-200, -220)
- pen.write ('Swipe Knife over Germs to Destroy germs', font = ('Comic Sans Ms', 15,
- 'normal'))
- pen.pencolor ('black')
- pen.goto (-100, -240)
- pen.seth (0)
- rect (pen, 200, 50)
- pen.goto (-53, -287)
- pen.write ('START', font = ('Bauhaus 93', 30, 'normal'))
- while True:
- screen.onclick (start_game)
- screen.update ()
- time.sleep (0.01)
- def menu (x, y):
- if x >= -100 and x <= 100:
- if y <= -25 and y >= -75:
- main_game ()
- elif y <= -100 and y >= -150:
- help_screen ()
- def welcome_screen ():
- global knife_animate
- pen.pencolor ('black')
- pen.clear ()
- screen.bgpic ('bg1.gif')
- for i in knife_animate:
- i.hideturtle ()
- pen.goto (-100, -25)
- pen.fillcolor ('white')
- rect (pen, 200, 50)
- pen.goto (-100, -100)
- rect (pen, 200, 50)
- pen.goto (-50, 100)
- pen.goto (-53, -72)
- pen.write ('START', font = ('Bauhaus 93', 30, 'normal'))
- pen.goto (-40, -147)
- pen.write ('HELP', font = ('Bauhaus 93', 30, 'normal'))
- pen.pencolor ('white')
- pen.goto (-200, 180)
- pen.write ('VIRUS NINJA', font = ('ravie', 35, 'normal'))
- pen.pencolor ('black')
- pen.goto (-50, 75)
- pen.shape ('virus3.gif')
- pen.showturtle ()
- x = 0
- dir = +1
- while True:
- pen.forward (1)
- x += 1 * dir
- if x >= 100:
- pen.setheading (pen.heading () + 180)
- dir = -1
- if x <= 0:
- dir = +1
- pen.setheading (pen.heading () + 180)
- screen.update ()
- screen.onclick (menu)
- time.sleep (0.01)
- def end_game (x, y):
- if x >= -100 and x <= 100:
- if y <= 100 and y >= 50:
- welcome_screen ()
- elif y <= 0 and y >= -50:
- screen.bye ()
- exit (0)
- def end_screen ():
- global score, virus, knife_animate, boosters
- pen.clear ()
- thermometer.hideturtle ()
- for i in range (len (virus)):
- virus [i].hideturtle ()
- for j in range (len (knife_animate)):
- knife_animate [j].hideturtle ()
- for k in range (len (boosters)):
- boosters [k].hideturtle ()
- screen.bgpic ('bg1.gif')
- pen.setheading (0)
- pen.goto (-100, 100)
- rect (pen, 200, 50)
- pen.goto (-100, 0)
- rect (pen, 200, 50)
- pen.goto (-63, 53)
- pen.pencolor ('black')
- pen.write ('REPLAY', font = ('Bauhaus 93', 30, 'normal'))
- pen.goto (-50, -47)
- pen.write ('QUIT', font = ('Bauhaus 93', 30, 'normal'))
- pen.pencolor ('white')
- pen.goto (-180, 150)
- pen.write ('Game Over', font = ('ravie', 40, 'normal'))
- if score < 100:
- pen.goto (-100, -120)
- elif score >= 100:
- pen.goto (-110, -120)
- pen.write ('Score : ' + str (score), font = ('ravie', 25, 'normal'))
- while True:
- screen.onclick (end_game)
- screen.update ()
- time.sleep (0.01)
- def finish_game ():
- screen.bye ()
- exit (0)
- def pause ():
- global is_paused
- pos = pen.pos ()
- if is_paused:
- is_paused = False
- elif not is_paused:
- is_paused = True
- if is_paused:
- pos = pen.pos ()
- pen.penup ()
- pen.goto (-80, -250)
- pen.pencolor ('white')
- pen.write ('Game Paused', font = ('Comic Sans Ms', 20, 'normal'))
- pen.goto (pos)
- for i in range (1, len (knife_animate)):
- knife_animate [i].hideturtle ()
- while True:
- screen.update ()
- time.sleep (0.01)
- if not is_paused:
- pen.undo ()
- pen.undo ()
- pen.undo ()
- pen.undo ()
- pen.undo ()
- break
- screen.onkey (finish_game, 'Escape')
- screen.onkey (pause, 'space')
- screen.listen ()
- # main_game_code
- print ('Welcome to the Virus Ninja Game!')
- print ('Use the mouse to drag the knife and slash virus')
- print ('The ability to kill viruses depends of the virus')
- print ('The order of killing difficulty is : ')
- print ('Green < Dark_Green < Blue < Pink')
- print ('Score as many points you can and prevent body')
- print ('temperature from getting maxed up')
- welcome_screen ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement