run pip install git+https://bitbucket.org/dwhite0/pycat.git -U
run pip install -e .
from the directory holding setup.py
from pycat.core import Window, Sprite, Label, Color, Point, KeyCode, Scheduler, RotationMode
from pycat.base import MouseEvent, MouseButton, NumpyImage
from pycat.experimental.ldtk import LdtkFile
from pycat.experimental.spritesheet import SpriteSheet, UniversalLPCSpritesheet
from pycat.experimental.movement import FourWayMovementController
from pycat.experimental.animation import Animator
window = Window()
# Place your code here
window.run()
window.run()
should be the final line in the file.
window.get_all_sprites()
window.get_sprites_with_tag('enemy')
window.delete_all_sprites()
window.delete_sprites_with_tag('enemy')
Name each property when creating the sprite as property_name=value
. This method is suitable when the sprite is simple and not repeated elsewhere in your program.
window.create_sprite(scale=100, x=200)
Create a class
to represent the sprite. Set properties inside the class using self.property_name=value
.
class Player(Sprite):
def on_create(self):
self.scale = 100
self.x = 200
window.create_sprite(Player)
Save the value from create_sprite
in a variable to modify its properties after creation.
player = window.create_sprite(scale=100)
player.x = 200
All three methods may be combined. However, note the ordering carefully:
- properties are set in
on_create
, sox=200
at the beginning create_sprite
may override properties, sox=200
is changed tox=300
- properties may be modified after creation, so finally
x=300
is changed tox=400
class Player(Sprite):
def on_create(self):
self.scale = 100
self.x = 200
player = window.create_sprite(Player, x=300)
player.x = 400
The section above described how to do things one time when a sprite is created. To modify a sprite lots of times during the game we use on_update()
. This method is called in every frame, which is about 60 times per second.
class Player(Sprite):
def on_update(self,dt):
self.x += 10
Normally a sprite will rotate based on its self.rotation
property. However, if you want to override that you can set self.rotation_mode
:
def on_create(self):
self.rotation_mode = RotationMode.ALL_AROUND
self.rotation_mode = RotationMode.RIGHT_LEFT
self.rotation_mode = RotationMode.MIRROR
self.rotation_mode = RotationMode.NO_ROTATION
Sprite collisions are usually checked in on_update
.
Check if sprites are touching. These methods return a bool
and are usually used in with if
:
def on_update(self,dt):
if self.is_touching_any_sprite():
self.delete()
if self.is_touching_any_sprite_with_tag('enemy'):
self.delete()
if self.is_touching_sprite(player):
self.delete()
Get the sprites that are touching. These methods return a list of Sprite
and are usually used in a for
loop:
def on_update(self,dt):
for sprite in self.get_touching_sprites():
sprite.delete()
for sprite in self.get_touching_sprites_with_tag('enemy'):
sprite.delete()
A label can be created with inital properties in the same way as a sprite.
window.create_label(text='Score: 10')
class ScoreLabel(Label):
def on_create(self):
self.text='Score: 10'
window.create_label(ScoreLabel)
score_label = window.create_label(text='Score: 10')
score_label.font_size = 30
def on_update(self,dt):
if window.is_key_pressed(KeyCode.UP):
self.y += 10
if window.is_key_down(KeyCode.SPACE):
self.shoot()
if window.is_key_up(KeyCode.UP):
pass
# We very rarely use this
window.mouse_position : Point
window.mouse_delta : Point
class Player(Sprite):
def on_left_click(self):
"""Called when left mouse button is clicked on this sprite."""
pass
def on_left_click_anywhere(self):
"""Called when left mouse button is clicked anywhere in the window."""
pass
class Player(Sprite):
def on_click(self, mouse_event: MouseEvent):
"""Called when ANY mouse button is clicked on this sprite."""
if mouse_event.button == MouseButton.RIGHT:
pass
def on_click_anywhere(self, mouse_event: MouseEvent):
"""Called when ANY mouse button is clicked anywhere in the window."""
pass
To schedule an action in 1 second (note that the method self.delete
is not called like )self.delete()
Scheduler.wait(1, self.delete)
To schedule an action to repeat every 2 seconds:
def create_enemy():
window.create_sprite(Enemy)
Scheduler.update(create_enemy, 2)
To cancel a scheduled repeating action:
Scheduler.cancel_update(create_enemy)
from pycat.experimental.ldtk import LdtkFile
ldtk_file = LdtkFile('level_file_name.ldtk')
window = Window()
ldtk_file.render_level(window,'Level_0',debug_tags=True)
window.run()
The following snippet cuts a segment out of an image and then uses it as a texture for a sprite:
image = NumpyImage.get_array_from_file('file_name.png')
segment = image[row_start: row_end, col_start: col_end, :]
sprite.texture = NumpyImage.get_texture_from_array(segment)