项目作者: yoyoberenguer

项目描述 :
2D video game guided missile algorithm
高级语言: Python
项目地址: git://github.com/yoyoberenguer/Homing-missile.git
创建时间: 2019-04-21T11:15:21Z
项目社区:https://github.com/yoyoberenguer/Homing-missile

开源协议:MIT License

下载


Homing Missile Library

The project is under the GNU GENERAL PUBLIC LICENSE Version 3

Building project from source

1) Download the latest release from here:

example ==> https://github.com/yoyoberenguer/Homing-missile/releases/tag/v1.0.2
2) Decompress the archive and enter the project directory

  1. c:\>cd Homing-missile-1.0.2
  2. Refers to section Building Cython & C
  3. C:\>python setup.py build_ext --inplace --force

Run the demo

In Homing-missile-1.0.2

  1. C:\>cd HomingMissile
  2. C:\>python homingmissile.py
Key Assignement
ARROW KEYS (LEFT, RIGHT, UP and DOWN) for spaceship direction
SPACEBAR Fire missiles
PAUSE/BREAK Pause the demo for 2 secs
ESC to quit
Mouse Move target

Preview

alt text

alt text

Missile attributes

The XML file Weapon.xml contains 4 different missile type:

  • STINGER
  • BUMBLEBEE
  • WASP
  • HORNET
  1. Missile properties/attributes can be edited and changed in the XML.
  2. You can change the missile velocity, reloading time, amount of damage, turn radius per frame etc
  3. e.g
  4. velocity='-15' | damage='1050' | max_rotation='10'
  5. You can also change the missile trail sprite animation to your liking, e.g
  6. missile_trail_fx="MISSILE_TRAIL_DICT2", you can passed a different sprite animation (check the file
  7. `textures.py` for more details on how to create a sprite animation)
  8. Sound effect can also be changed with the variable `propulsion_sound_fx`
  1. <?xml version="1.0"?>
  2. <class>
  3. <category name = "MISSILE">
  4. <modes>
  5. <mode name = "GUIDED">
  6. <weapon name="STINGER"
  7. type="Missile"
  8. image="STINGER_IMAGE"
  9. sprite_orientation="90"
  10. sprite_rotozoom ="STINGER_ROTATE_BUFFER"
  11. propulsion_sound_fx="STINGER_EXHAUST_SOUND"
  12. missile_trail_fx="MISSILE_TRAIL_DICT2"
  13. missile_trail_fx_blend = "pygame.BLEND_RGB_ADD"
  14. animation='None'
  15. range='SCREENRECT.h'
  16. bingo_range = '(100, 120)'
  17. velocity='-15'
  18. damage='1050'
  19. timestamp='0'
  20. reloading_time='2'
  21. detonation_dist='None'
  22. max_rotation='10'>

Missiles classes

The file Weapon.pyx contains all the missile class such as

  • HomingMissile
    1. PURE PURSUIT ALGORITHM
    2. Guided ballistic missile :
    3. This missile adjust its direction by adding small degrees values (turn radius) until
    4. reaching the optimal angle difference (0 degrees).
    5. The missile is in on collision course when the angle between the missile heading and the
    6. target heading difference is approximately null.
  • InterceptMissile
    1. LEAD COLLISION (proportional navigation) more effective, follow an optimal path
    2. Intercept theorem (Thales basic proportionality theorem)
    3. https://www.youtube.com/watch?v=T2fPKUfmnKo
    4. https://codereview.stackexchange.com/questions/86421/line-segment-to-circle-collision-algorithm
  • AdaptiveMissile
    1. Homing missile (guided missile) with automatic gradual acceleration/deceleration.
    2. This projectile is capable of very sharp angle.
    3. It can accelerate in straight trajectory and decelerate in the turns to increase manoeuvrability
    This file contains also the following methods
  • fast_lead_collision (Written in Cython and provide a very fast calculation for missile intercept, returns a vector)
  • lead_collision (written in Cython, this method returns a structure containing both, vectors and collision point)

Code

Check HomingMissile.py to see how to use the missile library.

1 - load the XML data for each missile
e.g

  1. # Load the missile from xml file
  2. STINGER_XML = dict(xml_get_weapon('Weapon.xml', 'STINGER'))
  3. BUMBLEBEE_XML = dict(xml_get_weapon('Weapon.xml', 'BUMBLEBEE'))
  4. WASP_XML = dict(xml_get_weapon('Weapon.xml', 'WASP'))
  5. HORNET_XML = dict(xml_get_weapon('Weapon.xml', 'HORNET'))

2 - Parse the XML values into a python dict

  1. # Parse the values into dictionaries
  2. STINGER_FEATURES = xml_parsing(STINGER_XML)
  3. BUMBLEBEE_FEATURES = xml_parsing(BUMBLEBEE_XML)
  4. WASP_FEATURES = xml_parsing(WASP_XML)
  5. HORNET_FEATURES = xml_parsing(HORNET_XML)

Below an example with the class HomingMissile using the missile STINGER (STINGER_FEATURES)

Shoot_angle define the firing angle, 90 degrees here means that the missile will be
fire ahead of the aircraft, -90 will lauch the missile from behind the aircraft.

offset (int:x, int:y) define the missile offset position from the aircraft center.

At (0, 0) the missile is lauched from the centre of the aircraft while (-30, 0) means that the
missile will be fired from the left wing and (+30, 0) from the right wing.

ignition True delay the particle system

  1. if keys[pygame.K_SPACE]:
  2. if GL.PLAYER.rect.colliderect(SCREENRECT):
  3. player = GL.PLAYER
  4. if not player.is_missile_reloading(
  5. STINGER_FEATURES['reloading_time'] * 1000 / GL.TIME_PASSED_SECONDS):
  6. extra = ExtraAttributes(
  7. {'target': target,
  8. 'shoot_angle': 90,
  9. 'ignition': False,
  10. 'offset': (-30, 0)})
  11. s = HomingMissile(
  12. gl_=GL,
  13. group_=(GL.ALL, GL.PLAYER_PROJECTILE),
  14. weapon_features_=WASP_FEATURES,
  15. extra_attributes=extra,
  16. timing_=800,
  17. )

and below for the class InterceptMissile using the
missile BUMBLEBEE (BUMBLEBEE_FEATURES)

  1. extra = ExtraAttributes(
  2. {'target': target,
  3. 'shoot_angle': 90,
  4. 'ignition': False,
  5. 'offset': (30, 0)})
  6. s = InterceptMissile(
  7. gl_=GL,
  8. group_=(GL.ALL, GL.PLAYER_PROJECTILE),
  9. weapon_features_=BUMBLEBEE_FEATURES,
  10. extra_attributes=extra,
  11. timing_=800,
  12. )

Goodies

The file SpriteSheet.pyx contains two super fast algorithms for dealing with spritesheets

  1. - sprite_sheet_per_pixel
  2. (for 32-bit spritesheet with per-pixel transparency)
  3. - sprite_sheet_fs8
  4. (for 24-bit 32-bit without alpha layer)

Building Cython & C

When do you need to compile the cython code ?

The first time compilation and each time you are modifying any
of the pyx files such as Sprites.pyx, Sprites.pxd, .pyx and .pxd init.pxd
or any external C code if applicable

1) open a terminal window
2) Go in the main project directory HomingMissile where (setup.py & requirements.txt
are located)
3) run : C:\>python setup.py build_ext --inplace --force

If you have to compile the code with a specific python
version, make sure to reference the right python version
in (python38 setup.py build_ext --inplace)

If the compilation fail, refers to the requirement section and
make sure Cython and a C-compiler are correctly install on your
system.

  • A compiler such visual studio, MSVC, CGYWIN setup correctly on
    your system.
    • a C compiler for windows (Visual Studio, MinGW etc) install
      on your system and linked to your Windows environment.
      Note that some adjustment might be needed once a compiler is
      install on your system, refer to external documentation or
      tutorial in order to setup this process.e.g :

https://devblogs.microsoft.com/python/unable-to-find-vcvarsall-bat/

If the project build successfully, the compilation will end up with the following lines

  1. Generating code
  2. Finished generating code

If you have any compilation error(s) refer to the section Building cython code, make sure
your system has the following program & libraries installed. Check also that the code is not
running in a different thread.

  • Python version >3
  • pygame >=2.0.0
  • numpy >= 1.18
  • cython >=0.29.21 (C extension for python)
  • PygameShader >=1.0.8
  • A C compiler for windows (Visual Studio, MinGW etc)

OPENMP for Linux and Windows

If you need to build the package with multiprocessing, you can change the flag OPENMP
in the setup.py file such as :

To build the package with multiprocessing (OPENMP=True)

in the setup.py file

  1. # True enable the multiprocessing
  2. OPENMP = True
  3. OPENMP_PROC = "-fopenmp"
  4. LANGUAGE = "c"
  5. ext_link_args = ""

Then compile the code (e.g :)

  1. C:\...HomingMissile\python setup.py build_ext --inplace

Dependencies :

  1. numpy >= 1.18
  2. pygame >=2.0.0
  3. cython >=0.29.21
  4. PygameShader>=1.0.8

Your contribution

Please report any bugs to

https://github.com/yoyoberenguer/Homing-missile/issues


Credit

Yoann Berenguer

License :

GNU GENERAL PUBLIC LICENSE Version 3

Copyright (c) 2019 Yoann Berenguer

Copyright (C) 2007 Free Software Foundation, Inc. https://fsf.org
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.