Exemplo n.º 1
0
def game(request):
    g=Game()
    t=''
    player = request.user.userprofile
    #Can initialise these using player state !!??
    pps = pickle.dumps(g.player_state)
    kills = 0
    days = 0
    food = 3
    ammo = 2
    people = 1
    g.player_state = pickle.loads(pps)


    context_dict = {'player':player, 'game_over':False, 'new_day':False, 'kills':kills, 'food':food, 'days':days, 'ammo': ammo, 'party': people,
                    'player_state': g.player_state }

    if g.is_game_over():
        context_dict['game_over'] = True
    else:
        g.start_new_day()

    if g.is_day_over():
        g.end_day()
        g.start_new_day()

    if context_dict['game_over'] == False and context_dict['new_day'] == False:
        if t == 'MOVE':
            g.take_turn('MOVE')
        elif t == ('ENTER'):
            g.take_turn('ENTER')
        elif t == ('WAIT'):
            g.take_turn('WAIT')
        elif t == ('FIGHT'):
            g.take_turn('FIGHT')
        elif t == ('SEARCH'):
            g.take_turn('SEARCH')
        elif t == ('EXIT'):
            g.take_turn('EXIT')
        elif t ==('RUN'):
            g.take_turn('RUN')

        context_dict = fill_dict(g)
        context_dict['state'] = str(g.player_state)
        context_dict['gstate'] = g.game_state
        context_dict['time'] = g.time_left

    if g.is_game_over():
        context_dict={'game_over':True}
    elif g.is_day_over():
        context_dict={'new_day':True}
        if g.update_state.party<0:
            print "You lost: {0} people".format(abs(g.update_state.party))

        elif g.update_state.party>0:
            print "{0} more people have joined your party".format(g.update_state.party)

        elif g.update_state.ammo > 0:
            print "You found: {0} units of ammo".format(g.update_state.ammo)

        elif g.update_state.ammo < 0:
            print "You used: {0} units of ammo".format(abs(g.update_state.ammo))

        elif g.update_state.food > 0:
            print "You found: {0} units of food".format(g.update_state.food)

        elif g.update_state.food < 0:
            print "You used: {0} units of food".format(abs(g.update_state.food))

        elif g.update_state.kills > 0:
            print "You killed: {0} zombies".format(g.update_state.kills)

        elif g.update_state.days > 0:
            print "New Day: You survived another day!"
    #Put these updates into a dictionary Q pickle ?

    return render(request, 'zombieGame/game.html', context_dict)
Exemplo n.º 2
0
def game(request):
    if request.POST.get("is_new_game") == "yes":
        g = Game()
    else:
        if not os.path.isfile('gameData/'+request.user.username+'.txt'): #new game
            g = Game()
        else: #continue game
            f = open('gameData/'+request.user.username+'.txt', 'rb')
            g = dill.load(f)
            f.close()
    
    
    if g.game_state is None: #game is uninitialised
        g.start_new_day()
    
    #update max_party
    if g.update_state.party >0:
        g.player_state.max_party += g.update_state.party
    
    new_day = False
    
    if g.is_day_over():
        new_day = True
        g.start_new_day()
    
    if request.method == 'POST':
        print "-------------------------"
        print request.POST.get('action')
        print request.POST.get('pos')
        print "-------------------------"
        action = request.POST.get('action')
        if action == 'MOVEENTER':
            pos = request.POST.get('pos')
            g.take_turn('MOVE', int(pos))
            g.take_turn('ENTER')
        elif action == 'SEARCH':
            pos = request.POST.get('pos')
            g.take_turn(action, int(pos))
        else:
            g.take_turn(action)
    
    if g.is_game_over():
        days_survived = g.player_state.days
        zombie_kills = g.player_state.kills
        most_survivors = g.player_state.max_party
        user_prof = UserProfile.objects.get(user=request.user)
        
        score = Score.objects.create(user=user_prof, zombie_kills=zombie_kills, most_survivors=most_survivors, days_survived=days_survived)
        score.save()
        
        os.remove('gameData/'+request.user.username+'.txt')
        
        return render(request, "zombie/gamedeath.html", {'days_survived':days_survived, 'zombie_kills':zombie_kills, 'most_survivors':most_survivors})
    
    f = open('gameData/'+request.user.username+'.txt', 'wb')
    dill.dump(g, f)
    f.close()
    
    t = str(int(((84-g.time_left)/6)+10))+":"+str(int((84-g.time_left)%6))+"0";
    
    return render(request, 'zombie/game.html', {'game':g, 'time':t, 'new_day':new_day})