Demo entry 6352622

EPQ Game

   

Submitted by anonymous on Mar 24, 2017 at 23:08
Language: Python 3. Code size: 37.1 kB.

import pygame
import sys
from pygame.locals import *
import time
from anim import Anim
from timer import Timer
import random
import tkinter
from tkinter import * 
import tkinter.messagebox as tkm
from functools import partial, wraps  


def menu():
    global menu1
    global menu_canvas
    menu1=tkinter.Tk()
    menu1.title("EPQ Game")
    menu_canvas=tkinter.Canvas(menu1, width=500, height=100, highlightthickness=0)
    menu_canvas.pack()
    message=tkinter.Label(menu_canvas, width=40,height=3, text="Pick a game mode")
    message.pack()
    easy_mode= tkinter.Button(menu_canvas,text="Easy",height=1, command=partial(intro, True))
    easy_mode.pack()
    hard_mode=tkinter.Button(menu_canvas, text="Hard",height=1, command=partial(intro, False))
    hard_mode.pack()
    menu1.mainloop()



def killdagame():
    sys.exit()
    end.destroy()


wheelz_done=False
wheel_done=False
kill_santa=False
kill_car=False
kill_robot=False
santa_done=False
night_dead=False
dog_dead=False
dog_done=False
kill_kid=False
wheel_returned=False
dog_walk=False
health=50
new_health=100


def resetanim():
    global ninja_anim
    global x_change
    if x_change !=0:
        ninja_anim=ninja_run
    elif x_change==0:
        ninja_anim = ninja_stand
    old_dir = ninja_anim.dir
    ninja_anim.face(old_dir)
    ninja_anim.reset()


def end():
    global wheel_done, kill_robot, kill_car, kill_santa, santa_done, night_dead, dog_dead, dog_done, score, kill_kid, presents_found, wheelz_done, dog_walk, returned, end
    pygame.quit()

    end=tkinter.Tk()
    end.title("Game Over")
    end_canvas=tkinter.Canvas(end, highlightthickness=0)
    end_canvas.pack()
    score=str(score)
    score_label=tkinter.Label(end_canvas,text="Your final score was "+score)
    score_label.pack()

    if ninja_anim!=ninja_dead:
        if kill_santa==True and kill_car==True and kill_robot==True and night_dead==True and dog_dead==True and kill_kid==True:
            bad=tkinter.Label(end_canvas,width=40,height=3, text="You killed everyone")
            bad.pack()
        elif kill_santa==True and kill_car ==True and kill_robot==True:
            bad=tkinter.Label(end_canvas,width=40,height=3 ,text="At the start you killed but by the end you didn't")
            bad.pack()
        elif kill_santa==True and kill_car==False and dog_dead==False and night_dead==False:
            badish=tkinter.Label(end_canvas,height=3,text="You initially killed but quickly realised it was easier not to kill")
            badish.pack()
        elif kill_santa==False and kill_car==False and kill_robot==False and night_dead==False and dog_dead==False and kill_kid==False:
            good=tkinter.Label(end_canvas,height=3,width=40, text="You did not kill anything")
            good.pack()
        else:
            bad=tkinter.Label(end_canvas,height=3,text="Your behaviour is unpredictable and does not appear to be affected by reward or punishment")
            bad.pack()

    elif ninja_anim==ninja_dead:
        if kill_santa==True and kill_car==True and kill_robot==False:
            bad=tkinter.Label(end_canvas,height=3,text="You killed everyone you encountered and died at the first possible point")
            bad.pack()
        elif kill_santa==True and kill_car==True and kill_robot==True and night_dead==False:
            bad=tkinter.Label(end_canvas,height=3,text="You killed everyone you encountered but died before you could complete the game")
            bad.pack()
        elif kill_santa==False and kill_car==False and dog_dead==False:
            goodish=tkinter.Label(end_canvas,height=3, width=50,text="You spared the innocent people but tried to kill the rest")
            goodish.pack()
        else:
            unknown=tkinter.Label(end_canvas,height=3,width=40,text="You were killed before you could complete the game")
            unknown.pack()

    if returned==True and wheelz_done==True and dog_done==True:
        gg=tkinter.Label(end_canvas,text="You successfully completed all of the encounters",height=3,width=40)
        gg.pack()

    elif returned==False and presents_found==3:
        if wheelz_done==False and wheel_done==True:
            if dog_done==False and dog_walk==True:
                useless=tkinter.Label(end_canvas,text="You begun all the encounters but didn't complete them", height=3)
                useless.pack()

    elif presents_found ==0 and wheel_done==False and dog_walk==False:
        pooreffort=tkinter.Label(end_canvas,height=3,text="You did not start or complete any of the encounters")
        pooreffort.pack()

    else:
        incomplete=tkinter.Label(end_canvas,height=3,text="You did some encounters but not all")
        incomplete.pack()
    exit=tkinter.Button(text="Exit", command=killdagame)
    exit.pack()



ninja_run=Anim("Run__00%s.png".__mod__, 0, 9, (150, 180), True, 10)
ninja_throw = Anim("Throw__00%s.png".__mod__, 0, 9, (150, 180), False, 10, oncomplete=resetanim)
ninja_dead=Anim("Dead__00%s.png".__mod__, 0, 9, (150,180), False, 10, oncomplete=end)
ninja_stand=Anim(lambda _: "Idle__000.png",1, 10, (95, 180), True, 1)


def intro(easy):
    global intro
    global menu_canvas
    menu_canvas.destroy()
    menu1.destroy()
    intro=tkinter.Tk()
    intro.title("Introduction")
    intro_canvas=tkinter.Canvas(intro, highlightthickness=0)
    intro_canvas.pack()
    message=tkinter.Label(intro_canvas,height=2, text="The main aim of this game is to encourage the user to use non-violence to pass encounters.\n Violence can be used but this could have concequences later on in the game and using non-violence will be rewarded.")
    message.pack()
    if easy:
        continu=tkinter.Button(intro_canvas, text="Continue", height=2, command=partial(game, True))
    else:
        continu=tkinter.Button(intro_canvas, text="Continue", height=2, command=partial(game, False))
    continu.pack()



def game(easy):
    pygame.font.init()
    default_font=pygame.font.get_default_font()
    font_renderer_big=pygame.font.Font(default_font, 18)
    font_renderer_small=pygame.font.Font(default_font, 16)
    font_renderer_xl=pygame.font.Font(default_font, 20)
    label_1=font_renderer_big.render(
        "Wrong Way!",
        1,
        (255,255,255))
    label=font_renderer_small.render(
        "I have lost my presents",
        1,
        (255,0,0))
    labelpt2=font_renderer_small.render(
        "and need you to",
        1,
        (255,0,0))
    labelpt3=font_renderer_small.render(
        "retrieve them. Please help",
        1,
        (255,0,0))
    label2=font_renderer_big.render(
        "Use the arrow keys to move",
        1,
        (255,255,255))
    label3=font_renderer_big.render(
        "Press space to throw knifes",
        1,
        (255,255,255))
    gift=font_renderer_xl.render(
        "Help Santa by collecting his presents for him",
        1,
        (255,255,255))
    label4=font_renderer_small.render(
        "Thanks for the help, enjoy your reward!",
        1,
        (255,0,0))
    reward=font_renderer_big.render(
        "Great job, now return to santa for your gift!",
        1,
        (255,255,255))
    reward2=font_renderer_big.render(
        "Use up and down arrows to jump and slide",
        1,
        (255,255,255))
    help1=font_renderer_small.render(
        "A robot stole my wheels",
        1,
        (255,255,255))
    help2=font_renderer_small.render(
        "Please help!",
        1,
        (255,255,255))
    robot_talk=font_renderer_small.render(
        "Since you killed Santa I'm going to kill you",
        1,
        (255,255,255))
    robot_talk2=font_renderer_small.render(
        "Since you helped Santa you can have these wheels",
        1,
        (255,255,255))
    wheels_done=font_renderer_big.render(
        "Return to the truck for your reward!",
        1,
        (255,255,255))
    wheels_done2=font_renderer_small.render(
        "Thanks, have some extra life as a reward",
        1,
        (255,255,255))
    attack=font_renderer_small.render(
        "You fool, you shall die for that",
        1,
        (255,255,255))
    attack2=font_renderer_small.render(
        "I can't trust you, you've killed everyone",
        1,
        (255,255,255))
    night_help=font_renderer_small.render(
        "Help, a group of zombies stole my dog",
        1,
        (255,255,255))
    night_angry=font_renderer_small.render(
        "You fool, now you will perish!",
        1,
        (255,255,255))
    doggy=font_renderer_small.render(
        "Please help me",
        1,
        (255,255,255))
    doggy_mad=font_renderer_small.render(
        "You killed my master! I can't",
        1,
        (255,255,255))
    doggy_mad2=font_renderer_small.render(
        "    trust you, kill him!",
        1,
        (255,255,255))
    doggy_complete=font_renderer_small.render(
        "Thanks, I've sent my friend to scare off the zombies",
        1,
        (255,255,255))
    boy_message=font_renderer_small.render(
        "Thanks for helping my friends out",
        1,
        (255,255,255))
    night_message=font_renderer_big.render(
        "Retrieve the dog from the zombies",
        1,
        (255,255,255))
    boy_message2=font_renderer_small.render(
        "I've scared off the zombies",
        1,
        (255,255,255))
    doggy_leadder=font_renderer_big.render(
        "Lead the dog back to the knight",
        1,
        (255,255,255))


    LEFT, RIGHT = 0,1
    global ninja_anim
    global intro
    intro.destroy()
    pygame.init()
    display_width=1000
    display_height=800
    forest_img=pygame.image.load("forest.jpg")
    clock=pygame.time.Clock()
    game_display=pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption("EPQ Game")
    game_display.blit(forest_img,(0,0))
    ninja_stand.load()
    kunai=pygame.image.load("Kunai.png")
    kunai=pygame.transform.scale(kunai, (13,55))
    ninja_run.load()
    ninja_slide=Anim("Slide__00%s.png".__mod__, 0, 9, (150, 145), True, 10)
    ninja_slide.load()
    ninja_jump=Anim("Jump__00%s.png".__mod__, 0, 9, (150, 180), True, 15)
    ninja_jump.load()
    ninja_throw.load()
    ninja_dead.load()
    ninja_anim = ninja_stand
    ninja_health=100
    global jump
    jump=False
    global presents_found
    presents_found=0
    global score
    score=0
    global returned
    returned=False


    def ninja(x,y):
        game_display.blit(ninja_anim.next(),(x,y))


    class Presents():
        def __init__(self,x,y, santa):
            self.x=x
            self.y=y
            self.santa = santa
            self.present=Anim(lambda _:"present.png", 1, 1, (90, 63), True, 1)
            self.present.load()
                    
        def draw(self):
            if self.santa.santa_anim != self.santa.santa_dead:
                game_display.blit(self.present.next(),(self.x+bx, self.y+113))

        def collide(self,x,y):
            global presents_found
            if x+100>self.x>x and y+185>self.y>y-200:
                presents.remove(self)
                presents_found +=1
                if presents_found==3:
                    santa.complete()

    class Santa():
        def __init__(self, x, y):
            self.x=x
            self.y=y
            self.dead = False
            self.vx=0
            self.done=False
            self.done2=False
            self.abc=False
            self.santa_stand=Anim(lambda _:"Santa Idle.png", 1,10,(270,190), True,1)
            self.santa_stand.load()
            self.santa_dead=Anim("Santa Dead 0%02d.png".__mod__, 1, 17, (270,190), False, 10)
            self.santa_dead.load()
            self.santa_anim=self.santa_stand

        def draw(self):
            global returned, jump, score
            game_display.blit(self.santa_anim.next(),(self.x+bx, self.y))
            if self.santa_anim != self.santa_dead and self.done==False:
                game_display.blit(label,(self.x+bx,self.y-50))
                game_display.blit(labelpt2,(self.x+bx,self.y-35))
                game_display.blit(labelpt3,(self.x+bx,self.y-20))
                game_display.blit(gift, (2700+bx, 10))
            elif self.santa_anim !=self.santa_dead and self.done==True and self.done2==False:
                game_display.blit(reward, (7000+bx,455))
                if x+270>self.x>x and y+190>self.y>y:
                    returned=True
            if returned==True:
                game_display.blit(label4,(self.x+bx,self.y-20))
                game_display.blit(reward2,(self.x+bx+500,455))
                jump=True
                if self.abc==False:
                    score+=1
                self.abc=True
                self.done2=True


        def collide(self, x, y):
            return self.x + 270 > x > self.x and self.y + 190 > y > self.y

        def die(self):
            global kill_santa
            kill_santa=True
            global score
            self.santa_anim=self.santa_dead
            self.dead=True
            score-=2


        def complete(self):
            global score
            global santa_done
            santa_done=True
            score+=1
            self.done=True

    class Wheel():
        def __init__(self,x,y):
            self.x=x
            self.y=y
            self.wheel=Anim(lambda _:"Wheel1.png", 1, 10, (90,90), True, 1)
            self.wheel.load()

        def draw(self):
            global kill_car
            if kill_car==False:
                game_display.blit(self.wheel.next(),(self.x+bx, self.y))

        def collide(self,x,y):
            if x+100>self.x>x and y+185>self.y>y-200:
                wheels.remove(self)
                if len(wheels)==0:
                    global wheel_done
                    wheel_done=True
                    global score
                    score+=1


    class Truck():
        def __init__(self, x, y):
            self.x=x
            self.y=y
            self.xyz=False
            self.body=Anim(lambda _:"Body.png", 1,10,(270,190), True,1)
            self.body.load()
            self.head=Anim(lambda _:"Head.png", 1,10,(90,70), True,1)
            self.head.load()
            self.wheel=Anim(lambda _:"Wheel1.png", 1, 10, (90,90), True, 1)
            self.wheel.load()            
            self.dead=False
            self.health=10


        def draw(self):
            global score
            global wheel_done
            global wheel_returned
            global wheelz_done
            if self.dead==False and wheel_done==False:
                game_display.blit(help1,(self.x+bx, self.y-90))
                game_display.blit(help2, (self.x+bx, self.y-75))
                game_display.blit(self.body.next(),(self.x+bx,self.y))
                game_display.blit(self.head.next(),(self.x+bx+52,self.y-58))

            if wheel_done==True and kill_car==False:
                if self.xyz==False:
                    score+=1
                    self.xyz=True
                game_display.blit(wheels_done,(12000+bx,10))
                game_display.blit(wheels_done2, (self.x+bx, self.y-100))
                game_display.blit(self.body.next(), (self.x+bx,self.y-25))
                game_display.blit(self.head.next(), (self.x+bx+52,self.y-83))
                game_display.blit(self.wheel.next(), (self.x+bx+20, self.y+100))
                game_display.blit(self.wheel.next(), (self.x+bx+150, self.y+100))
                

            if self.dead==True:
                game_display.blit(self.body.next(),(self.x+bx,self.y))
                game_display.blit(self.head.next(),(self.x+bx+100,self.y))

            if x+300>self.x>x and y+185>self.y>y-200 and wheel_done==True:
                wheelz_done=True


        def collide(self,x,y):
            return self.x + 270 > x > self.x and self.y + 190 > y > self.y

        def hit(self):
            self.health-=10
            if self.health==0:
                truck.die()

        def die(self):
            global score
            score-=2
            global kill_car
            kill_car=True
            self.dead=True
            global wheel_done
            wheel_done=False





    class Robot():
        def __init__(self, x, y, santa):
            self.x=x
            self.y=y
            self.dead=False
            self.health=50
            self.vx=0
            self.santa=santa
            self.robot_stand=Anim(lambda _:"Robot Idle.png", 1, 10, (210, 177), True, 1)
            self.robot_stand.load()
            self.robot_shoot=Anim("Robot Shoot %s.png".__mod__, 1, 4, (210, 177), True, 10)
            self.robot_shoot.load()
            self.robot_dead=Anim("Robot Dead %02d.png".__mod__, 1,10, (210, 177), False, 10)
            self.robot_dead.load()
            self.robot_anim=self.robot_stand
            self.bullet_timer = Timer(400)
        def draw(self):
            game_display.blit(self.robot_anim.next(), (self.x+bx, self.y))
        def update(self):
            global kill_car
            if self.dead:
                return

            self.x+=self.vx

            if self.robot_anim == self.robot_shoot and self.bullet_timer.tick():
                bullets.append(Bullet(self.x, self.y+50, 10 if self.robot_anim.dir == RIGHT else -10))

            direction = (x - self.x) / (abs(x - self.x) + 0.0001)
            if direction > 0:
                self.robot_anim.face(RIGHT)
            elif direction < 0:
                self.robot_anim.face(LEFT)

            if self.santa.dead==True and kill_car==False:
                game_display.blit(robot_talk, (self.x+bx,self.y-20))
                if x+400>self.x>x and y +200>self.y>y:
                    self.shoot()
            elif self.santa.dead==False and self.health==50 and kill_car==False:
                game_display.blit(robot_talk2, (self.x+bx,self.y-20))
            elif self.santa.dead==False and self.health<50:
                game_display.blit(attack,(self.x+bx,self.y-20))
            elif self.santa.dead==True and kill_car==True:
                game_display.blit(attack2,(self.x+bx,self.y-20))
                self.shoot()
                

        def die(self):
            self.robot_anim=self.robot_dead
            self.dead=True
            self.vx=0
            global kill_robot
            global score
            score-=1
            kill_robot=True

        def collide(self, x, y):
            return self.x + 210 > x > self.x and self.y + 177 > y > self.y

        def hit(self, damage):
            self.health-=damage
            if self.health <= 0:
                self.die()
            else:
                self.shoot()

        def shoot(self):
            old_dir=self.robot_anim.dir
            self.robot_anim=self.robot_shoot
            self.robot_anim.face(old_dir)

    class Bullet():
        def __init__(self, x, y, vx):
            self.x = x
            self.y = y
            self.vx = vx
            self.anim = Anim(lambda _:"Bullet.png", 1, 10, (20, 13), True, 1)
            self.anim.load()

        def update(self):
            self.x += self.vx

        def draw(self):
            game_display.blit(self.anim.next(), (self.x + bx+140, self.y+30))
            if self.x + bx+50 > display_width:
                bullets.remove(self)
            if self.x + bx+150 < 0:
                bullets.remove(self)
            if self.y < 0:
                bullets.remove(self)
            if self.y > display_height:
                bullets.remove(self)

        def collide(self,x,y):
            global health
            if self.x+100>x>self.x and self.y+185>y>self.y-200:
                if easy:
                    health-=5
                if not easy:
                    health-=10
                bullets.remove(self)

    class Night():
        def __init__(self,x,y,vx):
            self.x=x
            self.y=y
            self.dead=False
            self.health=50
            self.walking=False
            self.vx=vx
            self.night_stand=Anim(lambda _:"knight_idle.png", 1, 10, (180,200), True, 1)
            self.night_stand.load()
            self.night_die=Anim("knight_die0%s.png".__mod__, 0,9, (180,200),False,10)
            self.night_die.load()
            self.night_run=Anim("Knight Run %02d.png".__mod__, 1, 10, (180, 200), True, 10)
            self.night_run.load()
            self.night_attack=Anim("Knight Attack %02d.png".__mod__, 1, 10, (180, 200), True, 10, oncomplete=self.hitperson)
            self.night_attack.load()
            self.night_anim=self.night_stand

        def hitperson(self):
            global health
            global new_health
            global kill_car
            if kill_car==True:
                if easy:
                    new_health -= 8
                else:
                    new_health-=10
            else:
                if easy:
                    health-=8
                else:
                    health-=10

        def draw(self):
            global dog_done
            game_display.blit(self.night_anim.next(),(self.x+bx,self.y))
            if not self.dead and self.night_anim != self.night_run and self.night_anim != self.night_attack and dog_done==False:
                game_display.blit(night_help,(13500+bx,self.y-55))
                game_display.blit(night_message,(13500+bx,10))
            elif self.night_anim==self.night_attack or self.night_anim==self.night_run:
                game_display.blit(night_angry,(self.x+bx,self.y-55))
            elif self.dead==True:
                pass

        def update(self):
            if self.dead==True:
                return

            self.x+=self.vx
            if self.night_anim==self.night_run:
                direction = (x - self.x) / abs(x - self.x)+0.0000000001
                self.vx = 3 * direction
                if direction > 0:
                    self.night_anim.face(RIGHT)
                elif direction < 0:
                    self.night_anim.face(LEFT)

            if abs(self.x-x)<=40 and self.health<50:
                old_dir = self.night_anim.dir
                self.night_anim = self.night_attack
                self.night_anim.face(old_dir)
                self.vx = 0
            elif self.night_anim != self.night_run and self.walking:
                self.walk()

        def die(self):
            global night_dead
            night_dead=True
            self.dead=True
            self.night_anim=self.night_die
            global score
            score-=2

        def collide(self,x,y):
            return self.x + 210 > x > self.x and self.y + 177 > y > self.y

        def hit(self, damage):
            self.health-=damage
            if self.health <= 0:
                self.die()
            elif self.health !=0:
                self.walk()

        def walk(self):
            old_dir=self.night_anim.dir
            self.night_anim=self.night_run
            self.night_anim.reset()
            self.walking=True



    class Zombie():
        def __init__(self, x, y, vx):
            self.walking=False
            self.x=x
            self.y=y
            self.vx=vx
            self.dead = False
            self.health = 30
            self.attacked=False
            self.zombie_stand=Anim(lambda _:"Zombie Idle.png",1,10,(156,185), True, 1)
            self.zombie_stand.load()
            self.zombie_dead=Anim("Zombie Dead 0%02d.png".__mod__, 0, 11, (221, 185), False, 10)
            self.zombie_dead.load()
            self.zombie_walk=Anim("Zombie Walk 0%02d.png".__mod__, 1, 10, (156, 185), True, 10)
            self.zombie_walk.load()
            self.zombie_attack=Anim("Zombie Attack %s.png".__mod__, 1, 8, (156, 185), True, 10, oncomplete=self.eatperson)
            self.zombie_attack.load()
            self.zombie_anim=self.zombie_stand

        def eatperson(self):
            global health
            global new_health
            global kill_car
            if kill_car==False:
                if easy:
                    new_health-=5
                else:
                    new_health-=10
            else:
                if easy:
                    health-=5
                else:
                    health-=10


        def draw(self):
            global dog_done
            if dog_done==False:
                game_display.blit(self.zombie_anim.next(), (self.x+bx, self.y))

        def update(self):
            global night_dead
            if self.dead: return
            self.x += self.vx
            direction = (x - self.x) / (abs(x - self.x) + 0.0001)
            if direction > 0:
                self.zombie_anim.face(RIGHT)
            elif direction < 0:
                self.zombie_anim.face(LEFT)


            if self.zombie_anim == self.zombie_walk:
                self.vx = random.randint(1,6) * direction


            if night_dead==True:
                self.walking=True

            if abs(self.x-x)<=40:
                if night_dead==False:
                    pass
                if night_dead==True or self.walking==True:

                    old_dir = self.zombie_anim.dir
                    self.zombie_anim = self.zombie_attack
                    self.zombie_anim.face(old_dir)
                    self.vx = 0

            elif self.zombie_anim != self.zombie_walk and self.walking:
                self.walk()



        def collide(self, x, y):
            return self.x + 156 > x > self.x and self.y + 185 > y > self.y

        def walk(self):
            old_dir = self.zombie_anim.dir
            self.zombie_anim=self.zombie_walk
            self.zombie_anim.face(old_dir)
            self.zombie_anim.reset()
            self.walking = True

        def hit(self, damage):
            self.health -= damage
            if self.health <= 0:
                self.die()
            else:
                self.walk()
            for zombie in zombies:
                if not zombie.walking:
                    zombie.walk()


        def die(self):
            global score
            self.zombie_anim=self.zombie_dead
            self.dead = True
            self.vx = 0

    class Dog():
        def __init__(self,x,y,vx):
            self.x=x
            self.y=y
            self.dead=False
            self.vx=vx
            self.walking=False
            self.collided=False
            self.returned=False
            self.mah=False
            self.dog_stand=Anim(lambda _:"Dog Idle.png",1,10,(120,100),True,1)
            self.dog_stand.load()
            self.dog_die=Anim("Dog_dead %02d.png".__mod__, 1, 10, (120,100), False,10)
            self.dog_die.load()
            self.dog_walk=Anim("Dog Walk %02d.png".__mod__, 1, 10,(120,100), True, 10)
            self.dog_walk.load()
            self.dog_anim=self.dog_stand

        def draw(self):
            global night_dead
            global score
            game_display.blit(self.dog_anim.next(), (self.x+bx,self.y+18))
            if night_dead==False and self.walking==False and self.x==15300:
                game_display.blit(doggy,(self.x+bx,self.y-35))
                game_display.blit(doggy_leadder,(15300+bx,10))
            elif night_dead==True:
                game_display.blit(doggy_mad,(self.x+bx,self.y-50))
                game_display.blit(doggy_mad2,(self.x+bx,self.y-35))
            elif self.returned==True:
                game_display.blit(doggy_complete,(self.x+bx-30,self.y-50))
                if self.mah==False:
                    score+=1
                    self.mah=True

        def update(self):
            global night_dead
            global dog_done
            global score
            if self.walking==True:
                direction = (x - self.x) / (abs(x - self.x) + 0.0001)
                self.vx=(4*direction)
                if direction > 0:
                    self.dog_anim.face(RIGHT)
                elif direction < 0:
                    self.dog_anim.face(LEFT)
            if night_dead==True:
                pass
            elif abs(self.x-x)<=40:
                self.walking=True
                self.dog_anim=self.dog_stand
            elif self.walking==True:
                self.follow()
            if 13530>self.x>13440:
                self.walking=False
                self.dog_anim=self.dog_stand
                self.returned=True
                dog_done=True



        def die(self):
            global dog_dead
            dog_dead=True
            self.dog_anim=self.dog_die
            self.dead=True

        def collide(self, x, y):
            return self.x + 156 > x > self.x

        def follow(self):
            global dog_walk
            dog_walk=True
            self.dog_anim=self.dog_walk
            self.x+=self.vx

    class Boy():
        def __init__(self,x,y):
            self.x=x
            self.y=y
            self.dead=False
            self.boy_idle=Anim(lambda _:"Boy Idle.png",1,10,(130,165),True,1)
            self.boy_idle.load()
            self.boy_dead=Anim("Boy Dead %02d.png".__mod__,1,10,(130,165),False,10)
            self.boy_dead.load()
            self.boy_anim=self.boy_idle

        def draw(self):
            global dog_done
            if dog_done==True and self.dead==False:
                game_display.blit(self.boy_anim.next(),(self.x+bx,self.y))
                game_display.blit(boy_message2,(self.x+bx,self.y+30))
                game_display.blit(boy_message,(self.x+bx,self.y+15))
            if dog_done==True and self.dead==True:
                game_display.blit(self.boy_anim.next(),(self.x+bx,self.y))

        def collide(self,x,y):
            return self.x + 210 > x > self.x and self.y + 177 > y > self.y


        def die(self):
            global kill_kid
            kill_kid=True
            global score
            score-=1
            self.boy_anim=self.boy_dead
            self.dead=True

    class Knife():
        def __init__(self, vx):
            self.x= x
            self.y=int(ground+65)
            self.vx=vx
            self.angle = 0
            self.vangle = 30
            
        def update(self):
            self.x+=self.vx
            self.angle += self.vangle

            for santa in santas:
                if not santa.dead and santa.collide(self.x, self.y):
                    santa.die()
                    if self in knifes:
                        knifes.remove(self)

            for truck in trucks:
                if not truck.dead and truck.collide(self.x, self.y):
                    if self in knifes:
                        knifes.remove(self)
                    truck.hit()

            for robot in robots:
                if not robot.dead and robot.collide(self.x, self.y):
                    robot.hit(10)
                    if self in knifes:
                        knifes.remove(self)

            for zombie in zombies:
                if not zombie.dead and zombie.collide(self.x, self.y):
                    zombie.hit(10)
                    if self in knifes:
                        knifes.remove(self)

            for night in nights:
                if not night.dead and night.collide(self.x, self.y):
                    night.hit(5)
                    if self in knifes:
                        knifes.remove(self)

            for dog in dogs:
                if not dog.dead and dog.collide(self.x,self.y):
                    dog.die()
                    if self in knifes:
                        knifes.remove(self)

            for boy in boyz:
                if not boy.dead and boy.collide(self.x, self.y):
                    boy.die()
                    if self in knifes:
                        knifes.remove(self)


        def draw(self):
            game_display.blit(pygame.transform.rotate(kunai, self.angle), (self.x + bx, self.y))
            if self.x + bx > display_width:
                knifes.remove(self)
            if self.x + bx < 0:
                knifes.remove(self)
            if self.y < 0:
                knifes.remove(self)
            if self.y > display_height:
                knifes.remove(self)


    x= display_width//2 - 60
    ground = (display_height * 0.6)
    y = ground - 10
    x_change = 0
    y_change = 0
    gravity = 1
    jump_speed = 15
    img_width = 1600
    img_facing_right = True
    bx=0
    ix=0
    knifes=[]
    bullets=[]
    wheels=[Wheel(12150,ground+110), Wheel(12250,ground+110)]
    trucks=[Truck(9000,ground+20)]
    santas=[Santa(2700,ground+20)]
    robots=[Robot(12000,ground+20, santas[0])]
    presents=[Presents(random.randint(4000,5000),ground+20, santas[0]),Presents(random.randint(5000,6000),ground+20, santas[0]),Presents(7000, ground+20, santas [0])]
    nights=[Night(13500,ground+20,0)]
    zombies=[Zombie(15000,ground+15, 0), Zombie(15100,ground+15, 0), Zombie(15200,ground+15, 0), Zombie(15400,ground+15, 0), Zombie(15500,ground+15, 0), Zombie(15600,ground+15, 0), Zombie(15700,ground+15, 0), Zombie(15800,ground+15, 0)]
    dogs=[Dog(15300,ground+90, 0)]
    boyz=[Boy(15200,ground+35)]


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_LEFT:
                    ninja_anim=ninja_run
                    ninja_anim.face(LEFT)
                    img_facing_right=False
                    x_change = 10
                elif event.key==pygame.K_RIGHT:
                    ninja_anim=ninja_run
                    ninja_anim.face(RIGHT)
                    img_facing_right=True
                    x_change = -10
                elif event.key == pygame.K_UP and y >= ground and jump==True:
                    y_change -= jump_speed
                    old_dir = ninja_anim.dir
                    ninja_anim=ninja_jump
                    ninja_anim.face(old_dir)
                    ninja_anim.reset()
                elif event.key == pygame.K_DOWN and y >=ground and jump==True:
                    ninja_anim=ninja_slide
                    if img_facing_right==True:
                        ninja_anim.face(RIGHT)
                    elif img_facing_right==False:
                        ninja_anim.face(LEFT)
                elif event.key == pygame.K_SPACE and y >= ground:
                    knifes.append(Knife(20 if img_facing_right else -20))
                    old_dir = ninja_anim.dir
                    ninja_anim = ninja_throw
                    ninja_anim.face(old_dir)
                    ninja_anim.reset()
            if event.type==pygame.KEYUP:
                if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                    x_change = 0
                    ninja_anim=ninja_stand
                    if img_facing_right==True:
                        ninja_anim.face(RIGHT)
                    elif img_facing_right==False:
                        ninja_anim.face(LEFT)
                    ninja_anim.reset()
                elif event.key==pygame.K_DOWN:
                    if x_change !=0:
                        ninja_anim=ninja_run
                    elif x_change == 0:
                        ninja_anim=ninja_stand
                    ninja_anim.reset()
        x -= x_change
        bx+=x_change
        ix+=x_change
        if y < ground:
           y_change += gravity
        y += y_change
        if y > ground:
            y = ground
            y_change = 0
            if x_change !=0:
                ninja_anim=ninja_run
            elif x_change==0:
                ninja_anim=ninja_stand
            ninja_anim.reset()
        game_display.fill([255,255,255])
        if ix < -1600 or ix > 1600:
            ix = 0
        game_display.blit(forest_img, (ix,0))
        game_display.blit(forest_img, (ix+1600,0))
        game_display.blit(forest_img, (ix-1600,0))
        game_display.blit(label2, (360+bx,455))
        game_display.blit(label3, (1100+bx,455))
        game_display.blit(label_1, (-1200+bx,455))

        for present in presents:
            present.draw()
            present.collide(x,y)

        for knife in knifes:
            knife.draw()
            knife.update()

        for santa in santas:
            santa.draw()

        for truck in trucks:
            truck.draw()

        for wheel in wheels:
            wheel.draw()
            wheel.collide(x,y)

        for robot in robots:
            robot.update()
            robot.draw()

        for bullet in bullets:
            bullet.update()
            bullet.draw()
            bullet.collide(x,y)

        for night in nights:
            night.draw()
            night.update()

        for zombie in zombies:
            zombie.update()
            zombie.draw()

        for dog in dogs:
            dog.draw()
            dog.update()
            dog.collide(x,y)

        for boy in boyz:
            boy.draw()
            boy.collide(x,y)

        if health<=0:
            ninja_anim=ninja_dead

        if new_health<=0:
            ninja_anim=ninja_dead

        if x>16300:
            end()

            
        ninja(x+bx, y+15)
        print(score)
        global x_change
        pygame.display.update()
        clock.tick(60)

menu()

This snippet took 0.07 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).