├── evoman
├── __init__.py
├── Base
│ ├── __init__.py
│ ├── SpriteConstants.py
│ └── SpriteDefinition.py
├── images
│ ├── bb.jpeg
│ ├── bubb.png
│ ├── met.png
│ ├── met2.png
│ ├── mi2.png
│ ├── tex2.jpg
│ ├── tex3.jpg
│ ├── tex4.jpg
│ ├── blade.png
│ ├── images.jpg
│ ├── roles.png
│ ├── torna.png
│ ├── torna2.png
│ ├── wall1.jpg
│ ├── wall2.jpg
│ ├── water2.jpg
│ ├── bullet2_l.png
│ ├── bullet2_r.png
│ ├── bullet_l.png
│ ├── bullet_r.png
│ ├── download.jpg
│ ├── fireball.png
│ ├── fireball2.png
│ ├── plataform1.jpg
│ ├── plataform2.jpg
│ ├── EnemySprites.png
│ └── EvoManSprites.png
├── sounds
│ ├── open.wav
│ ├── scifi003.wav
│ └── scifi011.wav
├── roles.tsx
├── roles4.tsx
├── README.md
├── controller.py
├── roles2.tsx
├── roles3.tsx
├── sensors.py
├── map3.tmx
├── map2.tmx
├── map4.tmx
├── map1.tmx
├── enemy6.py
├── enemy8.py
├── enemy2.py
├── enemy1.py
├── enemy4.py
├── enemy5.py
├── enemy3.py
└── player.py
├── co_evolution.pdf
├── evoman1.0-doc.pdf
├── multi_evolution.pdf
├── README.md
├── .idea
└── vcs.xml
├── loop.sh
├── dummy_demo.py
├── human_demo.py
├── controller_generalist_demo.py
├── controller_specialist_demo.py
├── optimization_dummy.py
├── solutions_demo
├── demo_4.txt
├── demo_all.txt
├── demo_5.txt
├── demo_6.txt
├── demo_7.txt
├── demo_2.txt
├── demo_8.txt
├── demo_1.txt
└── demo_3.txt
├── .gitignore
├── demo_controller.py
├── optimization_coevolution_demo.py
├── competition_results_v2.py
├── optimization_generalist_demo.py
└── optimization_specialist_demo.py
/evoman/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/evoman/Base/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/co_evolution.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/co_evolution.pdf
--------------------------------------------------------------------------------
/evoman1.0-doc.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman1.0-doc.pdf
--------------------------------------------------------------------------------
/multi_evolution.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/multi_evolution.pdf
--------------------------------------------------------------------------------
/evoman/images/bb.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bb.jpeg
--------------------------------------------------------------------------------
/evoman/images/bubb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bubb.png
--------------------------------------------------------------------------------
/evoman/images/met.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/met.png
--------------------------------------------------------------------------------
/evoman/images/met2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/met2.png
--------------------------------------------------------------------------------
/evoman/images/mi2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/mi2.png
--------------------------------------------------------------------------------
/evoman/images/tex2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/tex2.jpg
--------------------------------------------------------------------------------
/evoman/images/tex3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/tex3.jpg
--------------------------------------------------------------------------------
/evoman/images/tex4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/tex4.jpg
--------------------------------------------------------------------------------
/evoman/sounds/open.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/sounds/open.wav
--------------------------------------------------------------------------------
/evoman/images/blade.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/blade.png
--------------------------------------------------------------------------------
/evoman/images/images.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/images.jpg
--------------------------------------------------------------------------------
/evoman/images/roles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/roles.png
--------------------------------------------------------------------------------
/evoman/images/torna.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/torna.png
--------------------------------------------------------------------------------
/evoman/images/torna2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/torna2.png
--------------------------------------------------------------------------------
/evoman/images/wall1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/wall1.jpg
--------------------------------------------------------------------------------
/evoman/images/wall2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/wall2.jpg
--------------------------------------------------------------------------------
/evoman/images/water2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/water2.jpg
--------------------------------------------------------------------------------
/evoman/images/bullet2_l.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bullet2_l.png
--------------------------------------------------------------------------------
/evoman/images/bullet2_r.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bullet2_r.png
--------------------------------------------------------------------------------
/evoman/images/bullet_l.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bullet_l.png
--------------------------------------------------------------------------------
/evoman/images/bullet_r.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/bullet_r.png
--------------------------------------------------------------------------------
/evoman/images/download.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/download.jpg
--------------------------------------------------------------------------------
/evoman/images/fireball.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/fireball.png
--------------------------------------------------------------------------------
/evoman/images/fireball2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/fireball2.png
--------------------------------------------------------------------------------
/evoman/sounds/scifi003.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/sounds/scifi003.wav
--------------------------------------------------------------------------------
/evoman/sounds/scifi011.wav:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/sounds/scifi011.wav
--------------------------------------------------------------------------------
/evoman/images/plataform1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/plataform1.jpg
--------------------------------------------------------------------------------
/evoman/images/plataform2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/plataform2.jpg
--------------------------------------------------------------------------------
/evoman/images/EnemySprites.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/EnemySprites.png
--------------------------------------------------------------------------------
/evoman/images/EvoManSprites.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/karinemiras/evoman_framework/HEAD/evoman/images/EvoManSprites.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Evoman is a video game playing framework to be used as a testbed for optimization algorithms.
2 |
3 | A demo can be found here: https://www.youtube.com/watch?v=ZqaMjd1E4ZI
4 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/evoman/roles.tsx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/loop.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | experiment_name='individual_demo'
4 |
5 | while [ ! -f $experiment_name"/neuroended" ]
6 | do
7 | python optimization_specialist_demo.py
8 | done
9 |
10 | exit 0
11 |
12 |
--------------------------------------------------------------------------------
/evoman/roles4.tsx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/evoman/Base/SpriteConstants.py:
--------------------------------------------------------------------------------
1 |
2 | #Colors
3 | BLACK = (0, 0, 0)
4 |
5 | #Directions
6 | RIGHT = 1
7 | LEFT = 0
8 |
9 | #Movements types
10 | STANDING = 0
11 |
12 | START_RUNNING = 1
13 |
14 | RUNNING_STEP1 = 2
15 | RUNNING_STEP2 = 3
16 |
17 | JUMPING = 4
18 |
19 | SHOOTING = 6
20 | SHOOTING_JUMPING = 5
21 |
22 | HURTING = 7
23 |
24 |
--------------------------------------------------------------------------------
/evoman/README.md:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | EvoMan is a framework for testing optimization algorithms in General Video Game Playing research field. The environment provides multiple 2D platform games to be run in several parameterized simulation modes.
8 |
9 | Needs: numpy and pygame
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/dummy_demo.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | # imports framework
8 | import sys, os
9 |
10 | from evoman.environment import Environment
11 |
12 | experiment_name = 'dummy_demo'
13 | if not os.path.exists(experiment_name):
14 | os.makedirs(experiment_name)
15 |
16 | # initializes environment with ai player using random controller, playing against static enemy
17 | env = Environment(experiment_name=experiment_name)
18 | env.play()
19 |
20 |
--------------------------------------------------------------------------------
/evoman/controller.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import numpy
8 |
9 | class Controller(object):
10 |
11 | def set(self, genome, n_inputs):
12 | pass
13 |
14 | def control(self, params, cont = None):
15 |
16 | left = numpy.random.choice([1,0])
17 | right = numpy.random.choice([1,0])
18 | jump = numpy.random.choice([1,0])
19 | shoot = numpy.random.choice([1,0])
20 | release = numpy.random.choice([1,0])
21 |
22 | return [left, right, jump, shoot, release]
23 |
--------------------------------------------------------------------------------
/human_demo.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | # imports framework
8 | import sys, os
9 |
10 | from evoman.environment import Environment
11 |
12 | experiment_name = 'test'
13 |
14 | if not os.path.exists(experiment_name):
15 | os.makedirs(experiment_name)
16 |
17 | # initializes environment with human player and static enemies
18 | for en in range(1, 9):
19 | env = Environment(experiment_name=experiment_name,
20 | enemymode='static',
21 | speed="normal",
22 | sound="on",
23 | fullscreen=True,
24 | use_joystick=True,
25 | playermode='human',
26 | visuals=True)
27 | env.update_parameter('enemies', [en])
28 | env.play()
29 |
30 |
--------------------------------------------------------------------------------
/evoman/Base/SpriteDefinition.py:
--------------------------------------------------------------------------------
1 | import pygame
2 |
3 | from . import SpriteConstants
4 |
5 |
6 | class SpriteDefinition(object):
7 | """Contains the properties and methods to control a SpriteSheet structure"""
8 |
9 | def __init__(self, fileName, origin_X, origin_Y, width, height):
10 | self.SpriteSheet = pygame.image.load(fileName).convert()
11 | self.Origin_X = origin_X
12 | self.Origin_Y = origin_Y
13 | self.Width = width
14 | self.Height = height
15 |
16 | def getImage(self, steps_X, steps_Y):
17 | marginX = self.Width * steps_X
18 | marginY = self.Height * steps_Y
19 |
20 | image = pygame.Surface([self.Width, self.Height]).convert()
21 |
22 | image.blit(self.SpriteSheet,
23 | (0, 0),
24 | (marginX,
25 | marginY,
26 | self.Width,
27 | self.Height))
28 |
29 | image.set_colorkey(SpriteConstants.BLACK)
30 |
31 | return image
32 |
--------------------------------------------------------------------------------
/controller_generalist_demo.py:
--------------------------------------------------------------------------------
1 | #######################################################################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # DEMO : perceptron neural network controller evolved by Genetic Algorithm. #
4 | # general solution for enemies (games) #
5 | # Author: Karine Miras #
6 | # karine.smiras@gmail.com #
7 | #######################################################################################
8 |
9 | # imports framework
10 | import sys,os
11 |
12 | from evoman.environment import Environment
13 | from demo_controller import player_controller
14 |
15 | # imports other libs
16 | import numpy as np
17 |
18 | experiment_name = 'controller_generalist_demo'
19 | if not os.path.exists(experiment_name):
20 | os.makedirs(experiment_name)
21 |
22 | # Update the number of neurons for this specific example
23 | n_hidden_neurons = 0
24 |
25 | # initializes environment for multi objetive mode (generalist) with static enemy and ai player
26 | env = Environment(experiment_name=experiment_name,
27 | playermode="ai",
28 | player_controller=player_controller(n_hidden_neurons),
29 | speed="normal",
30 | enemymode="static",
31 | level=2)
32 |
33 | sol = np.loadtxt('solutions_demo/demo_all.txt')
34 | print('\n LOADING SAVED GENERALIST SOLUTION FOR ALL ENEMIES \n')
35 |
36 | # tests saved demo solutions for each enemy
37 | for en in range(1, 9):
38 |
39 | #Update the enemy
40 | env.update_parameter('enemies',[en])
41 |
42 | env.play(sol)
43 |
44 | print('\n \n')
45 |
--------------------------------------------------------------------------------
/controller_specialist_demo.py:
--------------------------------------------------------------------------------
1 | #######################################################################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # DEMO : perceptron neural network controller evolved by Genetic Algorithm. #
4 | # specialist solutions for each enemy (game) #
5 | # Author: Karine Miras #
6 | # karine.smiras@gmail.com #
7 | #######################################################################################
8 |
9 | # imports framework
10 | import sys, os
11 |
12 | from evoman.environment import Environment
13 | from demo_controller import player_controller
14 |
15 | # imports other libs
16 | import numpy as np
17 |
18 | experiment_name = 'controller_specialist_demo'
19 | if not os.path.exists(experiment_name):
20 | os.makedirs(experiment_name)
21 |
22 | # Update the number of neurons for this specific example
23 | n_hidden_neurons = 0
24 |
25 | # initializes environment for single objective mode (specialist) with static enemy and ai player
26 | env = Environment(experiment_name=experiment_name,
27 | playermode="ai",
28 | player_controller=player_controller(n_hidden_neurons),
29 | speed="normal",
30 | enemymode="static",
31 | level=2,
32 | visuals=True)
33 |
34 |
35 | # tests saved demo solutions for each enemy
36 | for en in range(1, 9):
37 |
38 | #Update the enemy
39 | env.update_parameter('enemies',[en])
40 |
41 | # Load specialist controller
42 | sol = np.loadtxt('solutions_demo/demo_'+str(en)+'.txt')
43 | print('\n LOADING SAVED SPECIALIST SOLUTION FOR ENEMY '+str(en)+' \n')
44 | env.play(sol)
45 |
--------------------------------------------------------------------------------
/evoman/roles2.tsx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/evoman/roles3.tsx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/optimization_dummy.py:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # DEMO : Neuroevolution - Genetic Algorithm neural network. #
4 | # Author: Karine Miras #
5 | # karine.smiras@gmail.com #
6 | ###############################################################################
7 |
8 | # imports framework
9 | import sys
10 |
11 | from evoman.environment import Environment
12 | from demo_controller import player_controller
13 |
14 | # imports other libs
15 | import numpy as np
16 | import os
17 |
18 | # runs simulation
19 | def simulation(env,x):
20 | f,p,e,t = env.play(pcont=x)
21 | return f
22 |
23 | # evaluation
24 | def evaluate(env, x):
25 | return np.array(list(map(lambda y: simulation(env,y), x)))
26 |
27 |
28 | def main():
29 | # choose this for not using visuals and thus making experiments faster
30 | headless = True
31 | if headless:
32 | os.environ["SDL_VIDEODRIVER"] = "dummy"
33 |
34 |
35 | experiment_name = 'optimization_test'
36 | if not os.path.exists(experiment_name):
37 | os.makedirs(experiment_name)
38 |
39 | n_hidden_neurons = 10
40 |
41 | # initializes simulation in individual evolution mode, for single static enemy.
42 | env = Environment(experiment_name=experiment_name,
43 | enemies=[2],
44 | playermode="ai",
45 | player_controller=player_controller(n_hidden_neurons), # you can insert your own controller here
46 | enemymode="static",
47 | level=2,
48 | speed="fastest",
49 | visuals=False)
50 |
51 |
52 | # number of weights for multilayer with 10 hidden neurons
53 | n_vars = (env.get_num_sensors()+1)*n_hidden_neurons + (n_hidden_neurons+1)*5
54 |
55 | # start writing your own code from here
56 |
57 |
58 |
59 | if __name__ == '__main__':
60 | main()
--------------------------------------------------------------------------------
/evoman/sensors.py:
--------------------------------------------------------------------------------
1 | import numpy
2 | import struct
3 | import binascii
4 |
5 | # sensors for the controllers
6 | class Sensors():
7 |
8 |
9 | def get(self, game):
10 |
11 |
12 | # calculates vertical and horizontal distances between sprites centers
13 |
14 | posx_p = game.player.rect.left +((game.player.rect.right - game.player.rect.left)/2)
15 | posy_p = game.player.rect.bottom +((game.player.rect.top - game.player.rect.bottom)/2)
16 | posx_e = game.enemy.rect.left +((game.enemy.rect.right - game.enemy.rect.left)/2)
17 | posy_e = game.enemy.rect.bottom +((game.enemy.rect.top - game.enemy.rect.bottom)/2)
18 |
19 | # pre-allocate values for the bullets
20 | param_values = [ posx_p-posx_e, posy_p-posy_e, game.player.direction, game.enemy.direction] + [0]*16
21 |
22 | # calculates vertical and horizontal distances between player and the center of enemy's bullets
23 | bullet_count = 0
24 | for i in range(0,len(game.enemy.twists)):
25 | if game.enemy.twists[i] != None:
26 | posx_be = game.enemy.twists[i].rect.left +((game.enemy.twists[i].rect.right - game.enemy.twists[i].rect.left)/2)
27 | posy_be = game.enemy.twists[i].rect.bottom +((game.enemy.twists[i].rect.top - game.enemy.twists[i].rect.bottom)/2)
28 | param_values[4 + bullet_count * 2] = posx_p - posx_be
29 | param_values[4 + bullet_count * 2 + 1] = posy_p - posy_be
30 | bullet_count+=1
31 |
32 |
33 | # applies several transformations to input variables (sensors)
34 | if game.inputscoded == "yes":
35 |
36 | types = struct.Struct('q q q q q q q q q q q q q q q q q q q q') # defines the data types of each item of the array that will be packed. (q=int, f=flo)
37 | packed_data = types.pack(*param_values) # packs data as struct
38 | coded_variables = binascii.hexlify(packed_data) # converts packed data to an hexadecimal string
39 | coded_variables = [coded_variables[i:i+2] for i in range(0, len(coded_variables), 2)] # breaks hexadecimal string in bytes.
40 | coded_variables = numpy.array(map(lambda y: int(y, 16), coded_variables)) # converts bytes to integer
41 |
42 | param_values = coded_variables
43 |
44 |
45 | self.sensors = param_values # defines sensors state
46 |
47 |
48 | return numpy.array(self.sensors)
49 |
--------------------------------------------------------------------------------
/evoman/map3.tmx:
--------------------------------------------------------------------------------
1 |
2 |
70 |
--------------------------------------------------------------------------------
/solutions_demo/demo_4.txt:
--------------------------------------------------------------------------------
1 | -2.335073215241207922e-01
2 | -3.269523114297601118e-01
3 | -4.489887641351597569e-01
4 | 6.646502835925969244e-01
5 | -1.267248686507127520e-01
6 | 5.712602224987395338e-01
7 | -8.207548946574130255e-01
8 | 7.576276926725800109e-01
9 | -1.786865554539971424e-01
10 | -2.645032521108574564e-01
11 | 1.747228650397767424e-01
12 | 5.775595130380848374e-01
13 | -9.088601870163095509e-01
14 | -3.227107391539711934e-01
15 | -4.024320653196098441e-01
16 | 2.813125734425565572e-01
17 | 3.195621066971547908e-01
18 | -8.489397684933235322e-01
19 | 4.148725400069055436e-01
20 | 7.152304736057191192e-01
21 | -1.619832548353250523e-01
22 | 2.568755992916957887e-01
23 | 9.493883537893006430e-01
24 | 5.962846658706544289e-01
25 | 5.637233161638397494e-01
26 | -4.587142562135346768e-01
27 | -7.136249295668308790e-01
28 | 8.776545458635729435e-01
29 | 7.003445959136134213e-01
30 | 1.251831365398330398e-01
31 | -3.724337068039054177e-01
32 | 2.927570159316472687e-01
33 | 4.356182544210360774e-01
34 | 7.697193192292552943e-01
35 | 6.605653071776247387e-01
36 | -8.212558358369601574e-01
37 | 7.331810553551032505e-02
38 | -2.821537067218344852e-01
39 | -3.734444161902894521e-01
40 | -6.860499243693429783e-01
41 | -5.816785527458885330e-01
42 | 6.178055722230408886e-01
43 | 7.016175740842167663e-02
44 | 3.381503533998380107e-01
45 | -9.816993470354735685e-01
46 | -2.115542774309320251e-02
47 | 3.937764101385370363e-01
48 | -4.580726310074145768e-01
49 | 3.045077679123648906e-01
50 | -6.583344755717792651e-01
51 | 4.381756510383281178e-01
52 | 7.644735778259601489e-01
53 | 9.758085742787734329e-01
54 | 2.344229729720004318e-01
55 | 3.370328793838854864e-01
56 | 3.906669261619952760e-01
57 | 2.023100288248960155e-01
58 | -2.791512381199747761e-01
59 | -9.670307853094555206e-01
60 | -6.724307637665025705e-01
61 | 9.380554921915869038e-01
62 | 1.076738338010267970e-01
63 | 3.539340246961919512e-02
64 | -3.499399408126737665e-01
65 | 9.200349727678265310e-01
66 | 1.976408038793064925e-01
67 | -1.652716695593174911e-01
68 | 4.082781140515256979e-01
69 | -9.814863002211300369e-01
70 | -3.974409276545287639e-02
71 | -8.747615432220736675e-01
72 | 6.805297690719411818e-01
73 | 3.608491175237706905e-01
74 | 9.344932258192983898e-01
75 | 4.307192076835293371e-01
76 | 5.802187272885563907e-01
77 | 3.878639631036946422e-01
78 | 7.387108451150090627e-01
79 | 8.674309024472475205e-01
80 | 3.543064853750577115e-02
81 | -2.635429606854182705e-01
82 | 7.980317565957435466e-01
83 | -4.011505531629642185e-01
84 | 7.125373403098349545e-01
85 | -4.736466926401223798e-01
86 | 6.789411267537948103e-01
87 | 5.816318781186247122e-01
88 | 9.556853328294312444e-01
89 | 6.439779293178959918e-01
90 | -2.394027422023268237e-01
91 | -8.037543832638311958e-01
92 | -3.204877127832523875e-01
93 | 4.854195540704309142e-01
94 | 8.376685685244869006e-01
95 | -9.733052220931290854e-01
96 | -1.096242039954173908e-01
97 | -8.528340664281015560e-01
98 | 9.566094111132543709e-01
99 | 1.442475690777107467e-01
100 | -9.096470195282413407e-01
101 | 2.502512666061569302e-01
102 | -5.637992677760119697e-01
103 | -1.601778063334873270e-01
104 | -2.820012228710673252e-01
105 | -6.662039174007452491e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_all.txt:
--------------------------------------------------------------------------------
1 | -4.305198696724163998e-01
2 | 1.839929042032597550e-01
3 | -9.116733312535161282e-01
4 | 1.782694406869373971e-01
5 | 3.593287183084019620e-01
6 | 9.983580423854088171e-01
7 | -7.853121183587241649e-01
8 | 5.741671757950901345e-01
9 | 9.262942267618945458e-01
10 | 6.291099066499228343e-01
11 | -6.850101769834040510e-01
12 | 1.652749963848942905e-02
13 | 3.992203199649695389e-01
14 | 9.321465461476088343e-01
15 | -5.355370492712014929e-01
16 | -3.872228223919498280e-01
17 | -8.363471288230481804e-01
18 | -4.486144259100577347e-01
19 | 1.710869735220164056e-01
20 | -8.416284545133432271e-01
21 | 9.839648166923908867e-01
22 | 1.337320001564084038e-01
23 | -7.246958538362653357e-01
24 | 2.757063756201609817e-01
25 | -3.907234586272527732e-01
26 | -7.176966154668722453e-01
27 | -3.034641750189916998e-01
28 | 4.810198435301313857e-01
29 | 3.506075661519902908e-01
30 | 1.878959023729693811e-01
31 | 8.420184321022554652e-01
32 | 2.928328371663326823e-01
33 | -4.722297532709185397e-02
34 | 6.904909040648521401e-01
35 | 3.893646626008817613e-02
36 | -3.247333507818610521e-01
37 | -4.661041773072109873e-01
38 | -7.625372129432794743e-01
39 | -5.973452868827129336e-01
40 | 6.133561993145559832e-01
41 | -8.687904107207091720e-01
42 | 8.193644820183703104e-01
43 | -2.051620460756329312e-01
44 | 9.768201265205713746e-01
45 | -7.330690934806971537e-01
46 | -1.130294695899638491e-01
47 | 2.310440326517075871e-01
48 | 7.510275310201448562e-01
49 | -7.872602553266976333e-01
50 | 8.625878996771723095e-01
51 | -2.811863244665773198e-01
52 | -9.051731420423612118e-01
53 | -4.008793691178134111e-01
54 | -3.721231186293609827e-01
55 | -6.826160048787766055e-01
56 | 8.540114883351854758e-01
57 | 4.543898472882361705e-01
58 | 4.796328591373004713e-01
59 | -7.522536568221247499e-01
60 | 2.372971002174970589e-01
61 | 5.214600874195696179e-01
62 | 2.742389211916247405e-01
63 | -5.609823281028807340e-01
64 | -2.317248806521110538e-02
65 | 2.842923160803996208e-01
66 | -9.019761445083458451e-01
67 | -5.209465861484792182e-01
68 | 3.573760343570786535e-01
69 | 2.330616878240927559e-01
70 | 1.059091945373458188e-03
71 | -2.599261949876336963e-01
72 | 5.073429623981506875e-01
73 | 7.867993667539081581e-01
74 | 6.228123680159964870e-01
75 | 6.845747806306445860e-02
76 | -1.410502806538371789e-01
77 | 1.014990996569844484e-01
78 | 9.385324587613093072e-01
79 | -1.248403780394085061e-01
80 | 6.943291068084394002e-01
81 | -7.700720708816084503e-01
82 | 3.620565687394039500e-02
83 | 7.906438234271344623e-01
84 | 4.606705687128767046e-01
85 | 5.522154750134715684e-01
86 | -1.165179475962554445e-01
87 | 3.033963001112471858e-01
88 | 1.499710640813514129e-01
89 | 9.983674069230474846e-01
90 | 4.415412438389589855e-01
91 | -9.523091527993305938e-01
92 | 4.721522774096298392e-01
93 | -9.506284939022768832e-02
94 | 5.982213321414484586e-01
95 | 5.968410216416306646e-01
96 | 7.405052213129392769e-01
97 | -7.675692838099665849e-01
98 | 9.645917449846572733e-01
99 | 4.721763638875866587e-01
100 | -2.163752358335713222e-01
101 | 5.943331909914726374e-01
102 | 5.398873217506163780e-01
103 | 5.484119234595616899e-01
104 | -5.458521571035002129e-01
105 | 1.856350093319645289e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_5.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | -4.293571111671073970e-01
3 | 1.312429579262228974e-01
4 | 2.440981550621654228e-01
5 | -1.195700016460885806e-01
6 | 2.765798646415129625e-01
7 | 2.251538993775406006e-01
8 | 2.400847125288060047e-01
9 | 2.374061284172892528e-01
10 | 2.986744652742503447e-01
11 | -7.112921741298985912e-02
12 | -7.367213855461092475e-02
13 | 3.303404702695609263e-01
14 | 3.223468511346369025e-01
15 | -3.504139224120854101e-01
16 | -1.133791618007970597e-01
17 | 1.127942587639835237e-02
18 | 1.800067412120630594e-01
19 | 4.875529201724828482e-02
20 | 2.222426379271925101e-01
21 | 2.599023736431735898e-01
22 | 4.508078080257441611e-01
23 | 2.561405931112156198e-01
24 | 2.949351065147950424e-01
25 | -9.263843421718356841e-02
26 | 2.420668617913982190e-01
27 | -1.723882827130810702e-01
28 | -1.990775154648552481e-01
29 | 3.989102305036896778e-01
30 | -4.461073121398195229e-01
31 | -4.635944848591702083e-01
32 | 9.042528790757090040e-02
33 | -3.246663971962858719e-01
34 | 1.880410039465672534e-01
35 | -4.021576666758988061e-01
36 | -4.228405554339519368e-01
37 | 6.722345402054425989e-01
38 | 1.088616140419058215e-01
39 | 1.970344634287687566e-02
40 | -4.012188602611651556e-01
41 | -1.605026063107814829e-03
42 | -4.858270030382627613e-01
43 | -3.168487000227470096e-01
44 | 1.805758357225519806e-01
45 | 4.227150719317843142e-01
46 | 1.858894985673232303e-01
47 | 2.908426131309240215e-01
48 | 3.049995077514062736e-01
49 | -1.115694530352583924e-01
50 | 3.333303482257791162e-01
51 | 3.744049823152298950e-01
52 | -1.859551153938737433e-01
53 | -1.710302297171888974e-01
54 | 1.276124527734868314e-02
55 | -6.030492214386597993e-01
56 | 1.001155023416989143e-01
57 | -2.242948104781200269e-01
58 | 3.004456594676555570e-01
59 | -2.763035637901117836e-01
60 | 2.751142588017510549e-01
61 | 4.123141543220728567e-01
62 | -5.036290215510423884e-01
63 | 1.271440325907207680e-01
64 | -2.389028175798888209e-01
65 | -3.673692368989171786e-02
66 | -1.947133128337003721e-02
67 | -2.525702532858545846e-01
68 | -2.370458742495619008e-03
69 | 1.541991013552501122e-01
70 | 2.068372475813395339e-01
71 | -5.525818661446514346e-01
72 | 4.109093172232699542e-01
73 | -1.877213367819214329e-01
74 | 8.148211845617443772e-02
75 | -5.188792585673226987e-02
76 | 9.838459681128450263e-02
77 | 2.285964199650111772e-01
78 | 1.314347254263153186e-01
79 | 5.204970486523302284e-03
80 | -3.004472511058491335e-01
81 | -3.094000073518277660e-01
82 | 1.405855158107018055e-01
83 | 4.932628885871480628e-01
84 | -2.653244881312990522e-01
85 | 8.029481931213480750e-02
86 | -3.612574021749314496e-03
87 | 2.617693603110875511e-01
88 | 1.495531164564655180e-01
89 | 2.279286925566076649e-01
90 | 1.192524211568060655e-02
91 | -2.419340999889614541e-01
92 | -4.242999030158172147e-01
93 | -7.928097379735150885e-02
94 | -4.614358273854447523e-01
95 | 1.191099316200539548e-01
96 | -1.783383830950480975e-01
97 | -4.847830660615380927e-01
98 | -2.532762997715318765e-01
99 | -1.063742642041850955e-01
100 | 2.330519804376058324e-01
101 | -8.875760935564969145e-02
102 | 3.801028859424788742e-01
103 | -1.956746279525403698e-01
104 | -6.579982795005656637e-02
105 | -9.977949759219865156e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_6.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | -4.698717526598942262e-01
3 | -9.708395502645726649e-02
4 | 1.735677985451941630e-02
5 | 2.887164814879246144e-01
6 | 8.782414726374841024e-03
7 | 3.382008440995863396e-01
8 | 3.841831434601273298e-01
9 | 2.279490052904271902e-04
10 | -1.173466468195162732e-01
11 | -2.393690268620803085e-01
12 | 5.169102019114338864e-01
13 | 3.559329633053420205e-01
14 | 9.823736877512240229e-02
15 | 1.204882491049904109e-01
16 | -5.911592613868127399e-02
17 | -3.018667481098151195e-01
18 | 5.717459984190600419e-01
19 | -9.508751372676110813e-02
20 | 1.624095106474035544e-01
21 | -5.763800880874949545e-02
22 | 2.594086966175377929e-01
23 | 1.574250669546412851e-01
24 | 1.667341652456500178e-01
25 | -8.629352689394736986e-02
26 | 2.938474794490952280e-01
27 | -4.281769965524361288e-01
28 | -8.775337859153500841e-02
29 | -8.419323626364824675e-02
30 | 1.679671998396565225e-01
31 | -2.505824740981710264e-01
32 | -9.173178614526808494e-02
33 | -7.852117087287180386e-01
34 | 2.633485827082538422e-01
35 | -4.185248249634421214e-01
36 | -2.944312890360397672e-01
37 | 1.543072135872461392e-01
38 | 2.319858352561024462e-02
39 | 2.620047179427000827e-01
40 | 7.976651739992887680e-02
41 | 3.239890999728720633e-01
42 | -4.390498429649163925e-01
43 | 5.748814027309223640e-01
44 | 3.722197680794339658e-01
45 | 1.645029837676153517e-01
46 | -1.441013642563483965e-01
47 | 1.132832629205083284e-03
48 | 6.720076930082108380e-01
49 | -1.533159396353225845e-01
50 | -2.935755596995346384e-01
51 | -4.314552835210337944e-01
52 | -3.502331720869050757e-02
53 | 2.022403865031064951e-01
54 | 3.117597503725164643e-01
55 | -6.866547115948797020e-02
56 | -4.534657259450681210e-01
57 | -1.550859932906895611e-01
58 | -1.918734678992845000e-01
59 | -2.907532932620655042e-01
60 | 2.775121949943220589e-01
61 | -1.226136058593562533e-01
62 | 1.945069662864468141e-01
63 | -4.308380416750292596e-01
64 | 2.486187571825139719e-01
65 | 9.250598840730132888e-02
66 | -1.928035740167824419e-01
67 | -2.040222184049018672e-01
68 | -2.529859316312303985e-01
69 | 2.757104948356980945e-01
70 | -6.083644573628907981e-01
71 | 2.151073577236186640e-01
72 | -1.787645137759051039e-01
73 | 4.160796558827288549e-02
74 | 2.434301061812983447e-01
75 | 1.398291216139814150e-02
76 | 6.479145767101465170e-01
77 | 1.095809096698579926e-01
78 | -7.770040437920562593e-02
79 | 2.896598896990623118e-01
80 | 2.398877931909782490e-01
81 | 3.601988937531958035e-01
82 | 2.698907524982989137e-01
83 | -2.534374043860758707e-01
84 | 1.486358040598496078e-01
85 | 5.968899581377153929e-01
86 | 2.863907058432672015e-01
87 | 2.131801400356279075e-01
88 | -3.142270883012929228e-01
89 | -3.377988033848425831e-01
90 | -2.129266822376543189e-01
91 | -3.852856684392850228e-01
92 | 4.040667031064889003e-01
93 | 1.260102166611452579e-01
94 | 1.654052529009484296e-01
95 | -4.950552814509624755e-02
96 | -8.316190593223657390e-02
97 | -4.380926734637567710e-02
98 | 1.899540605318358541e-02
99 | 4.576237133828450210e-01
100 | -3.042364593447599574e-01
101 | -3.313666460389991242e-01
102 | -3.714364981477586425e-01
103 | -2.599602087504853873e-02
104 | 2.856912863315009266e-01
105 | -7.754365013106804261e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_7.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | 5.355361462085145297e-01
3 | 1.814289897976559263e-01
4 | 6.027787735989600382e-01
5 | 2.088682322259752255e-01
6 | 5.560948033945123381e-01
7 | -2.703554015496540952e-01
8 | -5.686039162984646289e-02
9 | -1.311190584045488772e-01
10 | 5.530098399381506991e-02
11 | 2.675928435592269533e-02
12 | -2.026810271246801409e-01
13 | -2.150823160823488456e-01
14 | -2.213020925879408130e-01
15 | -4.669102451115217378e-02
16 | 1.580012565257210289e-01
17 | 2.551569953473408203e-01
18 | 4.383996766250526367e-01
19 | 1.647875839244697382e-01
20 | -2.847668670038636107e-01
21 | -8.487089958115315302e-02
22 | -1.424801339945592493e-01
23 | 3.099984630436457556e-01
24 | -4.519287526875305772e-01
25 | -5.663951485285233234e-02
26 | 5.740437678115739573e-01
27 | -9.896598865423550384e-02
28 | -4.505098556740360499e-02
29 | 4.554513029048009587e-01
30 | -2.334779849055123035e-01
31 | -5.726319933651828897e-01
32 | 9.087013959587272871e-02
33 | -9.399088521508064398e-02
34 | -3.669259953746287772e-01
35 | 6.457834480412978362e-01
36 | -8.196378747906560291e-02
37 | 4.002881105992315280e-01
38 | 2.399536664929246277e-01
39 | 9.650313266552115743e-02
40 | -5.696663803008400473e-01
41 | -4.417740985121423325e-01
42 | -2.582646893510606168e-01
43 | 1.336054030491951039e-01
44 | -1.890703726562155684e-01
45 | 3.387768461587330471e-01
46 | 1.983692134069810764e-01
47 | 2.829031090625633293e-01
48 | 6.694889965696955159e-01
49 | 3.739773419463154891e-01
50 | 9.535069754516832530e-02
51 | 3.774490419845649702e-02
52 | 4.819615723767798482e-02
53 | -3.055991919986111166e-02
54 | 1.309060412441336030e-01
55 | 3.893125909404762108e-01
56 | -1.222109278637436569e-01
57 | -2.499656517107365639e-01
58 | 2.209795968255187648e-02
59 | 1.354384814970962525e-01
60 | 2.886054994620272929e-01
61 | -2.930979842360700860e-01
62 | -4.318725049178649966e-01
63 | -1.021954168423485909e-01
64 | 1.590013869050212747e-01
65 | 4.139773747120125691e-01
66 | 6.325130488522354155e-02
67 | -6.785103668225193063e-01
68 | 3.070460425459897724e-01
69 | -9.486528464255986060e-02
70 | -2.677255876881123875e-01
71 | -4.656014976532086291e-01
72 | -2.159046238152874664e-01
73 | 1.713189330497471019e-01
74 | 5.083504984955369288e-01
75 | 2.032123473760608923e-01
76 | -3.973805569440265195e-01
77 | -1.825232381915113389e-01
78 | -4.374229404698486423e-01
79 | 2.887622172805633380e-01
80 | -3.785127852429899520e-02
81 | 3.096906574221935937e-01
82 | 5.501171292716036110e-01
83 | 1.207242708786182284e-01
84 | -2.680519225887469936e-01
85 | -2.803207397358047359e-01
86 | -2.693631559104243678e-01
87 | -3.820120458491788051e-01
88 | -1.314791371134748088e-01
89 | 4.586015415677893170e-01
90 | 4.984752444296041696e-01
91 | 4.704642776136891413e-01
92 | 1.122229681548426172e-01
93 | 5.568656700285372629e-01
94 | -5.535579168896988256e-02
95 | 1.729429106155225548e-01
96 | -2.757508962256415702e-02
97 | 2.900223923448000396e-01
98 | -7.720633640543976828e-01
99 | 4.838350809776484351e-02
100 | -8.165763061940049805e-02
101 | 1.387431415432536369e-02
102 | 4.623945510676923110e-02
103 | -8.517438015470020496e-02
104 | 1.936090067155180755e-02
105 | -9.972329659041151428e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_2.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | -4.828209633658692157e-01
3 | -3.208558080091941100e-01
4 | 5.120666623275705459e-02
5 | 4.411096889863109216e-01
6 | 4.060237952761409641e-01
7 | 1.750584774561255441e-01
8 | 1.332816169958273567e-01
9 | -6.813909994284018945e-02
10 | 3.748535823828798019e-01
11 | 3.967273530488419353e-01
12 | -2.198143562563584785e-01
13 | 4.681845829395080427e-01
14 | 4.043599692358753606e-01
15 | 3.560118035581642526e-01
16 | -5.217647981848853866e-02
17 | 2.007775041463628618e-01
18 | -1.259828811841428864e-01
19 | 2.805545896957988283e-01
20 | 2.885433131158136044e-01
21 | -6.697521404327005934e-02
22 | 3.176910949290916442e-01
23 | -3.172833794781901862e-01
24 | -1.081668977648486945e-03
25 | 6.334650389238233315e-01
26 | 2.346711561609144889e-02
27 | 1.178274385313880235e-01
28 | 7.569333904047301220e-02
29 | -3.631792116485303007e-02
30 | -6.774479295622437558e-02
31 | -3.715196762959985938e-02
32 | 2.563037661900596786e-01
33 | 1.474327562693324989e-01
34 | -3.056913836250558236e-01
35 | -2.876331140380910534e-01
36 | -4.567381866282546055e-01
37 | -2.571989071388390558e-02
38 | -1.147352043338922545e-01
39 | -1.931075021502449973e-01
40 | 3.476832163069345366e-02
41 | -2.766366793538127533e-01
42 | -3.767567867617337141e-02
43 | 2.850750127745821372e-01
44 | 5.705956735846673400e-01
45 | -3.255935898213934698e-01
46 | -4.299817672607976204e-01
47 | -3.415526534917274759e-01
48 | 4.200155072407809609e-01
49 | 3.880890552348651990e-01
50 | 3.395770588827504155e-01
51 | -3.443622896948119738e-01
52 | 2.254563819441217920e-01
53 | 3.238762105695576765e-01
54 | 4.930811881706549937e-01
55 | 2.518653062172554469e-01
56 | -3.085169292300960625e-02
57 | 3.920474448178990490e-01
58 | 2.355186517238666566e-01
59 | -3.526082992066886246e-01
60 | -1.745117705220761151e-02
61 | -1.301821712100907080e-01
62 | 1.166593297536090146e-01
63 | -3.309854422706736132e-01
64 | -8.045730431133550631e-02
65 | 5.164754531915004554e-01
66 | 3.283244269451594533e-01
67 | -3.470443331468634623e-02
68 | 5.254229790566756347e-02
69 | -8.302376182243964564e-02
70 | 4.743086148868188578e-01
71 | -6.426706620286985749e-02
72 | -1.543545099217791816e-01
73 | 9.187350224029537660e-02
74 | 1.526239316036185345e-01
75 | 1.121676224586797971e-01
76 | -7.529018828659569951e-02
77 | 4.543889162361772760e-01
78 | 2.112580252799179381e-01
79 | -4.662830495509240336e-02
80 | -2.589995843037459777e-01
81 | -5.216010697482481584e-01
82 | 3.704045980750955147e-01
83 | -2.467205788187304683e-01
84 | 1.130692626070486581e-02
85 | -5.845651523604493693e-01
86 | 3.888289659879398587e-03
87 | -1.647088572970769049e-01
88 | 1.736360256151835701e-01
89 | 2.300617200283530117e-01
90 | -2.978222905054223046e-01
91 | -4.674414755651373210e-01
92 | -4.650705778818944314e-01
93 | 7.089466339454868204e-02
94 | 1.622052218407568358e-01
95 | -1.196863955133640744e-01
96 | -5.779781428495058826e-01
97 | 2.962199059000716634e-03
98 | -3.205375238914061065e-01
99 | 5.061924912166956281e-01
100 | 1.426472134439600581e-01
101 | 8.650717443874915424e-02
102 | -2.676557858233954423e-01
103 | -3.002512209308912383e-01
104 | -4.034721917875954689e-01
105 | -9.417877746494868196e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_8.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | 3.561472023038952384e-01
3 | -2.304856979116161331e-01
4 | -3.021719687337119931e-01
5 | 3.093805662045809868e-01
6 | -1.104877347942199717e-01
7 | 2.076934980736386827e-01
8 | 3.588399415873475690e-02
9 | -3.395661629269048354e-01
10 | -1.689806345341771654e-01
11 | -1.035021272601917286e-02
12 | 8.714291438830432807e-03
13 | -9.829124179665511829e-02
14 | 2.965124345014056306e-01
15 | -1.018206734706424421e-01
16 | 3.260651390695728513e-01
17 | 1.398099084849758200e-01
18 | -1.391148765333355686e-01
19 | 9.721385650824507230e-02
20 | -2.451524751620388909e-01
21 | -2.198646389563890802e-01
22 | 1.729820931563411934e-01
23 | 3.784017925175524599e-01
24 | 2.520674452833880294e-03
25 | 2.022553625053389315e-01
26 | 2.478692030774872213e-01
27 | 1.957868751451738790e-02
28 | 2.008280056479478382e-01
29 | 2.131830756872891142e-01
30 | 1.751722322220748140e-01
31 | -1.580375669540706046e-02
32 | -4.268730730385407630e-03
33 | 7.817491179282534328e-03
34 | 2.533113672061926835e-01
35 | 1.266029800034939867e-01
36 | -1.503125589841712573e-01
37 | 1.380923072548297936e-01
38 | 3.290591901128158137e-01
39 | 1.967007393265623461e-01
40 | -2.445140092397642684e-01
41 | 1.445967238327429405e-01
42 | -1.444094552900775652e-01
43 | -8.932106078395493642e-02
44 | 1.534793811581660894e-01
45 | 4.151431598162433345e-02
46 | -2.613401551765411890e-01
47 | -1.661194841329028671e-01
48 | -1.300056920989375309e-01
49 | 8.341144674586935259e-02
50 | 1.672796025628824623e-01
51 | -1.549053877526587741e-01
52 | -1.704922008445496906e-01
53 | -2.959133523500580565e-01
54 | 4.297931018800886793e-01
55 | -9.690265460688006829e-03
56 | 4.520484160254678585e-02
57 | 1.098187015887530996e-02
58 | 2.741546420029331577e-01
59 | -3.124881520909490729e-02
60 | -1.602675164761995508e-01
61 | -6.491678603612927034e-02
62 | -6.559776772018875712e-03
63 | -9.769689361938821426e-02
64 | -9.665468997019813213e-02
65 | 4.024595279577734830e-02
66 | -4.357063029783896091e-02
67 | 1.947254084818465514e-01
68 | 3.231827467129939069e-01
69 | 8.881811180793475202e-02
70 | -5.800880064120872426e-02
71 | -1.846081388065712037e-01
72 | 2.018587685773436669e-01
73 | 2.299075886080131081e-01
74 | -8.876979264766000910e-02
75 | 9.281163116564677962e-02
76 | -2.036776908414600351e-01
77 | 2.126350640695366945e-01
78 | 1.758612916766761347e-01
79 | 4.991101466786921487e-02
80 | -8.034835591265376831e-02
81 | -2.154809748636816458e-01
82 | 1.062608875337137798e-01
83 | -1.318074191431571271e-02
84 | 2.661904380490742045e-02
85 | -1.606824172516137517e-01
86 | -2.689810169699821119e-01
87 | 9.652843233478466956e-02
88 | 2.561190765968547889e-01
89 | 1.192581521740480183e-01
90 | -3.480538627041104404e-03
91 | -2.751365697296055401e-01
92 | -1.672533459171449965e-01
93 | -3.996094260804424203e-01
94 | 6.508206524914994517e-02
95 | 4.023901695349975105e-01
96 | -8.555305161922276830e-03
97 | -5.925928369141383271e-04
98 | 1.375775547697503653e-01
99 | -1.085914468719001019e-01
100 | 1.454876560229583782e-01
101 | -4.194644588241010497e-01
102 | -2.907374771913221467e-01
103 | -1.088138635020058365e-01
104 | -3.055160927303202101e-01
105 | -3.920487042627285312e-01
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_1.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | 1.407936195361622622e-01
3 | -1.285582543264502386e-01
4 | 2.290539410215178262e-01
5 | -8.030874498034462627e-02
6 | -9.622064735495039489e-02
7 | 3.384755420490687117e-02
8 | 2.269153463758968869e-01
9 | 4.605262001990400034e-01
10 | 2.346266869736804428e-01
11 | -3.515014212392431059e-01
12 | -5.108800290248622922e-01
13 | 2.454100270370600523e-01
14 | -1.179957986073475740e-01
15 | -4.942580621140438835e-01
16 | -5.450634201059632034e-01
17 | 3.742440861796543783e-01
18 | 1.409178961840695243e-01
19 | 8.394720957460263810e-02
20 | 3.873850012997460679e-01
21 | -3.013385507817897269e-01
22 | 4.175913934127237215e-02
23 | -2.968436960733996655e-01
24 | 2.504115263522274093e-01
25 | 4.104997748919678724e-01
26 | -2.476361425799001870e-01
27 | -1.131321083161431207e-01
28 | 3.286665205304604154e-01
29 | -9.788483951841597808e-02
30 | -2.791401035676811526e-01
31 | -2.074278730673355109e-01
32 | -5.398341954515609720e-01
33 | 1.640240662746998979e-01
34 | 3.189627305087449072e-01
35 | 9.124892614178714556e-02
36 | -1.297110615990704485e-01
37 | -2.550795468741896554e-01
38 | 2.122161989826453765e-01
39 | -1.565302363582626866e-03
40 | 5.059143424194849359e-02
41 | 5.868502352904489960e-01
42 | 4.658056383201479855e-01
43 | -2.930674613524147931e-01
44 | -1.115272991269609937e-02
45 | -2.079402730433735358e-01
46 | -3.210672060739578892e-01
47 | -5.073810522073161078e-01
48 | 1.011531923583953718e-01
49 | 1.412331001413955761e-02
50 | -3.150638127693130564e-01
51 | -3.862622460501485833e-01
52 | -2.596104719949596240e-02
53 | 3.160560497996238172e-01
54 | 4.306265981497453543e-01
55 | -5.332639732162730573e-01
56 | -5.428340028437556031e-01
57 | -3.940272748780419554e-01
58 | 7.289203078517014767e-02
59 | -8.940442466505998942e-02
60 | 4.577684965581387821e-01
61 | -3.934877635301334831e-01
62 | -3.599960580821491285e-01
63 | -2.839010885456007272e-02
64 | -4.004800226147915698e-02
65 | 2.668112582434523095e-01
66 | -4.277815910382094799e-02
67 | 3.246573531123558842e-01
68 | -2.221177260650653018e-01
69 | -7.658834100476769047e-02
70 | 1.271083551837089154e-01
71 | 1.365160804998786226e-01
72 | -1.542730545421862054e-01
73 | -6.834117121866829792e-02
74 | -4.076127814904949298e-02
75 | -4.005167927287656138e-01
76 | -4.385117900785809875e-01
77 | -2.347399762632998987e-01
78 | -2.362132140065004715e-01
79 | 8.207624197814730738e-02
80 | 2.908174131065649481e-01
81 | -4.894123371495914254e-01
82 | 3.981021939783551122e-01
83 | -3.413694033731544675e-01
84 | -1.671372313767089945e-01
85 | -5.721507152380340688e-01
86 | 1.492194145433505259e-01
87 | -4.443347050702409762e-01
88 | 1.786284262889387686e-01
89 | -3.503451534743139140e-02
90 | 3.108487452674447704e-01
91 | -4.112608553113196175e-01
92 | 8.009841898630962187e-02
93 | 4.035627686065613728e-01
94 | -3.129784731593825142e-02
95 | 4.466658877140770034e-01
96 | 4.187686973172053406e-01
97 | -2.665038599081738235e-01
98 | -1.557907139052726042e-01
99 | -3.181529188562560728e-01
100 | -4.350391943543346907e-02
101 | 1.686779746877836716e-03
102 | 3.908611237128165605e-01
103 | -8.500146029752940480e-02
104 | 6.265437930633462882e-01
105 | 8.444511926144371294e-02
106 |
--------------------------------------------------------------------------------
/solutions_demo/demo_3.txt:
--------------------------------------------------------------------------------
1 | -1.000000000000000000e+00
2 | -6.091400483395815746e-01
3 | 9.094860309035329715e-02
4 | 4.099774434375756949e-01
5 | -1.115005646386124927e-01
6 | 3.159928480460408862e-01
7 | -2.726349487871058686e-01
8 | 9.870283270102930828e-02
9 | 2.305868115892424908e-01
10 | 6.552273455432128246e-02
11 | -1.602860856163603742e-01
12 | -6.227301975966866854e-01
13 | -2.360296685180764131e-01
14 | 3.822866177440992219e-01
15 | 5.953358625849471814e-02
16 | -2.582370125918271864e-01
17 | -2.431465475852632174e-01
18 | -4.876183333980846912e-01
19 | -5.649362788836805732e-02
20 | -4.903982794827504987e-01
21 | -3.267418894840448118e-01
22 | 6.134566052982313167e-01
23 | -2.309082599571375960e-01
24 | 5.317824922514027830e-02
25 | 2.277398553399270220e-01
26 | -3.838648516573197611e-01
27 | -1.025162112535409065e-01
28 | 5.138180923554109825e-01
29 | 5.080024555166142708e-01
30 | 2.763991915821167322e-01
31 | 1.773470068773430608e-01
32 | -2.144314298560075216e-01
33 | 5.071563184902567745e-01
34 | -1.893420151261684392e-01
35 | 3.275400229141509773e-02
36 | 2.095870278344347248e-01
37 | -1.535383207118536930e-01
38 | -8.317299664039425056e-02
39 | -2.078599768873066189e-01
40 | -5.444762250688958694e-02
41 | -2.557994192080562113e-01
42 | 9.544122134991664397e-02
43 | 2.292886054684375452e-01
44 | 6.333272455404159729e-01
45 | -4.738024627559835750e-02
46 | -1.984239898543910807e-01
47 | -3.507358460213437779e-01
48 | 5.107992875123642529e-01
49 | -9.178160255845309312e-02
50 | -9.527573128587837892e-02
51 | -2.865600714690593742e-01
52 | -1.206554645889672261e-01
53 | 7.495284160130230577e-02
54 | 2.020791229578621645e-02
55 | -4.316729349606651667e-01
56 | -4.069539321714205071e-01
57 | 4.495487220808984385e-02
58 | 3.890824006402974211e-01
59 | 1.664620870095998573e-01
60 | 4.482088053051608290e-01
61 | 3.517083441374553310e-01
62 | -2.071932702447859265e-01
63 | -4.065600104609554677e-01
64 | 4.130698350106301597e-01
65 | -8.451803417196837587e-02
66 | 1.790174025887553491e-02
67 | -1.746810580149396885e-01
68 | 1.109950478105980326e-02
69 | 2.593812484685372732e-01
70 | -1.087964984030477811e-01
71 | -3.214233624043059678e-01
72 | -1.750405797324918744e-01
73 | -1.242574951573142406e-01
74 | 3.884736797476717185e-01
75 | -6.125156410509662031e-02
76 | 3.786239854333562915e-01
77 | -5.076536620043681225e-01
78 | -1.954603171065246170e-01
79 | 3.870882066373775299e-01
80 | 2.052494258503864688e-01
81 | -9.948957543771611012e-02
82 | -5.973857825804103350e-02
83 | 7.450449492649854899e-02
84 | -2.507595951857590211e-01
85 | 1.352128327037182554e-01
86 | -4.143378825334089743e-01
87 | 2.698986242275590031e-01
88 | -4.528187492060898323e-01
89 | -8.711062642746847384e-02
90 | -6.319487809179459570e-02
91 | -2.948721359180107582e-01
92 | 6.641815969095056504e-02
93 | -4.948583173261683621e-02
94 | 2.577174365723825522e-01
95 | 3.346797545083493586e-02
96 | 1.681046877778457904e-01
97 | -1.166300104010407068e-01
98 | -3.630744153955783871e-01
99 | 6.460175667978577119e-01
100 | 7.910718089506449391e-02
101 | 7.034374102899815240e-02
102 | -4.858241280775395937e-02
103 | -1.348752750692363833e-01
104 | -2.858251340350318470e-01
105 | -8.929390358783892978e-01
106 |
--------------------------------------------------------------------------------
/evoman/map2.tmx:
--------------------------------------------------------------------------------
1 |
2 |
115 |
--------------------------------------------------------------------------------
/evoman/map4.tmx:
--------------------------------------------------------------------------------
1 |
2 |
120 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | __pycache__/
2 | *.py[cod]
3 | *$py.class
4 |
5 | # Visual Studio Code
6 | .vscode/
7 |
8 | # Byte-compiled / optimized / DLL files
9 | __pycache__/
10 | *.py[cod]
11 | *$py.class
12 |
13 | # C extensions
14 | *.so
15 |
16 | # Distribution / packaging
17 | .Python
18 | build/
19 | develop-eggs/
20 | dist/
21 | downloads/
22 | eggs/
23 | .eggs/
24 | lib/
25 | lib64/
26 | parts/
27 | sdist/
28 | var/
29 | wheels/
30 | share/python-wheels/
31 | *.egg-info/
32 | .installed.cfg
33 | *.egg
34 | MANIFEST
35 |
36 | # PyInstaller
37 | # Usually these files are written by a python script from a template
38 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
39 | *.manifest
40 | *.spec
41 |
42 | # Installer logs
43 | pip-log.txt
44 | pip-delete-this-directory.txt
45 |
46 | # Unit test / coverage reports
47 | htmlcov/
48 | .tox/
49 | .nox/
50 | .coverage
51 | .coverage.*
52 | .cache
53 | nosetests.xml
54 | coverage.xml
55 | *.cover
56 | *.py,cover
57 | .hypothesis/
58 | .pytest_cache/
59 | cover/
60 |
61 | # Translations
62 | *.mo
63 | *.pot
64 |
65 | # Django stuff:
66 | *.log
67 | local_settings.py
68 | db.sqlite3
69 | db.sqlite3-journal
70 |
71 | # Flask stuff:
72 | instance/
73 | .webassets-cache
74 |
75 | # Scrapy stuff:
76 | .scrapy
77 |
78 | # Sphinx documentation
79 | docs/_build/
80 |
81 | # PyBuilder
82 | .pybuilder/
83 | target/
84 |
85 | # Jupyter Notebook
86 | .ipynb_checkpoints
87 |
88 | # IPython
89 | profile_default/
90 | ipython_config.py
91 |
92 | # pyenv
93 | # For a library or package, you might want to ignore these files since the code is
94 | # intended to run in multiple environments; otherwise, check them in:
95 | # .python-version
96 |
97 | # pipenv
98 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
99 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
100 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
101 | # install all needed dependencies.
102 | #Pipfile.lock
103 |
104 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
105 | __pypackages__/
106 |
107 | # Celery stuff
108 | celerybeat-schedule
109 | celerybeat.pid
110 |
111 | # SageMath parsed files
112 | *.sage.py
113 |
114 | # Environments
115 | .env
116 |
117 | # Spyder project settings
118 | .spyderproject
119 | .spyproject
120 |
121 | # Rope project settings
122 | .ropeproject
123 |
124 | # mkdocs documentation
125 | /site
126 |
127 | # mypy
128 | .mypy_cache/
129 | .dmypy.json
130 | dmypy.json
131 |
132 | # Pyre type checker
133 | .pyre/
134 |
135 | # pytype static type analyzer
136 | .pytype/
137 |
138 | # Cython debug symbols
139 | cython_debug/
140 |
141 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
142 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
143 |
144 | # User-specific stuff
145 | .idea/**/workspace.xml
146 | .idea/**/tasks.xml
147 | .idea/**/usage.statistics.xml
148 | .idea/**/dictionaries
149 | .idea/**/shelf
150 |
151 | # AWS User-specific
152 | .idea/**/aws.xml
153 |
154 | # Generated files
155 | .idea/**/contentModel.xml
156 |
157 | # Sensitive or high-churn files
158 | .idea/**/dataSources/
159 | .idea/**/dataSources.ids
160 | .idea/**/dataSources.local.xml
161 | .idea/**/sqlDataSources.xml
162 | .idea/**/dynamic.xml
163 | .idea/**/uiDesigner.xml
164 | .idea/**/dbnavigator.xml
165 |
166 | # Gradle
167 | .idea/**/gradle.xml
168 | .idea/**/libraries
169 |
170 | # Gradle and Maven with auto-import
171 | # When using Gradle or Maven with auto-import, you should exclude module files,
172 | # since they will be recreated, and may cause churn. Uncomment if using
173 | # auto-import.
174 | # .idea/artifacts
175 | # .idea/compiler.xml
176 | # .idea/jarRepositories.xml
177 | # .idea/modules.xml
178 | # .idea/*.iml
179 | # .idea/modules
180 | # *.iml
181 | # *.ipr
182 |
183 | # CMake
184 | cmake-build-*/
185 |
186 | # Mongo Explorer plugin
187 | .idea/**/mongoSettings.xml
188 |
189 | # File-based project format
190 | *.iws
191 |
192 | # IntelliJ
193 | out/
194 |
195 | # mpeltonen/sbt-idea plugin
196 | .idea_modules/
197 |
198 | # JIRA plugin
199 | atlassian-ide-plugin.xml
200 |
201 | # Cursive Clojure plugin
202 | .idea/replstate.xml
203 |
204 | # SonarLint plugin
205 | .idea/sonarlint/
206 |
207 | # Crashlytics plugin (for Android Studio and IntelliJ)
208 | com_crashlytics_export_strings.xml
209 | crashlytics.properties
210 | crashlytics-build.properties
211 | fabric.properties
212 |
213 | # Editor-based Rest Client
214 | .idea/httpRequests
215 |
216 | # Android studio 3.1+ serialized cache file
217 | .idea/caches/build_file_checksums.ser
218 |
--------------------------------------------------------------------------------
/demo_controller.py:
--------------------------------------------------------------------------------
1 | # the demo_controller file contains standard controller structures for the agents.
2 | # you can overwrite the method 'control' in your own instance of the environment
3 | # and then use a different type of controller if you wish.
4 | # note that the param 'controller' received by 'control' is provided through environment.play(pcont=x)
5 | # 'controller' could contain either weights to be used in the standard controller (or other controller implemented),
6 | # or even a full network structure (ex.: from NEAT).
7 | from evoman.controller import Controller
8 | import numpy as np
9 |
10 |
11 | def sigmoid_activation(x):
12 | return 1./(1.+np.exp(-x))
13 |
14 |
15 | # implements controller structure for player
16 | class player_controller(Controller):
17 | def __init__(self, _n_hidden):
18 | self.n_hidden = [_n_hidden]
19 |
20 | def set(self,controller, n_inputs):
21 | # Number of hidden neurons
22 |
23 | if self.n_hidden[0] > 0:
24 | # Preparing the weights and biases from the controller of layer 1
25 |
26 | # Biases for the n hidden neurons
27 | self.bias1 = controller[:self.n_hidden[0]].reshape(1, self.n_hidden[0])
28 | # Weights for the connections from the inputs to the hidden nodes
29 | weights1_slice = n_inputs * self.n_hidden[0] + self.n_hidden[0]
30 | self.weights1 = controller[self.n_hidden[0]:weights1_slice].reshape((n_inputs, self.n_hidden[0]))
31 |
32 | # Outputs activation first layer.
33 |
34 |
35 | # Preparing the weights and biases from the controller of layer 2
36 | self.bias2 = controller[weights1_slice:weights1_slice + 5].reshape(1, 5)
37 | self.weights2 = controller[weights1_slice + 5:].reshape((self.n_hidden[0], 5))
38 |
39 | def control(self, inputs, controller):
40 | # Normalises the input using min-max scaling
41 | inputs = (inputs-min(inputs))/float((max(inputs)-min(inputs)))
42 |
43 | if self.n_hidden[0]>0:
44 | # Preparing the weights and biases from the controller of layer 1
45 |
46 | # Outputs activation first layer.
47 | output1 = sigmoid_activation(inputs.dot(self.weights1) + self.bias1)
48 |
49 | # Outputting activated second layer. Each entry in the output is an action
50 | output = sigmoid_activation(output1.dot(self.weights2)+ self.bias2)[0]
51 | else:
52 | bias = controller[:5].reshape(1, 5)
53 | weights = controller[5:].reshape((len(inputs), 5))
54 |
55 | output = sigmoid_activation(inputs.dot(weights) + bias)[0]
56 |
57 | # takes decisions about sprite actions
58 | if output[0] > 0.5:
59 | left = 1
60 | else:
61 | left = 0
62 |
63 | if output[1] > 0.5:
64 | right = 1
65 | else:
66 | right = 0
67 |
68 | if output[2] > 0.5:
69 | jump = 1
70 | else:
71 | jump = 0
72 |
73 | if output[3] > 0.5:
74 | shoot = 1
75 | else:
76 | shoot = 0
77 |
78 | if output[4] > 0.5:
79 | release = 1
80 | else:
81 | release = 0
82 |
83 | return [left, right, jump, shoot, release]
84 |
85 |
86 | # implements controller structure for enemy
87 | class enemy_controller(Controller):
88 | def __init__(self, _n_hidden):
89 | # Number of hidden neurons
90 | self.n_hidden = [_n_hidden]
91 |
92 | def control(self, inputs,controller):
93 | # Normalises the input using min-max scaling
94 | inputs = (inputs-min(inputs))/float((max(inputs)-min(inputs)))
95 |
96 | if self.n_hidden[0]>0:
97 | # Preparing the weights and biases from the controller of layer 1
98 |
99 | # Biases for the n hidden neurons
100 | bias1 = controller[:self.n_hidden[0]].reshape(1,self.n_hidden[0])
101 | # Weights for the connections from the inputs to the hidden nodes
102 | weights1_slice = len(inputs)*self.n_hidden[0] + self.n_hidden[0]
103 | weights1 = controller[self.n_hidden[0]:weights1_slice].reshape((len(inputs),self.n_hidden[0]))
104 |
105 | # Outputs activation first layer.
106 | output1 = sigmoid_activation(inputs.dot(weights1) + bias1)
107 |
108 | # Preparing the weights and biases from the controller of layer 2
109 | bias2 = controller[weights1_slice:weights1_slice + 5].reshape(1,5)
110 | weights2 = controller[weights1_slice + 5:].reshape((self.n_hidden[0],5))
111 |
112 | # Outputting activated second layer. Each entry in the output is an action
113 | output = sigmoid_activation(output1.dot(weights2)+ bias2)[0]
114 | else:
115 | bias = controller[:5].reshape(1, 5)
116 | weights = controller[5:].reshape((len(inputs), 5))
117 |
118 | output = sigmoid_activation(inputs.dot(weights) + bias)[0]
119 |
120 | # takes decisions about sprite actions
121 | if output[0] > 0.5:
122 | attack1 = 1
123 | else:
124 | attack1 = 0
125 |
126 | if output[1] > 0.5:
127 | attack2 = 1
128 | else:
129 | attack2 = 0
130 |
131 | if output[2] > 0.5:
132 | attack3 = 1
133 | else:
134 | attack3 = 0
135 |
136 | if output[3] > 0.5:
137 | attack4 = 1
138 | else:
139 | attack4 = 0
140 |
141 | return [attack1, attack2, attack3, attack4]
142 |
--------------------------------------------------------------------------------
/evoman/map1.tmx:
--------------------------------------------------------------------------------
1 |
2 |
146 |
--------------------------------------------------------------------------------
/optimization_coevolution_demo.py:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # DEMO : Neuroevolution - Genetic Algorithm with neural network. #
4 | # Author: Karine Miras #
5 | # karine.smiras@gmail.com #
6 | ###############################################################################
7 |
8 | # imports framework
9 | import sys
10 |
11 | from evoman.environment import Environment
12 | from demo_controller import player_controller, enemy_controller
13 |
14 | # imports other libs
15 | import time
16 | import numpy as np
17 | from math import fabs,sqrt
18 | import glob, os
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 | class environm(Environment):
27 |
28 | # implements fitness function
29 | def fitness_single(self):
30 |
31 | if self.contacthurt == "player":
32 | return 0.9*(100 - self.get_enemylife()) + 0.1*self.get_playerlife() - np.log(self.get_time())
33 |
34 | else:
35 | return 0.9*(100 - self.get_playerlife()) + 0.1*self.get_enemylife() - np.log(self.get_time())
36 |
37 |
38 | experiment_name = 'co_demo'
39 | if not os.path.exists(experiment_name):
40 | os.makedirs(experiment_name)
41 |
42 | n_hidden_neurons = 10
43 |
44 | # initializes simulation for coevolution evolution mode.
45 | env = environm(experiment_name=experiment_name,
46 | enemies=[2],
47 | playermode="ai",
48 | enemymode="ai",
49 | player_controller=player_controller(n_hidden_neurons),
50 | enemy_controller=enemy_controller(n_hidden_neurons),
51 | level=2,
52 | speed="fastest")
53 |
54 |
55 | env.state_to_log() # checks environment state
56 |
57 |
58 | #### Optimization for controller solution (best genotype/weights for perceptron phenotype network): Ganetic Algorihm ###
59 |
60 | ini = time.time() # sets time marker
61 |
62 |
63 | # genetic algorithm params
64 |
65 | run_mode = 'train' # train or test
66 |
67 | # number of weights for multilayer with 10 hidden neurons
68 | n_vars = (env.get_num_sensors()+1)*n_hidden_neurons + (n_hidden_neurons+1)*5
69 |
70 | dom_u = 1
71 | dom_l = -1
72 | npop = 100
73 | gens = 100
74 | mutation = 0.2
75 | last_best = 0
76 |
77 |
78 | # runs simulation
79 | def simulation(env,x1,x2):
80 | f,p,e,t = env.play(pcont=x1,econt=x2)
81 | return f
82 |
83 | # normalizes
84 | def norm(x,pfit_pop):
85 |
86 | if ( max(pfit_pop) - min(pfit_pop) ) > 0:
87 | x_norm = ( x - min(pfit_pop) )/( max(pfit_pop) - min(pfit_pop) )
88 | else:
89 | x_norm = 0
90 |
91 | if x_norm <= 0:
92 | x_norm = 0.0000000001
93 | return x_norm
94 |
95 |
96 | # evaluation
97 | def evaluate(x1,x2):
98 | return np.array(list(map(lambda y: simulation(env,y,x2), x1)))
99 |
100 |
101 | # tournament
102 | def tournament(pop,fit_pop):
103 | c1 = np.random.randint(0,pop.shape[0], 1)
104 | c2 = np.random.randint(0,pop.shape[0], 1)
105 | if fit_pop[c1] > fit_pop[c2]:
106 | return pop[c1][0]
107 | else:
108 | return pop[c2][0]
109 |
110 |
111 | # limits
112 | def limits(x):
113 |
114 | if x>dom_u:
115 | return dom_u
116 | elif x 0:
79 | x_norm = ( x - min(pfit_pop) )/( max(pfit_pop) - min(pfit_pop) )
80 | else:
81 | x_norm = 0
82 |
83 | if x_norm <= 0:
84 | x_norm = 0.0000000001
85 | return x_norm
86 |
87 |
88 | # evaluation
89 | def evaluate(x):
90 | return np.array(list(map(lambda y: simulation(env,y), x)))
91 |
92 |
93 | # tournament
94 | def tournament(pop):
95 | c1 = np.random.randint(0,pop.shape[0], 1)
96 | c2 = np.random.randint(0,pop.shape[0], 1)
97 |
98 | if fit_pop[c1] > fit_pop[c2]:
99 | return pop[c1][0]
100 | else:
101 | return pop[c2][0]
102 |
103 |
104 | # limits
105 | def limits(x):
106 |
107 | if x>dom_u:
108 | return dom_u
109 | elif x= 15:
255 |
256 | file_aux = open(experiment_name+'/results.txt','a')
257 | file_aux.write('\ndoomsday')
258 | file_aux.close()
259 |
260 | pop, fit_pop = doomsday(pop,fit_pop)
261 | notimproved = 0
262 |
263 | best = np.argmax(fit_pop)
264 | std = np.std(fit_pop)
265 | mean = np.mean(fit_pop)
266 |
267 |
268 | # saves results
269 | file_aux = open(experiment_name+'/results.txt','a')
270 | print( '\n GENERATION '+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)))
271 | file_aux.write('\n'+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)) )
272 | file_aux.close()
273 |
274 | # saves generation number
275 | file_aux = open(experiment_name+'/gen.txt','w')
276 | file_aux.write(str(i))
277 | file_aux.close()
278 |
279 | # saves file with the best solution
280 | np.savetxt(experiment_name+'/best.txt',pop[best])
281 |
282 | # saves simulation state
283 | solutions = [pop, fit_pop]
284 | env.update_solutions(solutions)
285 | env.save_state()
286 |
287 |
288 |
289 |
290 | fim = time.time() # prints total execution time for experiment
291 | print( '\nExecution time: '+str(round((fim-ini)/60))+' minutes \n')
292 |
293 |
294 | file = open(experiment_name+'/neuroended', 'w') # saves control (simulation has ended) file for bash loop file
295 | file.close()
296 |
297 |
298 | env.state_to_log() # checks environment state
299 |
--------------------------------------------------------------------------------
/optimization_specialist_demo.py:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # DEMO : Neuroevolution - Genetic Algorithm neural network. #
4 | # Author: Karine Miras #
5 | # karine.smiras@gmail.com #
6 | ###############################################################################
7 |
8 | # imports framework
9 | import sys
10 |
11 | from evoman.environment import Environment
12 | from demo_controller import player_controller
13 |
14 | # imports other libs
15 | import time
16 | import numpy as np
17 | from math import fabs,sqrt
18 | import glob, os
19 |
20 |
21 | # choose this for not using visuals and thus making experiments faster
22 | headless = True
23 | if headless:
24 | os.environ["SDL_VIDEODRIVER"] = "dummy"
25 |
26 |
27 | experiment_name = 'optimization_test'
28 | if not os.path.exists(experiment_name):
29 | os.makedirs(experiment_name)
30 |
31 | n_hidden_neurons = 10
32 |
33 |
34 |
35 | # initializes simulation in individual evolution mode, for single static enemy.
36 | env = Environment(experiment_name=experiment_name,
37 | enemies=[8],
38 | playermode="ai",
39 | player_controller=player_controller(n_hidden_neurons),
40 | enemymode="static",
41 | level=2,
42 | speed="fastest",
43 | visuals=False)
44 |
45 | # default environment fitness is assumed for experiment
46 |
47 | env.state_to_log() # checks environment state
48 |
49 |
50 | #### Optimization for controller solution (best genotype-weights for phenotype-network): Ganetic Algorihm ###
51 |
52 | ini = time.time() # sets time marker
53 |
54 |
55 | # genetic algorithm params
56 |
57 | run_mode = 'train' # train or test
58 |
59 | # number of weights for multilayer with 10 hidden neurons
60 | n_vars = (env.get_num_sensors()+1)*n_hidden_neurons + (n_hidden_neurons+1)*5
61 |
62 |
63 | dom_u = 1
64 | dom_l = -1
65 | npop = 100
66 | gens = 30
67 | mutation = 0.2
68 | last_best = 0
69 |
70 |
71 | # runs simulation
72 | def simulation(env,x):
73 | f,p,e,t = env.play(pcont=x)
74 | return f
75 |
76 | # normalizes
77 | def norm(x, pfit_pop):
78 |
79 | if ( max(pfit_pop) - min(pfit_pop) ) > 0:
80 | x_norm = ( x - min(pfit_pop) )/( max(pfit_pop) - min(pfit_pop) )
81 | else:
82 | x_norm = 0
83 |
84 | if x_norm <= 0:
85 | x_norm = 0.0000000001
86 | return x_norm
87 |
88 |
89 | # evaluation
90 | def evaluate(x):
91 | return np.array(list(map(lambda y: simulation(env,y), x)))
92 |
93 |
94 | # tournament
95 | def tournament(pop):
96 | c1 = np.random.randint(0,pop.shape[0], 1)
97 | c2 = np.random.randint(0,pop.shape[0], 1)
98 |
99 | if fit_pop[c1] > fit_pop[c2]:
100 | return pop[c1][0]
101 | else:
102 | return pop[c2][0]
103 |
104 |
105 | # limits
106 | def limits(x):
107 |
108 | if x>dom_u:
109 | return dom_u
110 | elif x= 15:
256 |
257 | file_aux = open(experiment_name+'/results.txt','a')
258 | file_aux.write('\ndoomsday')
259 | file_aux.close()
260 |
261 | pop, fit_pop = doomsday(pop,fit_pop)
262 | notimproved = 0
263 |
264 | best = np.argmax(fit_pop)
265 | std = np.std(fit_pop)
266 | mean = np.mean(fit_pop)
267 |
268 |
269 | # saves results
270 | file_aux = open(experiment_name+'/results.txt','a')
271 | print( '\n GENERATION '+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)))
272 | file_aux.write('\n'+str(i)+' '+str(round(fit_pop[best],6))+' '+str(round(mean,6))+' '+str(round(std,6)) )
273 | file_aux.close()
274 |
275 | # saves generation number
276 | file_aux = open(experiment_name+'/gen.txt','w')
277 | file_aux.write(str(i))
278 | file_aux.close()
279 |
280 | # saves file with the best solution
281 | np.savetxt(experiment_name+'/best.txt',pop[best])
282 |
283 | # saves simulation state
284 | solutions = [pop, fit_pop]
285 | env.update_solutions(solutions)
286 | env.save_state()
287 |
288 |
289 |
290 |
291 | fim = time.time() # prints total execution time for experiment
292 | print( '\nExecution time: '+str(round((fim-ini)/60))+' minutes \n')
293 | print( '\nExecution time: '+str(round((fim-ini)))+' seconds \n')
294 |
295 |
296 | file = open(experiment_name+'/neuroended', 'w') # saves control (simulation has ended) file for bash loop file
297 | file.close()
298 |
299 |
300 | env.state_to_log() # checks environment state
301 |
--------------------------------------------------------------------------------
/evoman/enemy6.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map2.tmx'
14 | timeexpire = 2200 # game run limit
15 |
16 | # enemy 6 sprite, crashman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 | def __init__(self, location, *groups, visuals):
20 |
21 | super(Enemy, self).__init__(*groups)
22 | self.visuals = visuals
23 | if visuals:
24 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
25 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
26 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.rect = pygame.rect.Rect(location, self.image.get_size())
28 | else:
29 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
30 | self.direction = -1
31 | self.max_life = 100
32 | self.life = self.max_life
33 | self.resting = 0
34 | self.dy = 0
35 | self.twists = []
36 | self.alternate = 1
37 | self.just_shoot = 0
38 | self.imune = 0
39 | self.timeenemy = 0
40 | self.hurt = 0
41 | self.shooting = 0
42 | self.gun_cooldown = 0
43 |
44 |
45 |
46 | def update(self, dt, game):
47 |
48 |
49 | if game.time==1:
50 | # puts enemy in random initial position
51 | if game.randomini == 'yes':
52 | self.rect.x = numpy.random.choice([640,500,400,300])
53 |
54 |
55 | # Defines game mode for player action.
56 | if game.enemymode == 'static': # Enemy controlled by static movements.
57 |
58 | if self.timeenemy == 105:
59 | atack1 = 1
60 | else:
61 | atack1 = 0
62 |
63 | if ( (abs(self.rect.left-game.player.rect.left)<=1 or abs(self.rect.right-game.player.rect.right)<=1) or self.dy>200 ):
64 | atack2 = 1
65 | else:
66 | atack2 = 0
67 |
68 |
69 | atack3 = 0
70 |
71 |
72 | elif game.enemymode == 'ai': # Player controlled by AI algorithm.
73 |
74 |
75 | # calls the controller providing game sensors
76 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
77 | if len(actions) < 3:
78 | game.print_logs("ERROR: Enemy 1 controller must return 3 decision variables.")
79 | sys.exit(0)
80 |
81 | atack1 = actions[0]
82 | atack2 = actions[1]
83 | atack3 = actions[2]
84 |
85 |
86 |
87 | if atack2 == 1 and not self.gun_cooldown:
88 | atack2 = 1
89 | else:
90 | atack2 = 0
91 |
92 |
93 | # if the 'start game' marker is 1
94 | if game.start == 1:
95 |
96 | self.timeenemy += 1 # increments enemy timer
97 |
98 | last = self.rect.copy() # copies last position state of the enemy
99 |
100 | # movements of the enemy on the axis x until a limit (turning aroud)
101 |
102 | if self.rect.left<60:
103 | self.direction = self.direction * -1
104 | self.rect.left = 60
105 | if self.rect.right>680:
106 | self.direction = self.direction * -1
107 | self.rect.right = 680
108 |
109 | # calculating the relative distance between enemy and player to set the jumping strengh
110 | aux_dist = (abs(game.player.rect.right - self.rect.right)/490.0)+0.1
111 |
112 | # when atacking, enemy may accelarate his movement.
113 | if self.dy<0:
114 | self.rect.x += self.direction * (1500 *aux_dist) * dt
115 | else:
116 | self.rect.x += self.direction * 180 * dt
117 |
118 | # jumps over the player. It happens from time to time, or when the player shoots.
119 | if ((self.resting == 1 and atack1 == 1) or ( self.resting == 1 and game.player.atacked == 1)):
120 |
121 | if game.enemymode == 'static':
122 | # enemy turns to the players direction.
123 | if game.player.rect.right <= self.rect.left:
124 | self.direction = -1
125 | if game.player.rect.left >= self.rect.right:
126 | self.direction = 1
127 |
128 | # reinicializes enemy timer
129 | self.timeenemy = 0
130 |
131 | self.dy = -1500 * aux_dist
132 | self.resting = 0
133 |
134 | if atack3 == 1 and game.enemymode == 'ai':
135 | self.direction = self.direction * -1
136 |
137 | # throws a bullet over the player when enemy is jumping and right over him
138 | if self.resting == 0 and self.just_shoot == 0 and atack2 == 1:
139 |
140 | self.shooting = 5
141 | self.gun_cooldown = 3
142 |
143 | # bullets sound effect
144 | if game.sound == "on" and game.playermode == "human":
145 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
146 | c = pygame.mixer.Channel(3)
147 | c.set_volume(10)
148 | c.play(sound)
149 |
150 | self.just_shoot = 1
151 |
152 | self.twists.append(Bullet_e6((self.rect.x ,self.rect.y ), self.direction, len(self.twists), game.sprite_e))
153 |
154 |
155 | self.gun_cooldown = max(0, self.gun_cooldown - dt) # decreases time for bullets limitation
156 |
157 | # animation, running enemy images alternation
158 | if self.direction > 0:
159 | direction = SpriteConstants.RIGHT
160 | else:
161 | direction = SpriteConstants.LEFT
162 |
163 | if self.alternate == 1:
164 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
165 | if self.alternate == 4 or self.alternate == 10:
166 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
167 | if self.alternate == 7:
168 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
169 |
170 | self.alternate += 1
171 | if self.alternate > 12:
172 | self.alternate = 1
173 |
174 | # changes the image when enemy jumps
175 | if self.resting == 0:
176 | if self.direction == -1:
177 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
178 | else:
179 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
180 |
181 | # checks collision of the player with the enemy
182 | if self.rect.colliderect(game.player.rect):
183 |
184 | # choses what sprite penalise according to config
185 | if game.contacthurt == "player":
186 | game.player.life = max(0, game.player.life-(game.level*0.3))
187 | if game.contacthurt == "enemy":
188 | game.enemy.life = max(0, game.enemy.life-(game.level*0.3))
189 |
190 | game.player.rect.x += self.direction * 50 * dt # pushes player when he collides with the enemy
191 |
192 | # limits the player to stand on the screen space even being pushed
193 | if game.player.rect.x < 60:
194 | game.player.rect.x = 60
195 | if game.player.rect.x > 620:
196 | game.player.rect.x = 620
197 |
198 | game.player.hurt = 5 # sets flag to change the player image when he is hurt
199 |
200 | # gravity
201 | self.dy = min(400, self.dy + 100)
202 | self.rect.y += self.dy * dt
203 |
204 | # controls screen walls and platforms limits agaist enemy
205 | new = self.rect
206 | self.resting = 0
207 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
208 |
209 | blockers = cell['blockers']
210 |
211 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
212 | new.right = cell.left
213 |
214 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
215 | new.left = cell.right
216 |
217 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
218 | self.resting = 1
219 | new.bottom = cell.top
220 | self.dy = 0
221 | self.just_shoot = 0
222 |
223 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
224 | new.top = cell.bottom
225 |
226 | # hurt enemy animation
227 | if self.hurt > 0:
228 | if self.direction == -1:
229 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
230 | else:
231 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
232 |
233 | self.hurt -=1
234 |
235 | # changes bullets images according to the enemy direction
236 | if self.shooting > 0:
237 | if self.direction == -1:
238 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
239 | else:
240 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
241 |
242 | self.shooting -= 1
243 | self.shooting = max(0,self.shooting)
244 |
245 |
246 | def updateSprite(self, state, direction):
247 | if self.visuals:
248 | self.image = self.spriteDefinition.getImage(state, direction)
249 |
250 | # enemy's bullet
251 | class Bullet_e6(pygame.sprite.Sprite):
252 |
253 |
254 |
255 | image = pygame.image.load('evoman/images/mi2.png')
256 |
257 | def __init__(self, location, direction, n_twist, *groups):
258 | super(Bullet_e6, self).__init__(*groups)
259 | self.rect = pygame.rect.Rect(location, self.image.get_size())
260 | self.direction = direction
261 | self.lifespan = 70
262 | self.n_twist = n_twist
263 |
264 |
265 | def update(self, dt, game):
266 |
267 |
268 | self.rect.y += 500 * dt # moves the bullets
269 |
270 | self.rect.y = min(410,self.rect.y) # prevents bullets from passing throught the floor
271 |
272 | self.lifespan -= 1 # decreases bullet's timer
273 |
274 | # removes old bullets
275 | if self.lifespan < 0:
276 | self.kill()
277 | game.enemy.twists[self.n_twist] = None
278 | return
279 |
280 | # checks collision of enemy's bullet with the player
281 | if self.rect.colliderect(game.player.rect):
282 |
283 | # player loses life points, according to the difficulty level of the game (the more difficult, the more it loses).
284 | game.player.life = max(0, game.player.life-(game.level*0.3))
285 |
286 |
287 | game.player.rect.x += self.direction * 100 * dt # pushes player when he collides with the enemy
288 |
289 | # limits the player to stand on the screen space even being pushed
290 | if game.player.rect.x < 60:
291 | game.player.rect.x = 60
292 | if game.player.rect.x > 620:
293 | game.player.rect.x = 620
294 |
295 |
296 | game.player.hurt = 5 # sets flag to change the player image when he is hurt
297 |
--------------------------------------------------------------------------------
/evoman/enemy8.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map3.tmx'
14 | timeexpire = 1000 # game run limit
15 |
16 | # enemy 8 sprite, quickman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 | def __init__(self, location, *groups, visuals):
20 |
21 | super(Enemy, self).__init__(*groups)
22 | self.visuals = visuals
23 | if visuals:
24 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
25 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
26 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.rect = pygame.rect.Rect(location, self.image.get_size())
28 | else:
29 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
30 | self.direction = -1
31 | self.max_life = 100
32 | self.life = self.max_life
33 | self.resting = 0
34 | self.dy = 0
35 | self.alternate = 1
36 | self.just_shoot = 0
37 | self.imune = 0
38 | self.timeenemy = 0
39 | self.twists = []
40 | self.hurt = 0
41 | self.shooting = 0
42 | self.gun_cooldown = 0
43 |
44 |
45 | def update(self, dt, game):
46 |
47 |
48 | if game.time==1:
49 |
50 | # puts enemy in random initial position
51 | if game.randomini == 'yes':
52 | self.rect.x = numpy.random.choice([640,500,400,300])
53 |
54 | # defines game mode for player action
55 | if game.enemymode == 'static': # enemy controlled by static movements
56 |
57 | if (self.timeenemy >= 1 and self.timeenemy <10) or (self.timeenemy >= 20 and self.timeenemy <30):
58 | atack1 = 1
59 | else:
60 | atack1 = 0
61 |
62 | if self.timeenemy == 1 or self.timeenemy == 20:
63 | atack2 = 1
64 | else:
65 | atack2 = 0
66 |
67 | if self.timeenemy == 9 or self.timeenemy == 29:
68 | atack3 = 1
69 | else:
70 | atack3 = 0
71 |
72 | if self.timeenemy >=40 and self.timeenemy <50:
73 | atack4 = 1
74 | else:
75 | atack4 = 0
76 |
77 | if self.timeenemy == 50:
78 | atack5 = 1
79 | else:
80 | atack5 = 0
81 |
82 | if ( (abs(self.rect.left-game.player.rect.left)<=200 or abs(self.rect.right-game.player.rect.right)<=200) ) and not self.gun_cooldown:
83 | atack6 = 1
84 | else:
85 | atack6 = 0
86 |
87 |
88 |
89 | elif game.enemymode == 'ai': # player controlled by AI algorithm
90 |
91 |
92 |
93 | # calls the controller providing game sensors
94 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
95 | if len(actions) < 6:
96 | game.print_logs("ERROR: Enemy 1 controller must return 6 decision variables.")
97 | sys.exit(0)
98 |
99 | atack1 = actions[0]
100 | atack2 = actions[1]
101 | atack3 = actions[2]
102 | atack4 = actions[3]
103 | atack5 = actions[4]
104 | atack6 = actions[5]
105 |
106 |
107 |
108 | if atack6 == 1 and not self.gun_cooldown:
109 | atack6 = 1
110 | else:
111 | atack6 = 0
112 |
113 |
114 | # If the 'start game' marker is 1.
115 | if game.start == 1:
116 |
117 | self.timeenemy += 1 # increments enemy timer
118 |
119 | last = self.rect.copy() # copies last position state of the enemy
120 |
121 | # jumps over the player. It happens twice.
122 | if atack1== 1 :
123 | # moves on the axis x
124 | self.rect.x += self.direction * 730 * dt
125 | if self.resting == 1 and atack2 == 1:
126 | self.dy = -900
127 | self.resting = 0
128 |
129 | # enemy turns to the players direction
130 | if atack3 == 1:
131 |
132 | if game.enemymode == 'static':
133 | if game.player.rect.right < self.rect.left:
134 | self.direction = -1
135 | if game.player.rect.left > self.rect.right:
136 | self.direction = 1
137 | else:
138 | self.direction = self.direction * -1
139 |
140 | # runs in the player's direction, after jumping twice
141 | elif atack4 == 1 :
142 | self.rect.x += self.direction * 900 * dt
143 |
144 | # reinicializes enemy timer
145 | elif atack5 == 1:
146 | self.timeenemy = 0
147 |
148 | # throws a bullet over the player when enemy is jumping and right over him
149 | if self.resting == 0 and self.just_shoot == 0 and atack6 == 1:
150 |
151 | self.shooting = 5
152 |
153 | self.gun_cooldown = 5
154 |
155 | # bullets sound effect
156 | if game.sound == "on" and game.playermode == "human":
157 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
158 | c = pygame.mixer.Channel(3)
159 | c.set_volume(10)
160 | c.play(sound)
161 |
162 | self.just_shoot = 1
163 |
164 | rand = 3
165 | # shoots from 1 to 3 bullets
166 | for i in range(0,rand):
167 | self.twists.append(Bullet_e8((self.rect.x+(i*60) ,self.rect.y ), i, self.direction, len(self.twists), game.sprite_e))
168 |
169 | # decreases time for bullets limitation
170 | self.gun_cooldown = max(0, self.gun_cooldown - dt)
171 |
172 | # animation, running enemy images alternation
173 | if self.direction > 0:
174 | direction = SpriteConstants.RIGHT
175 | else:
176 | direction = SpriteConstants.LEFT
177 |
178 | if self.alternate == 1:
179 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
180 | if self.alternate == 4 or self.alternate == 10:
181 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
182 | if self.alternate == 7:
183 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
184 |
185 | self.alternate += 1
186 | if self.alternate > 12:
187 | self.alternate = 1
188 |
189 | # changes the image when enemy jumps
190 | if self.resting == 0:
191 | if self.direction == -1:
192 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
193 | else:
194 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
195 |
196 | # checks collision of the player with the enemy
197 | if self.rect.colliderect(game.player.rect):
198 |
199 | # choses what sprite penalise according to config
200 | if game.contacthurt == "player":
201 | game.player.life = max(0, game.player.life-(game.level*0.3))
202 | if game.contacthurt == "enemy":
203 | game.enemy.life = max(0, game.enemy.life-(game.level*0.3))
204 |
205 | # sets flag to change the player image when he is hurt
206 | game.player.hurt = 5
207 |
208 | # gravity
209 | self.dy = min(400, self.dy + 100)
210 | self.rect.y += self.dy * dt
211 |
212 | # controls screen walls and platforms limits agaist enemy
213 | new = self.rect
214 | self.resting = 0
215 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
216 |
217 | blockers = cell['blockers']
218 |
219 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
220 | self.resting = 1
221 | new.bottom = cell.top
222 | self.dy = 0
223 | self.just_shoot = 0
224 |
225 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
226 | new.top = cell.bottom
227 |
228 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom>cell.top:
229 | new.right = cell.left
230 |
231 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom>cell.top:
232 | new.left = cell.right
233 |
234 | # hurt enemy animation
235 | if self.hurt > 0:
236 | if self.direction == -1:
237 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
238 | else:
239 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
240 |
241 | self.hurt -=1
242 |
243 | # changes bullets images according to the enemy direction
244 | if self.shooting > 0:
245 | if self.direction == -1:
246 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
247 | else:
248 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
249 |
250 | self.shooting -= 1
251 | self.shooting = max(0,self.shooting)
252 |
253 |
254 | def updateSprite(self, state, direction):
255 | if self.visuals:
256 | self.image = self.spriteDefinition.getImage(state, direction)
257 |
258 |
259 | # enemy's bullet
260 | class Bullet_e8(pygame.sprite.Sprite):
261 |
262 | image = pygame.image.load('evoman/images/bullet2_l.png')
263 |
264 | def __init__(self, location, direction, n, n_twist, *groups):
265 | super(Bullet_e8, self).__init__(*groups)
266 | self.rect = pygame.rect.Rect(location, self.image.get_size())
267 | self.direction = direction
268 | self.lifespan = 70
269 | self.n = n
270 | self.n_twist = n_twist
271 |
272 |
273 |
274 | def update(self, dt, game):
275 |
276 |
277 | self.lifespan -= 1 # decreases bullet's timer
278 |
279 |
280 | # moves the bullets up after sometime
281 | if self.lifespan < 40:
282 | self.rect.y -= 700 * dt
283 | else:
284 | self.rect.y += 500 * dt # moves the bullets down when it is shoot
285 | self.rect.y = min(410,self.rect.y) # preevens bullets from going away
286 |
287 |
288 | # moves the bullet on the axis x according to the player's direction
289 | if not (abs(self.rect.left-game.player.rect.left)<=10 or abs(self.rect.right-game.player.rect.right)<=10):
290 | if game.player.rect.left < self.rect.left:
291 | self.rect.x -= (400) * dt
292 | else:
293 | self.rect.x += (400) * dt
294 |
295 | # removes bullets objetcs when they transpass the screen limits
296 | if self.rect.right < 1 or self.rect.left>736 or self.rect.bottom < 1 or self.rect.top > 512:
297 | self.kill()
298 | game.enemy.twists[self.n_twist] = None
299 | return
300 |
301 | # checks collision of enemy's bullet with the player
302 | if self.rect.colliderect(game.player.rect):
303 |
304 | # player loses life points, according to the difficult level of the game (the more difficult, the more it loses).
305 | game.player.life = max(0, game.player.life-(game.level*0.3))
306 |
307 | game.player.hurt = 5 # sets flag to change the player image when he is hurt
308 |
--------------------------------------------------------------------------------
/evoman/enemy2.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map2.tmx'
14 | timeexpire = 1000 # game run limit
15 |
16 | # enemy 2 sprite, airman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 |
20 |
21 | def __init__(self, location, *groups, visuals):
22 |
23 | super(Enemy, self).__init__(*groups)
24 | self.visuals = visuals
25 | if visuals:
26 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
27 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
28 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
29 | self.rect = pygame.rect.Rect(location, self.image.get_size())
30 | else:
31 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
32 | self.direction = -1
33 | self.max_life = 100
34 | self.life = self.max_life
35 | self.resting = 0
36 | self.dy = 0
37 | self.twists = []
38 | self.alternate = 1
39 | self.imune = 0
40 | self.timeenemy = 0
41 | self.hurt = 0
42 | self.shooting = 0
43 | self.gun_cooldown = 0
44 |
45 |
46 | def update(self, dt, game):
47 |
48 |
49 |
50 | if game.time==1:
51 | # puts enemy in random initial position
52 | if game.randomini == 'yes':
53 | self.rect.x = numpy.random.choice([630,610,560,530])
54 |
55 |
56 | # defines game mode for player action.
57 | if game.enemymode == 'static': # enemy controlled by static movements
58 |
59 | if (self.timeenemy >= 210 and self.timeenemy <= 250) or (self.timeenemy >= 260 and self.timeenemy <= 300):
60 | atack1 = 1
61 | else:
62 | atack1 = 0
63 |
64 | if self.timeenemy == 210 or self.timeenemy == 260:
65 | atack2 = 1
66 | else:
67 | atack2 = 0
68 |
69 | if self.timeenemy> 300:
70 | atack3 = 1
71 | else:
72 | atack3 = 0
73 |
74 | if (self.timeenemy == 40) or (self.timeenemy == 110) or (self.timeenemy == 180):
75 | atack4 = 1
76 | else:
77 | atack4 = 0
78 |
79 |
80 | elif game.enemymode == 'ai': # player controlled by AI algorithm
81 |
82 |
83 | # calls the controller providing game sensors
84 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
85 | if len(actions) < 4:
86 | game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
87 | sys.exit(0)
88 |
89 | atack1 = actions[0]
90 | atack2 = actions[1]
91 | atack3 = actions[2]
92 | atack4 = actions[3]
93 |
94 |
95 | if atack4 == 1 and not self.gun_cooldown:
96 | atack4 = 1
97 | else:
98 | atack4 = 0
99 |
100 | if atack1 == 1 and self.resting == 1:
101 | atack1 = 1
102 | else:
103 | atack1 = 0
104 |
105 |
106 | # if the 'start game' marker is 1
107 | if game.start == 1:
108 |
109 | # increments enemy timer
110 | self.timeenemy += 1
111 |
112 | # copies last position state of the enemy
113 | last = self.rect.copy()
114 |
115 | # movements of the enemy on the axis x. Happens 2 to each side.
116 | if atack1 == 1 :
117 | self.rect.x += self.direction * 200 * dt
118 |
119 | # jumps
120 | if atack2 == 1:
121 | self.dy = -900
122 | self.resting = 0
123 |
124 | # animation, running enemy images alternatetion.
125 | if self.direction > 0:
126 | direction = SpriteConstants.RIGHT
127 | else:
128 | direction = SpriteConstants.LEFT
129 |
130 | if self.alternate == 1:
131 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
132 | if self.alternate == 4 or self.alternate == 10:
133 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
134 | if self.alternate == 7:
135 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
136 |
137 | self.alternate += 1
138 | if self.alternate > 12:
139 | self.alternate = 1
140 |
141 | # changes the image when enemy jumps
142 | if self.resting == 0:
143 | if self.direction == -1:
144 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
145 | else:
146 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
147 |
148 | else:
149 | # animation, standing up images
150 | if self.direction == -1:
151 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
152 | else:
153 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
154 |
155 | # restart enemy timer and turns the enemy around
156 | if atack3 == 1:
157 | self.timeenemy = 0
158 | self.direction = self.direction * -1
159 |
160 | # checks collision of the player with the enemy
161 | if self.rect.colliderect(game.player.rect):
162 |
163 | # choses what sprite penalise according to config
164 | if game.contacthurt == "player":
165 | game.player.life = max(0, game.player.life-(game.level*1))
166 | if game.contacthurt == "enemy":
167 | game.enemy.life = max(0, game.enemy.life-(game.level*1))
168 |
169 | game.player.hurt = 5 # sets flag to change the player image when he is hurt.
170 |
171 | # gravity
172 | self.dy = min(400, self.dy + 100)
173 | self.rect.y += self.dy * dt
174 |
175 | # controls screen walls and platforms limits agaist enemy.
176 | new = self.rect
177 | self.resting = 0
178 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
179 |
180 | blockers = cell['blockers']
181 |
182 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
183 | new.right = cell.left
184 |
185 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
186 | new.left = cell.right
187 |
188 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
189 | self.resting = 1
190 | new.bottom = cell.top
191 | self.dy = 0
192 |
193 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
194 | new.top = cell.bottom
195 |
196 | # enemy shoots
197 | if atack4 == 1:
198 |
199 | self.shooting = 5
200 |
201 | self.gun_cooldown = 3
202 |
203 | # bullets sound effect
204 | if game.sound == "on" and game.playermode == "human":
205 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
206 | c = pygame.mixer.Channel(3)
207 | c.set_volume(10)
208 | c.play(sound)
209 |
210 |
211 | # shoots 6 bullets placed in a fixed range
212 | for i in range (0,6):
213 | self.twists.append(Bullet_e2((self.rect.x+10,self.rect.bottom), self.direction ,i, len(self.twists), game.sprite_e, visuals=self.visuals))
214 |
215 | # decreases time for bullets limitation
216 | self.gun_cooldown = max(0, self.gun_cooldown - dt)
217 |
218 | # hurt enemy animation
219 | if self.hurt > 0:
220 | if self.direction == -1:
221 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
222 | else:
223 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
224 |
225 | self.hurt -=1
226 |
227 | # changes bullets images according to the enemy direction
228 | if self.shooting > 0:
229 | if self.direction == -1:
230 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
231 | else:
232 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
233 |
234 | self.shooting -= 1
235 | self.shooting = max(0,self.shooting)
236 |
237 |
238 |
239 | def updateSprite(self, state, direction):
240 | if self.visuals:
241 | self.image = self.spriteDefinition.getImage(state, direction)
242 |
243 | # enemy's bullet
244 | class Bullet_e2(pygame.sprite.Sprite):
245 |
246 |
247 | def __init__(self, location, direction,n, n_twist , *groups, visuals):
248 | super(Bullet_e2, self).__init__(*groups)
249 |
250 | self.rect = pygame.rect.Rect(location, (23, 23))
251 | self.visuals = visuals
252 | if visuals:
253 | self.image = self.image_r = pygame.image.load('evoman/images/torna.png')
254 | self.image_l = pygame.image.load('evoman/images/torna2.png')
255 | self.rect = pygame.rect.Rect(location, self.image_r.get_size())
256 |
257 |
258 | self.direction = direction
259 | self.lifespan = 55
260 | self.n = n
261 | self.n_twist = n_twist
262 |
263 |
264 |
265 | def update(self, dt, game):
266 |
267 | if self.visuals:
268 | if game.time%2==0:
269 | self.image = self.image_r
270 | else:
271 | self.image = self.image_l
272 |
273 |
274 | # removes bullets objetcs when they transpass the screen limits
275 | if self.rect.right < 1 or self.rect.left>736 or self.rect.bottom < 1 or self.rect.top > 512:
276 | self.kill()
277 | game.enemy.twists[self.n_twist] = None
278 | return
279 |
280 | # enemy atack: blows the player forward with the bullets
281 | if self.lifespan > 43:
282 | ax = [100,380,440,270,220,300]
283 | ay = [30,70,120,-40 ,80,130]
284 |
285 | if self.direction == -1:
286 | if self.rect.x >= game.enemy.rect.x - ax[self.n]:
287 | self.rect.x -= 1400 * dt
288 | if self.direction == 1:
289 | if self.rect.x <= game.enemy.rect.x + ax[self.n]:
290 | self.rect.x += 1400 * dt
291 |
292 | if self.rect.y >= game.enemy.rect.y - ay[self.n]:
293 | self.rect.y -= 550 * dt
294 |
295 | elif self.lifespan <= 5:
296 | self.rect.x += self.direction * 650 * dt
297 | game.player.rect.x += self.direction * 150 * dt
298 |
299 | # limitates player in the screen.
300 | if game.player.rect.x < 60:
301 | game.player.rect.x = 60
302 | if game.player.rect.x > 620:
303 | game.player.rect.x = 620
304 |
305 | # decreases bullet's timer
306 | self.lifespan -= 1
307 |
308 | # checks collision of enemy's bullet with the player
309 | if self.rect.colliderect(game.player.rect):
310 |
311 | # player loses life points, according to the difficult level of the game (the more difficult, the more it loses).
312 | game.player.life = max(0, game.player.life-(game.level*1))
313 |
314 | # sets flag to change the player image when he is hurt
315 | game.player.hurt = 5
316 |
317 | # removes player's bullets when colliding with enemy's bullets
318 | aux = 0
319 | for t in game.player.twists:
320 | if t != None:
321 | if self.rect.colliderect(t.rect):
322 | t.kill()
323 | game.player.twists[aux] = None
324 | aux += 1
325 |
--------------------------------------------------------------------------------
/evoman/enemy1.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map1.tmx' # scenario
14 | timeexpire = 1000 # game run limit
15 |
16 |
17 | # enemy 1 sprite, flashman
18 | class Enemy(pygame.sprite.Sprite):
19 |
20 | def __init__(self, location, *groups, visuals):
21 |
22 | super(Enemy, self).__init__(*groups)
23 | self.visuals = visuals
24 | if visuals:
25 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
26 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
28 | self.rect = pygame.rect.Rect(location, self.image.get_size())
29 | else:
30 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
31 | self.direction = -1
32 | self.max_life = 100
33 | self.life = self.max_life
34 | self.resting = 0
35 | self.dy = 0
36 | self.time_colis = 0
37 | self.alternate = 1
38 | self.imune = 0
39 | self.timeenemy = 0
40 | self.twists = []
41 | self.hurt = 0
42 | self.shooting = 1
43 | self.gun_cooldown = 0
44 | self.gun_cooldown2 = 0
45 |
46 |
47 |
48 |
49 | def update(self, dt, game):
50 |
51 |
52 | if game.time==1:
53 | # puts enemy in random initial position
54 | if game.randomini == 'yes':
55 | self.rect.x = numpy.random.choice([640,500,400,300])
56 |
57 |
58 | # increments enemy timer
59 | if game.start == 1:
60 | self.timeenemy += 1
61 |
62 | # defines game mode for player action
63 | if game.enemymode == 'static': # controlled by static movements
64 |
65 | atack1 = 1
66 |
67 | if self.timeenemy >= 200 and self.timeenemy < 260:
68 | atack2 = 1
69 | else:
70 | atack2 = 0
71 |
72 | if self.timeenemy == 220:
73 | atack3 = 1
74 | else:
75 | atack3 = 0
76 |
77 | atack4 = 1
78 |
79 |
80 | elif game.enemymode == 'ai': # enemy controlled by AI algorithm
81 |
82 |
83 | # calls the controller providing game sensors
84 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
85 | if len(actions) < 4:
86 | game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
87 | sys.exit(0)
88 |
89 | atack1 = actions[0]
90 | atack2 = actions[1]
91 | atack3 = actions[2]
92 | atack4 = actions[3]
93 |
94 |
95 | # applies attack rules
96 | if atack2 == 1 and not self.gun_cooldown:
97 | atack2 = 1
98 | else:
99 | atack2 = 0
100 |
101 | if atack3 == 1 and not self.gun_cooldown2:
102 | atack3 = 1
103 | else:
104 | atack3 = 0
105 |
106 |
107 |
108 | # if the enemy is not atacking with the feezing atack (prevents player from making any movements) and also the 'start game' marker is 1.
109 | if game.freeze_e == 0 and game.start == 1:
110 |
111 | last = self.rect.copy()# copies last position state of the enemy
112 |
113 | if atack1 == 1:
114 | # moves the enemy on the axis x
115 | self.rect.x += self.direction * 100 * dt
116 |
117 | # chases player, switching direction as he moves.
118 | if atack4 == 1:
119 |
120 | if game.enemymode == 'static':
121 | if game.player.rect.right < self.rect.left:
122 | self.direction = -1
123 | elif game.player.rect.left > self.rect.right:
124 | self.direction = 1
125 | else:
126 | self.direction = self.direction * -1
127 |
128 | # animation, running enemy images alternation.
129 | if self.direction > 0:
130 | direction = SpriteConstants.RIGHT
131 | else:
132 | direction = SpriteConstants.LEFT
133 |
134 | if self.alternate == 1:
135 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
136 | if self.alternate == 4 or self.alternate == 10:
137 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
138 | if self.alternate == 7:
139 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
140 |
141 | self.alternate += 1
142 | if self.alternate > 12:
143 | self.alternate = 1
144 |
145 | # checks collision of the player with the enemy
146 | if self.rect.colliderect(game.player.rect):
147 |
148 | # sprite loses life points, according to the difficult level of the game (the more difficult, the more it loses).
149 |
150 | # choses what sprite penalise according to config
151 | if game.contacthurt == "player":
152 | game.player.life = max(0, game.player.life-(game.level*1))
153 | if game.contacthurt == "enemy":
154 | game.enemy.life = max(0, game.enemy.life-(game.level*1))
155 |
156 | # counts duration of the collision to jump from time to time during the collision
157 | self.time_colis += 1
158 | if self.time_colis > 15:
159 | self.time_colis = 0
160 | self.dy = -600
161 |
162 | # sets flag to change the player image when he is hurt
163 | game.player.hurt = 5
164 |
165 |
166 | # gravity
167 | self.dy = min(400, self.dy + 100)
168 | self.rect.y += self.dy * dt
169 |
170 | # controls screen walls and platforms limits towards enemy
171 | new = self.rect
172 | self.resting = 0
173 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
174 |
175 | blockers = cell['blockers']
176 |
177 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
178 | self.resting = 1
179 | new.bottom = cell.top
180 | self.dy = 0
181 |
182 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
183 | new.top = cell.bottom
184 |
185 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom>cell.top:
186 | new.right = cell.left
187 | # Jumps when finds a wall in the middle plataforms.
188 | if new.left<600:
189 | self.dy = -600
190 |
191 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom>cell.top:
192 | new.left = cell.right
193 | # Jumps when finds a wall in the middle plataforms.
194 | if new.left>29:
195 | self.dy = -600
196 |
197 | # Changes the image when enemy jumps.
198 | if self.resting == 0:
199 | if self.direction == -1:
200 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
201 | else:
202 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
203 |
204 | # Hurt enemy animation.
205 | if self.hurt > 0:
206 | if self.direction == -1:
207 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
208 | else:
209 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
210 |
211 | self.hurt -=1
212 |
213 |
214 | # Enemy atack: freezes the player (preeveting him from making any movements or atacking) and also himself from moving. Freenzing endures according to the timer.
215 | if atack2 == 1:
216 |
217 | self.gun_cooldown = 6
218 |
219 | game.freeze_p = 1
220 | game.freeze_e = 1
221 |
222 | # Enemy shooting after freezing.
223 | if atack3 == 1:
224 |
225 | self.shooting = 5
226 |
227 | self.gun_cooldown2 = 6
228 |
229 | # Bullets sound effect.
230 | if game.sound == "on" and game.playermode == "human":
231 |
232 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
233 | c = pygame.mixer.Channel(3)
234 | c.set_volume(10)
235 | c.play(sound)
236 |
237 | # Shoots 8 bullets placed in a fixed range with a little random variation in their position (x and y).
238 |
239 |
240 | for i in range (0,8):
241 | rand = numpy.array([30,20,10,15,9,25,18,5])
242 | rand2 = numpy.array([1,2,3,4,5,2,4,3])
243 |
244 | rand = rand[i]
245 | rand2 = rand2[i]
246 |
247 | # Start position of the bullets vary according to the position of the enemy.
248 | if self.direction > 0:
249 | self.twists.append(Bullet_e1((self.rect.x+(i*rand),self.rect.y+10+(i*rand2)), 1, len(self.twists), game.sprite_e, visuals=self.visuals))
250 | else:
251 | self.twists.append(Bullet_e1((self.rect.x-(i*rand)+46,self.rect.y+10+(i*rand2)), -1, len(self.twists), game.sprite_e, visuals=self.visuals))
252 |
253 | # Decreases time for bullets and freezing limitation.
254 | self.gun_cooldown = max(0, self.gun_cooldown - dt)
255 | self.gun_cooldown2 = max(0, self.gun_cooldown2 - dt)
256 |
257 | # Changes bullets images according to the enemy direction.
258 | if self.shooting > 0:
259 | if self.direction == -1:
260 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
261 | else:
262 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
263 |
264 | self.shooting -= 1
265 | self.shooting = max(0,self.shooting)
266 |
267 | # Releases movement.
268 | if self.gun_cooldown <= 5:
269 | game.freeze_p = 0
270 | game.freeze_e = 0
271 |
272 | # Reinicializes enemy atacking timer.
273 | if self.timeenemy == 260:
274 | self.timeenemy = 0
275 |
276 | def updateSprite(self, state, direction):
277 | if self.visuals:
278 | self.image = self.spriteDefinition.getImage(state, direction)
279 |
280 | # Enemy's bullets.
281 | class Bullet_e1(pygame.sprite.Sprite):
282 |
283 |
284 | def __init__(self, location, direction, n_twist, *groups, visuals):
285 | super(Bullet_e1, self).__init__(*groups)
286 | self.rect = pygame.rect.Rect(location, (15, 7))
287 | self.direction = direction
288 | self.n_twist = n_twist
289 |
290 | if visuals:
291 |
292 | # Fits image according to the side the enemy is turned to.
293 | if self.direction == 1:
294 | self.image = pygame.image.load('evoman/images/bullet2_r.png')
295 | else:
296 | self.image = pygame.image.load('evoman/images/bullet2_l.png')
297 | self.rect = pygame.rect.Rect(location, self.image.get_size())
298 |
299 |
300 |
301 | def update(self, dt, game):
302 |
303 | # Removes bullets objetcs when they transpass the screem limits.
304 | if self.rect.right < 1 or self.rect.left>736 or self.rect.bottom < 1 or self.rect.top > 512:
305 | self.kill()
306 | game.enemy.twists[self.n_twist] = None
307 | return
308 |
309 | # Moving on the X axis.
310 | self.rect.x += self.direction * 300 * dt
311 |
312 | # Checks collision of enemy's bullet with the player.
313 | if self.rect.colliderect(game.player.rect):
314 |
315 | # Player loses life points, accoring to the difficult level of the game (the more difficult, the more it loses).
316 | game.player.life = max(0, game.player.life-(game.level*3))
317 |
318 | # Removes the bullet off the screem after collision.
319 | self.kill()
320 | game.enemy.twists[self.n_twist] = None
321 |
322 | # Sets flag to change the player image when he is hurt.
323 | game.player.hurt = 5
324 |
--------------------------------------------------------------------------------
/evoman/enemy4.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map2.tmx'
14 | timeexpire = 1500 # game run limit
15 |
16 | # enemy 4 sprite, heatman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 | def __init__(self, location, *groups, visuals):
20 |
21 | super(Enemy, self).__init__(*groups)
22 | self.visuals = visuals
23 | if visuals:
24 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
25 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
26 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.rect = pygame.rect.Rect(location, self.image.get_size())
28 | else:
29 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
30 | self.direction = -1
31 | self.max_life = 100
32 | self.life = self.max_life
33 | self.resting = 0
34 | self.dy = 0
35 | self.twists = []
36 | self.alternate = 1
37 | self.fireflash = 0
38 | self.imune = 0
39 | self.rect.x = 550
40 | self.timeenemy = 0
41 | self.hurt = 0
42 | self.shooting = 0
43 | self.gun_cooldown = 0
44 | self.rect.right = 580
45 |
46 |
47 | def update(self, dt, game):
48 |
49 |
50 | if game.time==1:
51 | # puts enemy in random initial position
52 | if game.randomini == 'yes':
53 | self.rect.x = numpy.random.choice([640,500,400,300])
54 |
55 |
56 | # defines game mode for player action
57 | if game.enemymode == 'static': # enemy controlled by static movements
58 |
59 | if self.timeenemy == 2:
60 | atack1 = 1
61 | else:
62 | atack1 = 0
63 |
64 | if self.timeenemy> 50:
65 | atack2 = 1
66 | else:
67 | atack2 = 0
68 |
69 | if self.timeenemy == 3:
70 | atack3 = 1
71 | else:
72 | atack3 = 0
73 |
74 | if (self.fireflash>=1 and self.fireflash <=40):
75 | atack4 = 1
76 | else:
77 | atack4 = 0
78 |
79 | elif game.enemymode == 'ai': # player controlled by AI algorithm
80 |
81 |
82 | # calls the controller providing game sensors
83 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
84 | if len(actions) < 4:
85 | game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
86 | sys.exit(0)
87 |
88 | atack1 = actions[0]
89 | atack2 = actions[1]
90 | atack3 = actions[2]
91 | atack4 = actions[3]
92 |
93 | if atack3 == 1 and not self.gun_cooldown:
94 | atack3 = 1
95 | else:
96 | atack3 = 0
97 |
98 |
99 |
100 | # if the 'start game' marker is 1
101 | if game.start == 1:
102 |
103 | self.timeenemy += 1 # increments enemy timer
104 |
105 | last = self.rect.copy() # copies last position state of the enemy
106 |
107 |
108 | # when player atacks, enemy turns into fire and goes towards his direction
109 | if game.player.atacked == 1 and self.fireflash == 0:
110 | self.fireflash = 100
111 | else:
112 | self.fireflash = max(0,self.fireflash -1)
113 |
114 | if atack4 == 1:
115 | self.rect.x += self.direction * 600 * dt
116 |
117 | if self.fireflash == 1:
118 | self.direction = self.direction * -1
119 |
120 | if self.rect.colliderect(game.player.rect):
121 | self.fireflash = 0
122 |
123 | # otherwise he just keeps shooting towards the player direction
124 | elif self.fireflash == 0:
125 |
126 | if atack1 == 1 and self.resting == 1:
127 | self.dy = -900
128 | self.resting = 0
129 |
130 | self.imune = 0 # enemy is not imune to player's shooting anymore
131 |
132 | # images of the enemy standing up
133 | if self.direction == -1:
134 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
135 | else:
136 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
137 |
138 | # reinicializes timer and turns to the players direction
139 | if atack2 == 1:
140 | self.timeenemy = 1
141 |
142 | if game.enemymode == 'static':
143 | if game.player.rect.right < self.rect.left:
144 | self.direction = -1
145 | elif game.player.rect.left > self.rect.right:
146 | self.direction = 1
147 | else:
148 | self.direction = self.direction *-1
149 |
150 |
151 | # checks collision of the player with the enemy
152 | if self.rect.colliderect(game.player.rect):
153 |
154 | # choses what sprite penalise according to config
155 | if game.contacthurt == "player":
156 | game.player.life = max(0, game.player.life-(game.level*0.3))
157 | if game.contacthurt == "enemy":
158 | game.enemy.life = max(0, game.enemy.life-(game.level*0.3))
159 |
160 | # pushes player when he collides with the enemy
161 | game.player.rect.x += self.direction * 50 * dt
162 |
163 | # limits the player to stand on the screen space even being pushed
164 | if game.player.rect.x < 60:
165 | game.player.rect.x = 60
166 | if game.player.rect.x > 620:
167 | game.player.rect.x = 620
168 |
169 | # sets flag to change the player image when he is hurt
170 | game.player.hurt = 5
171 |
172 | # gravity
173 | self.dy = min(400, self.dy + 100)
174 | self.rect.y += self.dy * dt
175 |
176 | # controls screen walls and platforms limits agaist enemy
177 | new = self.rect
178 | self.resting = 0
179 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
180 |
181 | blockers = cell['blockers']
182 |
183 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
184 | new.right = cell.left
185 |
186 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
187 | new.left = cell.right
188 |
189 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
190 | self.resting = 1
191 | new.bottom = cell.top
192 | self.dy = 0
193 |
194 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
195 | new.top = cell.bottom
196 |
197 |
198 | # enemy shoots 3 bullets
199 | if atack3 == 1:
200 |
201 | self.shooting = 5
202 |
203 | self.gun_cooldown = 5
204 |
205 | # if enemy is not turned into fire, shoots, otherwise stops the time counter for a while.
206 | if self.fireflash == 0:
207 |
208 | # bullets sound effect
209 | if game.sound == "on" and game.playermode == "human":
210 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
211 | c = pygame.mixer.Channel(3)
212 | c.set_volume(10)
213 | c.play(sound)
214 |
215 |
216 | for i in range (0,3):
217 | self.twists.append(Bullet_e4((self.rect.x ,self.rect.y ), self.direction, i, len(self.twists), game.sprite_e))
218 | else :
219 | self.timeenemy -= 1
220 |
221 |
222 | self.gun_cooldown = max(0, self.gun_cooldown - dt) # decreases time for bullets limitation.
223 |
224 | # changes bullets images according to the enemy direction
225 | if self.shooting > 0:
226 | if self.direction == -1:
227 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
228 | else:
229 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
230 |
231 | self.shooting -= 1
232 | self.shooting = max(0,self.shooting)
233 |
234 | # changes the image when enemy is hurt and imune, as a fireball
235 | if self.visuals:
236 | if self.imune == 1:
237 | if game.time%2==0:
238 | self.image = pygame.image.load('evoman/images/fireball.png')
239 | else:
240 | self.image = pygame.image.load('evoman/images/fireball2.png')
241 |
242 | self.hurt -=1
243 |
244 | def updateSprite(self, state, direction):
245 | if self.visuals:
246 | self.image = self.spriteDefinition.getImage(state, direction)
247 |
248 |
249 | # enemy bullets
250 | class Bullet_e4(pygame.sprite.Sprite):
251 |
252 | image = pygame.image.load('evoman/images/bullet_l.png')
253 |
254 | def __init__(self, location, direction, n, n_twist, *groups):
255 | super(Bullet_e4, self).__init__(*groups)
256 | self.rect = pygame.rect.Rect(location, self.image.get_size())
257 | self.direction = direction
258 | self.lifespan = 30
259 | self.n= n
260 | self.n_twist = n_twist
261 |
262 | def update(self, dt, game):
263 |
264 |
265 | # puts the bullets in positions relative to the player. They go from the enemy to where the player is.
266 | if self.n == 0:
267 | aux_x = 50
268 | aux_y = (abs(game.player.rect.x - game.enemy.rect.x)*0.55)
269 | elif self.n == 1:
270 | aux_x = 20
271 | aux_y = (abs(game.player.rect.x - game.enemy.rect.x)*0.60)
272 | elif self.n == 2:
273 | aux_x = -10
274 | aux_y = (abs(game.player.rect.x - game.enemy.rect.x)*0.65)
275 |
276 | # bullets axis x movement
277 | if self.direction == -1:
278 | if self.rect.x > game.player.rect.left + aux_x:
279 | self.rect.x += self.direction * 650 * dt
280 | else:
281 | if self.rect.x < game.player.rect.right - aux_x:
282 | self.rect.x += self.direction * 650 * dt
283 |
284 | # bullets axis y movements
285 | if self.direction == -1:
286 | if self.rect.x > game.player.rect.left + aux_y:
287 | self.rect.y -= 500 * dt
288 | else:
289 | self.rect.y += 700 * dt
290 | else:
291 | if self.rect.x < game.player.rect.right - aux_y-10:
292 | self.rect.y -= 500 * dt
293 | else:
294 | self.rect.y += 700 * dt
295 |
296 | # prevents bullets from passing through the floor
297 | self.rect.y = min(410,self.rect.y)
298 |
299 | # removes old bullets
300 | if self.rect.y == 410:
301 | self.lifespan -= 1
302 |
303 | if self.lifespan < 0:
304 | self.kill()
305 | game.enemy.twists[self.n_twist] = None
306 | return
307 |
308 | if self.rect.right<1 or self.rect.left>736 or self.rect.top <1 or self.rect.bottom>512 :
309 | self.kill()
310 | game.enemy.twists[self.n_twist] = None
311 | return
312 |
313 | # checks collision of enemy's bullet with the player
314 | if self.rect.colliderect(game.player.rect):
315 |
316 | # player loses life points, accoring to the difficulty level of the game (the more difficult, the more it loses).
317 | game.player.life = max(0, game.player.life-(game.level*0.3))
318 |
319 | # pushes player when he collides with the enemy
320 | game.player.rect.x += self.direction * 100 * dt
321 |
322 | # limits the player to stand on the screen space even being pushed
323 | if game.player.rect.x < 60:
324 | game.player.rect.x = 60
325 | if game.player.rect.x > 620:
326 | game.player.rect.x = 620
327 |
328 | # sets flag to change the player image when he is hurt
329 | game.player.hurt = 5
330 |
--------------------------------------------------------------------------------
/evoman/enemy5.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map2.tmx'
14 | timeexpire = 1000 # game run limit
15 |
16 | # enemy 5 sprite, metalman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 | def __init__(self, location, *groups, visuals):
20 |
21 | super(Enemy, self).__init__(*groups)
22 | self.visuals = visuals
23 | if visuals:
24 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
25 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
26 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.rect = pygame.rect.Rect(location, self.image.get_size())
28 | else:
29 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
30 | self.direction = -1
31 | self.max_life = 100
32 | self.life = self.max_life
33 | self.resting = 0
34 | self.dy = 0
35 | self.alternate = 1
36 | self.direction_floor = 1
37 | self.imune = 0
38 | self.move = 0
39 | self.countmove = 0
40 | self.rect.x = 500
41 | self.timeenemy = 0
42 | self.twists = []
43 | self.hurt = 0
44 | self.shooting = 0
45 | self.gun_cooldown = 0
46 |
47 |
48 |
49 | def update(self, dt, game):
50 |
51 |
52 | if game.time==1:
53 | # puts enemy in random initial position
54 | if game.randomini == 'yes':
55 | self.rect.x = numpy.random.choice([640,500,400,300])
56 |
57 | # Defines game mode for player action.
58 | if game.enemymode == 'static': # Enemy controlled by static movements.
59 |
60 | if self.resting == 1 and self.timeenemy >= 95 and self.timeenemy <= 110:
61 | atack1 = 1
62 | else:
63 | atack1 = 0
64 |
65 | if self.resting == 0 :
66 | atack2 = 1
67 | else:
68 | atack2 = 0
69 |
70 | if (game.player.rect.right < game.enemy.rect.left and abs(game.player.rect.right - game.enemy.rect.left) <= 50 ) or (game.enemy.rect.right < game.player.rect.left and abs(game.enemy.rect.right - game.player.rect.left) <= 50):
71 | atack3 = 1
72 | else:
73 | atack3 = 0
74 |
75 |
76 | elif game.enemymode == 'ai': # Player controlled by AI algorithm.
77 |
78 |
79 |
80 | # calls the controller providing game sensors
81 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
82 | if len(actions) < 3:
83 | game.print_logs("ERROR: Enemy 1 controller must return 3 decision variables.")
84 | sys.exit(0)
85 |
86 | atack1 = actions[0]
87 | atack2 = actions[1]
88 | atack3 = actions[2]
89 |
90 |
91 |
92 | if atack2 == 1 and not self.gun_cooldown:
93 | atack2 = 1
94 | else:
95 | atack2 = 0
96 |
97 |
98 | # if the 'start game' marker is 1
99 | if game.start == 1:
100 |
101 |
102 | self.timeenemy += 1 # increments enemy timer
103 |
104 | # moving floor, changes the movement direction from time to time.
105 | for cell in game.tilemap.layers['triggers'].collide(game.player.rect, 'blockers'):
106 |
107 | blockers = cell['blockers']
108 | if 't' in blockers:
109 |
110 | game.player.rect.x += self.direction_floor * 100 * dt # moves player over the moving floor
111 |
112 | # limits player inside the screen
113 | if game.player.rect.left < 60:
114 | game.player.rect.left = 61
115 | if game.player.rect.right > 665:
116 | game.player.rect.right = 665
117 |
118 | if game.time%120 == 0:
119 | self.direction_floor = self.direction_floor *-1
120 |
121 |
122 | last = self.rect.copy() # copies last position state of the enemy
123 |
124 | # if player gets too close to the enemy, he jumps to the other side.
125 | if (self.resting == 1 and atack3 == 1):
126 | self.move = 1
127 | self.dy = -900
128 | self.resting = 0
129 |
130 | if self.move == 1:
131 | self.rect.x += self.direction * 900 * dt
132 |
133 | if self.move == 1 and self.rect.x<200:
134 | self.rect.x = 200
135 | self.direction = self.direction * -1
136 | self.move = 0
137 | if self.move == 1 and self.rect.x>500:
138 | self.rect.x = 500
139 | self.direction = self.direction * -1
140 | self.move = 0
141 |
142 |
143 | # jumps from time to time or when player atacks
144 | if ( (self.resting == 1 and atack1 == 1) or ( self.resting == 1 and game.player.atacked == 1)):
145 | self.dy = -900
146 | self.resting = 0
147 |
148 | # releases until 4 bullets (decided randomly) after jumping or when player atacks
149 | if (atack2 == 1 and not self.gun_cooldown) :
150 |
151 | self.shooting = 5
152 |
153 | self.gun_cooldown = 3
154 |
155 | # bullets sound effect
156 | if game.sound == "on" and game.playermode == "human":
157 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
158 | c = pygame.mixer.Channel(3)
159 | c.set_volume(10)
160 | c.play(sound)
161 |
162 | #aux = numpy.random.randint(1,4) no more randomness if all other enemies are deterministic
163 | aux = 2
164 | for i in range(0,aux):
165 | self.twists.append(Bullet_e5((self.rect.x + (self.direction*(i*30)) ,self.rect.top + (self.direction*(i*20)) ), self.direction, game.player.rect , len(self.twists), game.sprite_e))
166 |
167 |
168 | self.timeenemy = 0 # reinicializes enemy timer
169 |
170 | if game.player.atacked == 1:
171 | # bullets sound effect
172 | if game.sound == "on" and game.playermode == "human":
173 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
174 | c = pygame.mixer.Channel(3)
175 | c.set_volume(10)
176 | c.play(sound)
177 |
178 | self.twists.append(Bullet_e5((self.rect.x ,self.rect.top ), self.direction, game.player.rect , len(self.twists), game.sprite_e))
179 |
180 |
181 | self.gun_cooldown = max(0, self.gun_cooldown - dt) # decreases time for bullets limitation
182 |
183 | # animation, running enemy images alternation.
184 | if self.direction > 0:
185 | direction = SpriteConstants.RIGHT
186 | else:
187 | direction = SpriteConstants.LEFT
188 |
189 | if self.alternate == 1:
190 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
191 | if self.alternate == 4 or self.alternate == 10:
192 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
193 | if self.alternate == 7:
194 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
195 |
196 | self.alternate += 1
197 | if self.alternate > 12:
198 | self.alternate = 1
199 |
200 | # changes the image when enemy jumps
201 | if self.resting == 0:
202 | if self.direction == -1:
203 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
204 | else:
205 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
206 |
207 | # checks collision of the player with the enemy
208 | if self.rect.colliderect(game.player.rect):
209 |
210 | # choses what sprite penalise according to config
211 | if game.contacthurt == "player":
212 | game.player.life = max(0, game.player.life-(game.level*0.3))
213 | if game.contacthurt == "enemy":
214 | game.enemy.life = max(0, game.enemy.life-(game.level*0.3))
215 |
216 | # pushes player when he collides with the enemy
217 | game.player.rect.x += self.direction * 50 * dt
218 |
219 | # limits the player to stand on the screen space even being pushed.
220 | if game.player.rect.x < 60:
221 | game.player.rect.x = 60
222 | if game.player.rect.x > 620:
223 | game.player.rect.x = 620
224 |
225 | # gravity
226 | self.dy = min(400, self.dy + 100)
227 | self.rect.y += self.dy * dt
228 |
229 | # controls screen walls and platforms limits agaist enemy
230 | new = self.rect
231 | self.resting = 0
232 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
233 |
234 | blockers = cell['blockers']
235 |
236 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
237 | new.right = cell.left
238 |
239 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
240 | new.left = cell.right
241 |
242 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
243 | self.resting = 1
244 | new.bottom = cell.top
245 | self.dy = 0
246 |
247 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
248 | new.top = cell.bottom
249 |
250 | # hurt enemy animation
251 | if self.hurt > 0:
252 | if self.direction == -1:
253 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
254 | else:
255 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
256 |
257 | self.hurt -=1
258 |
259 | # changes bullets images according to the enemy direction
260 | if self.shooting > 0:
261 | if self.direction == -1:
262 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
263 | else:
264 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
265 |
266 | self.shooting -= 1
267 | self.shooting = max(0,self.shooting)
268 |
269 | def updateSprite(self, state, direction):
270 | if self.visuals:
271 | self.image = self.spriteDefinition.getImage(state, direction)
272 |
273 | # enemy bullets
274 | class Bullet_e5(pygame.sprite.Sprite):
275 |
276 |
277 |
278 | image = pygame.image.load('evoman/images/blade.png')
279 |
280 | def __init__(self, location, direction, pos_p, n_twist, *groups):
281 | super(Bullet_e5, self).__init__(*groups)
282 | self.rect = pygame.rect.Rect(location, self.image.get_size())
283 | self.direction = direction
284 | self.pos_p = pos_p
285 | self.n_twist = n_twist
286 |
287 |
288 | def update(self, dt, game):
289 |
290 |
291 | # bullets go the player's direction marked at the shooting time
292 | self.rect.x += self.direction * 550 * dt
293 | if self.rect.bottom < self.pos_p.bottom:
294 | self.rect.y += 300 * dt
295 |
296 | # removes bullets objetcs when they transpass the screen limits
297 | if self.rect.right < 1 or self.rect.left>736 or self.rect.bottom < 1 or self.rect.top > 512:
298 | self.kill()
299 | game.enemy.twists[self.n_twist] = None
300 | return
301 |
302 | # checks collision of enemy's bullet with the player
303 | if self.rect.colliderect(game.player.rect):
304 |
305 | # player loses life points, according to the difficulty level of the game (the more difficult, the more it loses).
306 | game.player.life = max(0, game.player.life-(game.level*0.3))
307 |
308 |
309 | game.player.rect.x += self.direction * 100 * dt # pushes player when he collides with the enemy
310 |
311 | # limits the player to stand on the screen space even being pushed
312 | if game.player.rect.x < 60:
313 | game.player.rect.x = 60
314 | if game.player.rect.x > 620:
315 | game.player.rect.x = 620
316 |
317 |
318 | game.player.hurt = 5 # sets flag to change the player image when he is hurt
319 |
--------------------------------------------------------------------------------
/evoman/enemy3.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | import numpy
10 |
11 | from evoman.Base.SpriteDefinition import *
12 |
13 | tilemap = 'evoman/map2.tmx'
14 | timeexpire = 1000 # game run limit
15 |
16 | # enemy 3 sprite, woodman
17 | class Enemy(pygame.sprite.Sprite):
18 |
19 | def __init__(self, location, *groups, visuals):
20 |
21 | super(Enemy, self).__init__(*groups)
22 | self.visuals = visuals
23 | if visuals:
24 | self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
25 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
26 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
27 | self.rect = pygame.rect.Rect(location, self.image.get_size())
28 | else:
29 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
30 | self.direction = -1
31 | self.max_life = 100
32 | self.life = self.max_life
33 | self.resting = 0
34 | self.dy = 0
35 | self.twists = []
36 | self.alternate = 1
37 | self.imune = 0
38 | self.timeenemy = 0
39 | self.hurt = 0
40 | self.shooting = 0
41 | self.gun_cooldown = 0
42 |
43 |
44 | def update(self, dt, game):
45 |
46 |
47 | if game.time==1:
48 | # puts enemy in random initial position
49 | if game.randomini == 'yes':
50 | self.rect.x = numpy.random.choice([640,500,400,300])
51 |
52 | # defines game mode for player action
53 | if game.enemymode == 'static': # enemy controlled by static movements
54 |
55 | if self.timeenemy >= 120 and self.timeenemy <= 140:
56 | atack1 = 1
57 | else:
58 | atack1 = 0
59 |
60 | if self.timeenemy == 130:
61 | atack2 = 1
62 | else:
63 | atack2 = 0
64 |
65 | if self.timeenemy> 140:
66 | atack3 = 1
67 | else:
68 | atack3 = 0
69 |
70 | if self.timeenemy == 30:
71 | atack4 = 1
72 | else:
73 | atack4 = 0
74 |
75 |
76 | elif game.enemymode == 'ai': # player controlled by AI algorithm
77 |
78 |
79 | # calls the controller providing game sensors
80 | actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
81 | if len(actions) < 4:
82 | game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
83 | sys.exit(0)
84 |
85 | atack1 = actions[0]
86 | atack2 = actions[1]
87 | atack3 = actions[2]
88 | atack4 = actions[3]
89 |
90 |
91 | if atack4 == 1 and not self.gun_cooldown:
92 | atack4 = 1
93 | else:
94 | atack4 = 0
95 |
96 |
97 | # if 'start game' is true
98 | if game.start == 1:
99 |
100 | self.timeenemy += 1 # increments enemy timer
101 |
102 |
103 | # copies last position state of the enemy
104 | last = self.rect.copy()
105 |
106 | # movements of the enemy on the axis x
107 | if atack1 == 1:
108 |
109 | self.rect.x += self.direction * 180 * dt # goes forward
110 | # jumps
111 | if atack2 == 1 and self.resting == 1:
112 | self.dy = -700
113 | self.resting = 0
114 |
115 | # animation, running enemy images alternatetion
116 | if self.direction > 0:
117 | direction = SpriteConstants.RIGHT
118 | else:
119 | direction = SpriteConstants.LEFT
120 |
121 | if self.alternate == 1:
122 | self.updateSprite(SpriteConstants.START_RUNNING, direction)
123 | if self.alternate == 4 or self.alternate == 10:
124 | self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
125 | if self.alternate == 7:
126 | self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
127 |
128 | self.alternate += 1
129 | if self.alternate > 12:
130 | self.alternate = 1
131 |
132 | # changes the image when enemy jumps
133 | if self.resting == 0:
134 | if self.direction == -1:
135 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
136 | else:
137 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
138 |
139 | else:
140 | # animation, standing up images
141 | if self.direction == -1:
142 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
143 | else:
144 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
145 |
146 |
147 | # restart enemy's timer from time to time, so that he stops moving.
148 | if atack3 == 1:
149 | self.timeenemy = 20
150 | # puts the enemy turned to the player's direction
151 | if game.enemymode == 'static':
152 | if game.player.rect.right < self.rect.left:
153 | self.direction = -1
154 | elif game.player.rect.left > self.rect.right:
155 | self.direction = 1
156 | else:
157 | self.direction = self.direction * -1
158 |
159 | # checks collision of the player with the enemy
160 | if self.rect.colliderect(game.player.rect):
161 |
162 | # choses what sprite penalise according to config
163 | if game.contacthurt == "player":
164 | game.player.life = max(0, game.player.life-(game.level*1))
165 | if game.contacthurt == "enemy":
166 | game.enemy.life = max(0, game.enemy.life-(game.level*1))
167 |
168 | # pushes player when he collides with the enemy
169 | game.player.rect.x += self.direction * 50 * dt
170 |
171 | # limits the player to stand on the screem space even being pushed
172 | if game.player.rect.x < 60:
173 | game.player.rect.x = 60
174 | if game.player.rect.x > 620:
175 | game.player.rect.x = 620
176 |
177 | # sets flag to change the player image when he is hurt
178 | game.player.hurt = 5
179 |
180 | # gravity
181 | self.dy = min(400, self.dy + 100)
182 | self.rect.y += self.dy * dt
183 |
184 | # controls screen walls and platforms limits agaist enemy
185 | new = self.rect
186 | self.resting = 0
187 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
188 |
189 | blockers = cell['blockers']
190 |
191 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
192 | new.right = cell.left
193 |
194 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
195 | new.left = cell.right
196 |
197 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
198 | self.resting = 1
199 | new.bottom = cell.top
200 | self.dy = 0
201 |
202 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
203 | new.top = cell.bottom
204 |
205 |
206 | # enemy shoots
207 | if atack4 == 1:
208 |
209 | self.shooting = 5
210 | self.gun_cooldown = 3
211 |
212 | # bullets sound effect
213 | if game.sound == "on" and game.playermode == "human":
214 | sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
215 | c = pygame.mixer.Channel(3)
216 | c.set_volume(10)
217 | c.play(sound)
218 |
219 | # shoots 4 bullets placed in fixed places - bullets coming from the enemy.
220 | for i in range (0,4):
221 |
222 | ay = [-10,-10,20,-45]
223 |
224 | if self.direction > 0:
225 | ax = [-24,50,1,1]
226 | self.twists.append(Bullet_e3((self.rect.x+ax[i],self.rect.y-ay[i]), 1, 'h', len(self.twists), game.sprite_e,visuals=self.visuals))
227 | else:
228 | ax = [25,-50,-7,-7]
229 | self.twists.append(Bullet_e3((self.rect.x-ax[i],self.rect.y-ay[i]), -1, 'h', len(self.twists), game.sprite_e,visuals=self.visuals))
230 |
231 | # shoots 4 bullets placed in fixed places - bullets coming from the top of the screen
232 | aux = 100
233 | for i in range (0,4):
234 | self.twists.append(Bullet_e3((aux,100), 1, 'v',len(self.twists),game.sprite_e,visuals=self.visuals))
235 | aux = aux + 150
236 |
237 | # decreases time for bullets limitation
238 | self.gun_cooldown = max(0, self.gun_cooldown - dt)
239 |
240 | # hurt enemy animation
241 | if self.hurt > 0:
242 | if self.direction == -1:
243 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
244 | else:
245 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
246 |
247 | self.hurt -=1
248 |
249 | # changes bullets images according to the enemy direction
250 | if self.shooting > 0:
251 | if self.direction == -1:
252 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
253 | else:
254 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
255 |
256 | self.shooting -= 1
257 | self.shooting = max(0,self.shooting)
258 |
259 |
260 | def updateSprite(self, state, direction):
261 | if self.visuals:
262 | self.image = self.spriteDefinition.getImage(state, direction)
263 |
264 | # enemy's bullet
265 | class Bullet_e3(pygame.sprite.Sprite):
266 |
267 |
268 |
269 | image = pygame.image.load('evoman/images/met.png')
270 |
271 | def __init__(self, location, direction, btype, n_twist, *groups, visuals):
272 | super(Bullet_e3, self).__init__(*groups)
273 | self.rect = pygame.rect.Rect(location, self.image.get_size())
274 | self.direction = direction
275 | self.lifespan = 100
276 | self.btype = btype
277 | self.swingtime = 0
278 | self.n_twist = n_twist
279 | self.visuals=visuals
280 |
281 |
282 |
283 | def update(self, dt, game):
284 |
285 | if self.visuals:
286 | if game.time%2==0:
287 | self.image = pygame.image.load('evoman/images/met.png')
288 | else:
289 | self.image = pygame.image.load('evoman/images/met2.png')
290 |
291 |
292 | # decreases bullet's timer
293 | self.lifespan -= 1
294 |
295 | # removes bullets objetcs when they transpass the screen limits
296 | if self.rect.right < 1 or self.rect.left>736 or self.rect.bottom < 1 or self.rect.top > 512:
297 | self.kill()
298 | game.enemy.twists[self.n_twist] = None
299 | return
300 |
301 | # moves the bullets
302 | if self.btype == 'h': # bullets that come from the enemy
303 | if self.lifespan <= 50:
304 | self.rect.x += self.direction * 550 * dt
305 | else:
306 | if self.lifespan <= 60: # bullets that come from the top
307 | self.rect.y += 300 * dt
308 |
309 | # animation of the bullets swinging
310 | self.swingtime += 1
311 |
312 | if self.swingtime == 10:
313 | self.rect.x += self.direction * 1000 * dt
314 | self.direction = self.direction * -1
315 | self.swingtime = 0
316 |
317 | # checks collision of enemy's bullet with the player
318 | if self.rect.colliderect(game.player.rect):
319 |
320 | # player loses life points, accoring to the difficult level of the game (the more difficult, the more it loses).
321 | game.player.life = max(0, game.player.life-(game.level*1))
322 |
323 | # pushes player when he collides with the enemy
324 | game.player.rect.x += self.direction * 100 * dt
325 |
326 | # limits the player to stand on the screen space even being pushed.
327 | if game.player.rect.x < 60:
328 | game.player.rect.x = 60
329 | if game.player.rect.x > 620:
330 | game.player.rect.x = 620
331 |
332 | # sets flag to change the player image when he is hurt
333 | game.player.hurt = 5
334 |
335 | # removes player's bullets when colliding with enemy's bullets
336 | aux = 0
337 | for t in game.player.twists:
338 | if t != None:
339 | if self.rect.colliderect(t.rect):
340 | t.kill()
341 | game.player.twists[aux] = None
342 | aux += 1
343 |
--------------------------------------------------------------------------------
/evoman/player.py:
--------------------------------------------------------------------------------
1 | ################################
2 | # EvoMan FrameWork - V1.0 2016 #
3 | # Author: Karine Miras #
4 | # karine.smiras@gmail.com #
5 | ################################
6 |
7 | import sys
8 |
9 | from evoman.Base.SpriteDefinition import *
10 |
11 |
12 | # player proctile
13 | class Bullet_p(pygame.sprite.Sprite):
14 |
15 | def __init__(self, location, direction, n_twist, *groups, visuals):
16 | super(Bullet_p, self).__init__(*groups)
17 | self.rect = pygame.rect.Rect(location, (13, 7))
18 | self.direction = direction
19 | self.n_twist = n_twist
20 |
21 | if visuals:
22 | self.image_r = pygame.image.load('evoman/images/bullet_r.png')
23 | self.image_l = pygame.image.load('evoman/images/bullet_l.png')
24 |
25 | if self.direction == 1:
26 | self.image = self.image_r
27 | else:
28 | self.image = self.image_l
29 |
30 | self.rect = pygame.rect.Rect(location, self.image.get_size())
31 |
32 | def update(self, dt, game):
33 |
34 | # removes bullets objetcs when they transpass the screen limits
35 | if self.rect.right < 1 or self.rect.left > 736 or self.rect.top < 1 or self.rect.bottom > 512:
36 | self.kill()
37 | game.player.twists[self.n_twist] = None
38 | return
39 |
40 | self.rect.x += self.direction * 600 * dt # moving on the X axis (left or tight). It adds 600*dt forward at each general game loop loop iteration, where dt controls the frames limit.
41 |
42 | # checks collision of player's bullet with the enemy
43 | if self.rect.colliderect(game.enemy.rect):
44 |
45 | # if enemy is not imune
46 | if game.enemy.imune == 0:
47 | # enemy loses life points, according to the difficult level of the game (the more difficult, the less it loses)
48 | game.enemy.life = max(0, game.enemy.life - (20 / game.level))
49 |
50 | if game.enemyn == 4:
51 | # makes enemy imune to player's shooting.
52 | game.enemy.imune = 1
53 |
54 | # removes the bullet off the screen after collision.
55 | self.kill()
56 | game.player.twists[self.n_twist] = None
57 |
58 | game.enemy.hurt = 5
59 |
60 |
61 | # player sprite
62 | class Player(pygame.sprite.Sprite):
63 |
64 | def __init__(self, location, enemyn, level, *groups, visuals):
65 | super(Player, self).__init__(*groups)
66 |
67 | self.visuals = visuals
68 | if visuals:
69 | self.spriteDefinition = SpriteDefinition('evoman/images/EvoManSprites.png', 0, 0, 43, 59)
70 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
71 | self.image = self.spriteDefinition.getImage(SpriteConstants.STANDING, SpriteConstants.RIGHT)
72 | self.rect = pygame.rect.Rect(location, self.image.get_size())
73 | else:
74 | self.rect = pygame.rect.Rect(location, pygame.Surface([43, 59]).get_size())
75 |
76 | self.resting = 0
77 | self.dy = 0
78 | self.direction = 1
79 | self.alternate = 1
80 | self.gun_cooldown = 0
81 | self.max_life = 100
82 | self.life = self.max_life
83 | self.atacked = 0
84 | self.hurt = 0
85 | self.shooting = 0
86 | self.inwater = 0
87 | self.twists = []
88 | self.vx = 0
89 | self.vy = 0
90 | self.hy = 0
91 | self.sensors = None
92 |
93 | def update(self, dt, game):
94 |
95 | if game.freeze_p != 0 or game.start != 1:
96 | game.tilemap.set_focus(self.rect.x, self.rect.y)
97 | return
98 | # if the enemies are not atacking with the freezing atack (prevents player from making any movements or atacking) and also the 'start game' marker is 1.
99 |
100 | # checks water environment flag to regulate movements speed
101 | if self.inwater == 1:
102 | self.vx = 0.5
103 | self.vy = 0.5
104 | self.hy = -2000
105 | else:
106 | self.vx = 1
107 | self.vy = 1
108 | self.hy = -900
109 |
110 | jump, left, release, right, shoot = self.get_input(game)
111 |
112 | # if the button is released before the jumping maximum height, them player stops going up.
113 | if release == 1 and self.resting == 0:
114 | self.dy = 0
115 |
116 | # copies last position state of the player
117 | last = self.rect.copy()
118 |
119 | # movements on the axis x (left)
120 | if left:
121 |
122 | self.handle_left(dt)
123 |
124 | # movements on the axis x (right)
125 | elif right:
126 |
127 | self.handle_right(dt)
128 |
129 |
130 | else:
131 | # animation, standing up images
132 | if self.direction == -1:
133 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
134 | else:
135 | self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)
136 |
137 | # if player is touching the floor, he is allowed to jump
138 | if self.resting == 1 and jump == 1:
139 | self.dy = self.hy
140 |
141 | # gravity
142 | self.dy = min(400, self.dy + 100)
143 | self.rect.y += self.dy * dt * self.vy
144 |
145 | new = self.rect # copies new (after movement) position state of the player
146 |
147 | # controls screen walls and platforms limits agaist player
148 | self.check_blockers(game, last, new)
149 |
150 | # shoots, limiting time between bullets.
151 | self.handle_shoot(game, shoot)
152 |
153 | # decreases time for limitating bullets
154 | self.gun_cooldown = max(0, self.gun_cooldown - dt)
155 |
156 | self.hurt -= 1
157 | self.hurt = max(0, self.hurt)
158 | self.shooting -= 1
159 | self.shooting = max(0, self.shooting)
160 |
161 | # kills player in case he touches killers stuff, like spikes.
162 | for cell in game.tilemap.layers['triggers'].collide(self.rect, 'killers'):
163 | game.player.life = 0
164 |
165 | self.update_animation(game, new)
166 |
167 | def get_input(self, game):
168 | # defines game mode for player action
169 | if game.playermode == 'human': # player controlled by keyboard/joystick
170 |
171 | jump, left, release, right, shoot = self.human_input(game)
172 |
173 | elif game.playermode == 'ai': # player controlled by AI algorithm
174 |
175 | jump, left, release, right, shoot = self.ai_input(game)
176 | return jump, left, release, right, shoot
177 |
178 | def update_animation(self, game, new):
179 | # changes the image when player jumps
180 | if self.resting == 0:
181 | if self.direction == -1:
182 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
183 | else:
184 | self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
185 | # hurt player animation
186 | if self.hurt > 0:
187 | if self.direction == -1:
188 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
189 | else:
190 | self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)
191 | # shooting animation
192 | if self.shooting > 0:
193 | if self.resting == 0:
194 | if self.direction == -1:
195 | self.updateSprite(SpriteConstants.SHOOTING_JUMPING, SpriteConstants.LEFT)
196 | else:
197 | self.updateSprite(SpriteConstants.SHOOTING_JUMPING, SpriteConstants.RIGHT)
198 | else:
199 | if self.direction == -1:
200 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
201 | else:
202 | self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
203 | # focuses screen center on player
204 | if self.visuals:
205 | game.tilemap.set_focus(new.x, new.y)
206 |
207 | def handle_shoot(self, game, shoot):
208 | if shoot == 1 and not self.gun_cooldown:
209 |
210 | self.shooting = 5
211 | self.atacked = 1 # marks if the player has atacked enemy
212 |
213 | # creates bullets objects according to the direction.
214 | if self.direction > 0:
215 | self.twists.append(
216 | Bullet_p(self.rect.midright, 1, len(self.twists), game.sprite_p, visuals=self.visuals))
217 |
218 | else:
219 | self.twists.append(
220 | Bullet_p(self.rect.midleft, -1, len(self.twists), game.sprite_p, visuals=self.visuals))
221 |
222 | self.gun_cooldown = 0.4 # marks time to the bullet for allowing next bullets
223 |
224 | # sound effects
225 | if game.sound == "on" and game.playermode == "human":
226 | sound = pygame.mixer.Sound('evoman/sounds/scifi003.wav')
227 | c = pygame.mixer.Channel(2)
228 | c.set_volume(1)
229 | c.play(sound)
230 |
231 | else:
232 | self.atacked = 0
233 |
234 | def check_blockers(self, game, last, new):
235 | self.resting = 0
236 | for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):
237 |
238 | blockers = cell['blockers']
239 |
240 | if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom > cell.top:
241 | new.right = cell.left
242 |
243 | if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom > cell.top:
244 | new.left = cell.right
245 |
246 | if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
247 | self.resting = 1 # player touches the floor
248 | new.bottom = cell.top
249 | self.dy = 0
250 |
251 | if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
252 | new.top = cell.bottom
253 |
254 | def human_input(self, game):
255 | # if joystick is connected, initializes it.
256 | if game.joy > 0:
257 | joystick = pygame.joystick.Joystick(0)
258 | joystick.init()
259 | # tests if the button/key was pressed or released.
260 | # if the player is jumping, the release stops the jump before its maximum high is achieved
261 | press = 0
262 | release = 0
263 | for event in game.event:
264 | if event.type == pygame.JOYBUTTONDOWN or event.type == pygame.KEYDOWN:
265 | press = 1
266 | else:
267 | press = 0
268 |
269 | if event.type == pygame.JOYBUTTONUP or event.type == pygame.KEYUP:
270 | release = 1
271 | else:
272 | release = 0
273 | # gets pressed key value
274 | key = pygame.key.get_pressed()
275 | # gets joystick value for axis x (left/right)
276 | left = 0
277 | if game.joy > 0:
278 | if round(joystick.get_axis(0)) == -1:
279 | left = 1
280 | if key[pygame.K_LEFT]:
281 | left = 1
282 | right = 0
283 | if game.joy > 0:
284 | if round(joystick.get_axis(0)) == 1:
285 | right = 1
286 | if key[pygame.K_RIGHT]:
287 | right = 1
288 | # gets joystick/key value for jumping
289 | jump = 0
290 | if game.joy > 0:
291 | if int(joystick.get_button(2)) == 1 and press == 1:
292 | jump = 1
293 | if key[pygame.K_SPACE] and press == 1:
294 | jump = 1
295 | # gets joystick/key value for shooting
296 | shoot = 0
297 | if game.joy > 0:
298 | if int(joystick.get_button(3)) == 1 and press == 1:
299 | shoot = 1
300 | if key[pygame.K_LSHIFT] and press == 1:
301 | shoot = 1
302 | return jump, left, release, right, shoot
303 |
304 | def ai_input(self, game):
305 | if game.time == 1:
306 | game.player_controller.set(game.pcont, len(self.sensors.get(game)))
307 | # calls the controller providing game sensors
308 | actions = game.player_controller.control(self.sensors.get(game), game.pcont)
309 | if len(actions) < 5:
310 | game.print_logs("ERROR: Player controller must return 5 decision variables.")
311 | sys.exit(0)
312 | left = actions[0]
313 | right = actions[1]
314 | jump = actions[2]
315 | shoot = actions[3]
316 | release = actions[4]
317 | return jump, left, release, right, shoot
318 |
319 | def handle_right(self, dt):
320 | self.rect.x += 200 * dt * self.vx
321 | self.direction = 1
322 | # animation, running player images alternation
323 | if self.alternate == 1:
324 | self.updateSprite(SpriteConstants.START_RUNNING, SpriteConstants.RIGHT)
325 | if self.alternate == 4 or self.alternate == 10:
326 | self.updateSprite(SpriteConstants.RUNNING_STEP1, SpriteConstants.RIGHT)
327 | if self.alternate == 7:
328 | self.updateSprite(SpriteConstants.RUNNING_STEP2, SpriteConstants.RIGHT)
329 | self.alternate += 1
330 | if self.alternate > 12:
331 | self.alternate = 1
332 |
333 | def handle_left(self, dt):
334 | self.rect.x -= 200 * dt * self.vx
335 | self.direction = -1
336 | # animation, running images alternation
337 | if self.alternate == 1:
338 | self.updateSprite(SpriteConstants.START_RUNNING, SpriteConstants.LEFT)
339 | if self.alternate == 4 or self.alternate == 10:
340 | self.updateSprite(SpriteConstants.RUNNING_STEP1, SpriteConstants.LEFT)
341 | if self.alternate == 7:
342 | self.updateSprite(SpriteConstants.RUNNING_STEP2, SpriteConstants.LEFT)
343 | self.alternate += 1
344 | if self.alternate > 12:
345 | self.alternate = 1
346 |
347 | def updateSprite(self, state, direction):
348 | if self.visuals:
349 | self.image = self.spriteDefinition.getImage(state, direction)
350 |
--------------------------------------------------------------------------------