In this tutorial, we will explore the art of writing wargames in Python using the Kivy framework. Kivy is an open-source Python library for developing multitouch applications. It is especially well-suited for developing games due to its support for both desktop and mobile platforms.
We will use Dorian Pula’s book "Creating Games with Python and Pygame" as a base for this tutorial. The book provides a solid foundation in game development principles and techniques, which we will adapt to the Kivy framework.
To start, we need to set up our development environment. First, make sure you have Python installed on your system. You can download the latest version from the official Python website. Next, install Kivy by running the following command:
pip install kivy
You may also need to install additional dependencies depending on your operating system. Refer to the official Kivy documentation for more information.
Once Kivy is installed, we can start developing our wargame. We will begin by creating a simple game loop that updates the game state and renders it on the screen. Here is a basic example:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
class WarGame(Widget):
def __init__(self, **kwargs):
super(WarGame, self).__init__(**kwargs)
self.player = Rectangle(pos=(100, 100), size=(50, 50))
def update(self):
pass
def on_touch_down(self, touch):
self.player.pos = touch.pos
class WarGameApp(App):
def build(self):
game = WarGame()
return game
def on_pause(self):
return True
def on_resume(self):
pass
if __name__ == '__main__':
WarGameApp().run()
In this example, we define a WarGame
class that inherits from Widget
. We initialize the game state in the __init__
method by creating a player object represented by a Rectangle
. The update
method will be called once per frame to update the game state, but for now, it is empty. The on_touch_down
method handles touch events by moving the player object to the position of the touch.
The WarGameApp
class extends App
and serves as the entry point for our application. In the build
method, we create an instance of WarGame
and return it as the root widget. The on_pause
and on_resume
methods are placeholders for handling pause and resume events, respectively.
To run the game, save the code to a file (e.g., wargame.py
) and execute it from the command line:
python wargame.py
You should see a window with a player object that moves when you touch the screen. This is just a basic starting point for our wargame, but we can expand upon it by adding more game elements, such as enemies, obstacles, and weapons.
In the following sections, we will cover various aspects of game development, including graphics, sound, physics, and user input. We will also discuss strategies for optimizing performance and improving the user experience.
Overall, writing wargames in Python with Kivy can be a rewarding experience. With the right approach and attention to detail, you can create immersive and engaging games that entertain players of all ages. Good luck on your game development journey!
Good
If they'd just come out with a new dark basic or some other BASIC programme, math would be done for you. Yeah, might not be the fastest, but neither is python. You want fast, use c or assembly. You want to concentrate on making a game, use a basic language. I could create a solar system in 20 lines of code vs 500+ in python or c++. It shouldn't have to be this way. My biggest issues were 3d character creation (in blender) and heightmap and follow terrain for a sphere (not allowed – terrain only plane, but can be height mapped [ie no Spore])
For a brilliant breakdown of how to do all the maths (and some code too) for hexagonal grids (with some amazing interactive demos) check out https://www.redblobgames.com/grids/hexagons/ . Having read that my advice would be don't start with hex-grids as its not just drawing them, all the algorithms relating to distance have to change (which also depends on your coordinate systems) etc
i have got a doubt. if i use kv language, where should i write the kv language's code? in notepad? or in a new file using python? where?
and if i use notepad,new file or anything else to write the kv code, then that kv code should be in a another place(file). then, if i send that kivy android app(main file) to my android phone, then how that kivy app will use that kv language code(as kv code is in another file or in notepad)?
I heard that Battlefield 2 was written in Python…
Does anybody know anything about this?
Kivy is awesome. Not only is it awesome for games, but I've even built my full smart home system around it and it's been great so far.
26:21 "Oh quick demo, oh boy… [breaths heavily] …. [breathy] alright, this is the part where it's a little embarrassing… [breathing] …Lemme…Lemme get it up…" THIS is that part that's embarrassing? XD ….I love this video for it's technical aspects, but some of the phrasing and presentation was comical in my opinion. 😀
Just for reference PySide has Python based documentation http://pyside.github.io/docs/pyside/
Kivy is good, especially for interactive apps. but I dropped the idea of developing games on it when I came across Godot.
Hey, nice presentation, just want to precise a couple technical points:
– parent/children event propagation is a bit specific to the touch events, which are dispatched first on the Window (parent of all widgets), and any widget, by default, dispatch the touch to its children (unless it decided it was for itself), that's why you usually call return super().on_touch_down(touch) in any override of on_touch_down (same for on_touch_move and on_touch_up), other events, like on_size, on_pos, or anything you define yourself, is dispatched on anything that binds to that property on the widget.
– Yes, kivy's graphic module is basically a wrapper around OpenGL, and you can totally do anything opengl allows, being careful about letting the context in a usable state for kivy :). Be aware, though, that kivy mostly cares about 2d opengl, so the api doesn't provide a lot of support for 3d, but you can do it if you are ready to get a bit dirty (see https://www.youtube.com/watch?v=jW_gqPwu0x0, or https://www.youtube.com/watch?v=halh-UMpYDU and a few others)
Thanks again for the nice shoutout 🙂
The ultimate hex-grid cheatsheet: http://www.redblobgames.com/grids/hexagons/
Here is a working (alpha) 3D engine for Kivy: https://github.com/expertmm/KivyGlops