Pythonista and Ipad: make a game 2

Add sprites to the backgroud

Your dreams can come alive with pythonista: create a game with ipad and in python. What is better to use the best language of the world on the best tablet? You can take advantage of the ipad input and all the feature that it has with python. So, let’s see how easy it is.

In the previous post we’ve seen how to start a game with pythonista on IOS, importing the scene module and starting the Game class, that inherit from the Scene class. With the function setup we defined the color and with the run function we started the Game class.
Now ew create the ground istance of Node, then with create a SpriteNode istance called tile that we attach it to the ground for all the width of the screen, incrementing the x position of the tile of 64 that is the width of each sprite.

# coding: utf-8

from scene import *

class Game(Scene):
	def setup(self):
		self.background_color = "#fc9525"

		ground = Node(parent=self)
		x = 0
		while x <= self.size.x +64:
			tile = SpriteNode('plf:Tile_Bush', position=(x,30))
			ground.add_child(tile)
			x += 64
        self.player = SpriteNode('plf:AlienBeige_front')
        self.player.anchor_point = (0.5,0)
        self.add_child(self.player)
		
run(Game())

At the end of the while cycle, we create another SpriteNode that is the player istance of the class containing the image of the sprite that you control. The we place it on the screen at the middle, with the anchor point to the feet of the sprite and we add it to the screen to be visible with add_child.

The anchor is by default to the center of the sprite. If (0,0) is the bottom-left, (1,1) is the top-right, so (0.5, 0) is bottom, middle.

The result is better explained by the following images:

pythonista games screen
pythonista games screen
(0, 0) (1,1) (0.5, 0)

The result being the following:

Video in italian (video in italiano)

External links

Video about pythonista in classroom

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.