Pythonista for Ipad day 1: how to make games on the ipad with Python

Let’s go exploring pythonista 3 for the ipad to see how to make games with python on the ipad. Here is how to display sprites on the screen.

How a videogame works in pythonista

Before the screen becomes visible on the screen the setup() method is called

the setup makes stuff change so that their are visible on the screen right after they changed

Change the background, before the screen is visible

from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = 'green'

run(MyScene())

This makes the background green

To add something on the screen, create a Node object.

There are different subclasses of Node for different stuffs:

SpriteNode renders an image

LabelNode text

etc.

Scene is a subclass of Node.

Nodes can contain other nodes (useful to move objects as a single entity).

  • All nodes have 
  • position
  • rotation
  • scale,
  • and alpha (opacity)

attributes that determine how the node is drawn.

The default position is (0, 0): lower-left corner of the screen.

Place a starship on the screen

from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.ship = SpriteNode('spc:PlayerShip1Orange')
        self.ship.position = self.size / 2
        self.add_child(self.ship)

run(MyScene())

Touch the screen and make things happen

the touch_began() method is what we use to move sprites.

from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.ship = SpriteNode('spc:PlayerShip1Orange')
        self.ship.position = self.size / 2
        self.add_child(self.ship)

    def touch_began(self, touch):
        x, y = touch.location
        move_action = Action.move_to(x, y, 0.7, TIMING_SINODIAL)
        self.ship.run_action(move_action)

run(MyScene())

This code will work only on pythonista on the ipad, not on the pc

from scene import *

# Making a game with ipad and pythonista 3; part 1
# We will set the background, the earth and the player on screen


class Game(Scene):
    def setup(self):
        self.add_ground()
        self.add_background()
        self.add_earth()
        self.add_player()

    def add_ground(self):
        ''' the root node. the window object, the main surface '''
        self.ground=Node(parent=self)

    def add_background(self):
        ''' the color of the background '''
        self.background_color = '#20106e'

    def add_earth(self):
        ''' draw the tiles for the earth under the player '''
        x = 0
        while x < self.size.w + 64: # draw tiles untile the end of the screen
            tile = SpriteNode('plf:Tile_BrickBrown', position=(x, 0))
            self.ground.add_child(tile)
            x += 64

    def add_player(self):
        ''' adds the player to the scene '''
        self.player = SpriteNode('plf:AlienBeige_front')
        self.player.anchor_point=(0.5, 0)
        self.player.position = (self.size.w//2, 32)
        self.add_child(self.player)

run(Game(), LANDSCAPE, show_fps=True)


Subscribe to the newsletter for updates
Tkinter templates

Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Claude's Games

Arkanoid
Platform 2d

1. Memory game

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Advertisement