Create a game in Pythonista on the Ipad in 40 minutes

In this video you wll see the potentials of Pythonista, a great app for the Ipad to code just with the tablet in your favourite language and with all the greatness of IOS system. There is a module made just to take advantage of all the great things that the Ipad brings with it. This video is based on the tutorial that is present on the app itself. I tried to explain it in the fastest and easiest way. The last videos are at the bottom of the page, with the code to destroy the meteors.

The process step by step

In video above is divided in the following steps below.

1. Create the background

2. Create the ground

3. The player arrives

4. Player is moving tilting the Ipad

5. Limit movement to screen size

6. Fire!

7. Animate the walk

8. Look towards the direction

9. Steps sound

10. Coins from the sky (they just appear)

11. Rain of coins (now they fall)

12. Collection coins (collision detection)

13. Put some score on the screen, please!

14. Here the enemy comes! Meteors falling from the sky!

15. Meteors Hit!

16. The end of the game: survive shooting to meteors

The code

# coding: utf-8

from scene import *
import sound
import random


# standing and walking texture
standing = Texture('plf:AlienGreen_front')
walking = [Texture('plf:AlienGreen_walk1'), Texture('plf:AlienGreen_walk2')]
hit_texture = Texture('plf:AlienGreen_duck')

class Coin(SpriteNode):
	def __init__(self, **kwargs):
		SpriteNode.__init__(self, 'plf:Item_CoinGold', **kwargs)

class Meteor(SpriteNode):
	def __init__(self, **kwargs):
		img = random.choice(['spc:MeteorBrownBig1','spc:MeteorBrownBig2'])
		SpriteNode.__init__(self, img, **kwargs)
		self.destroyed = False


class Game(Scene):
	
	def setup(self):
		self.background_color = "#3b21b5"
		ground = Node(parent=self)
		x = 0
		while x <= self.size.w +64:
			tile = SpriteNode('plf:Ground_Dirt', position=(x,10))
			ground.add_child(tile)
			x += 64
		# create player sprite
		self.player = SpriteNode('plf:AlienGreen_front')
		# position
		self.player.position = (self.size.w/2, 41)
		# anchor
		self.player.anchor_point = (0.5, 0)
		# attach the player the ground (make it visible)
		ground.add_child(self.player)
		# Add score
		self.label_score = LabelNode('0', ('futura',40), parent=self)
		self.label_score.position = (self.size.w / 2, self.size.h -40)
		self.game_over = False
		self.list_of_items = []
		self.new_game()
		
	def new_game(self):
		for item in self.list_of_items:
			item.remove_from_parent()
		self.score = 0
		self.walk_state = -1
		self.list_of_items = []
		self.list_of_lasers = []
	
		self.game_over = False
		self.label_score.text = '0'
		self.player.position = self.size.w / 2, 41
		self.speed = 1
		self.player.texture = standing
		
	def update(self):
		if self.game_over:
			return
		if random.random() < .05:
			self.spawn_items()
		self.update_player()
		 #look for collisions
		self.collisions_with_items()
		self.collisions_with_lasers()
		
	def update_player(self):
		g = gravity()
		
		self.player.x_scale = ((g.x > 0) - (g.x < 0))
		
		if abs(g.x) > 0.05:
			speed = g.x * 50
			x = self.player.position.x
			x = max(0, min(self.size.w, x + speed))
			self.player.position = x, 41
			step = int(self.player.position.x / 40) % 2
			if step != self.walk_state:
				self.player.texture = walking[step]
				sound.play_effect('rpg:Footstep00', 0.05, 1.0 + .5 * g.x)
				self.walk_state = step
				
		else:
			self.player.texture = standing
			self.walk_state = -1
		
	def spawn_items(self):
		if random.random() < 0.3:
			meteor = Meteor(parent=self)
			meteor.position = random.uniform(20, self.size.w), self.size.h
			duration = random.uniform(2,4)
			meteor.run_action(
				Action.sequence(
					Action.move_to(0,-1000, duration),
					Action.remove()
					)
				)
			# list of coins falling
			self.list_of_items.append(meteor)
		
		else:
			coin = Coin(parent=self)
			coin.position = random.uniform(20, self.size.w), self.size.h
			duration = random.uniform(2,4)
			coin.run_action(
				Action.sequence(
					Action.move_by(0,-1000, duration),
					Action.remove()
					)
				)
			# list of coins falling
			self.list_of_items.append(coin)
			
		
	# Coins collision intersection
	def collisions_with_items(self):
		p_box = Rect(self.player.position.x-20, 32, 40, 65)
		for item in self.list_of_items:
			if item.frame.intersects(p_box):
				if isinstance(item, Coin):
					sound.play_effect('arcade:Coin_2')
					item.remove_from_parent()
					self.list_of_items.remove(item)
					self.score += 10
					self.label_score.text = str(self.score)
				else:
					self.player_hit()

	def collisions_with_lasers(self):
		for laser in self.list_of_lasers:
			if not laser.parent:
				self.list_of_lasers.remove(laser)
				continue
			for item in self.list_of_items:
				if not isinstance(item, Meteor):
					continue
				if item.destroyed:
					continue
				if laser.position in item.frame:
					self.list_of_lasers.remove(laser)
					laser.remove_from_parent()
					self.destroy_meteor(item)
					break
					
	def destroy_meteor(self, item):
		sound.play_effect('arcade:Explosion_2', 0.20)
		item.destroy = False
		item.remove_from_parent()
		self.list_of_items.remove(item)
		self.score += 50
		self.label_score.text = str(self.score)
	
	def player_hit(self):
		self.game_over = True
		sound.play_effect('arcade:Explosion_6')
		self.player.texture = hit_texture
		self.player.run_action(Action.move_by(0,-150))
		self.run_action(Action.sequence(Action.wait(2*self.speed), Action.call(self.new_game)))
		
		
	def touch_began(self, touch):
		# load the sprite
		laser = SpriteNode('plf:LaserPurpleDot',
			position=self.player.position,
			z_position= -1,
			parent=self)
			
		 # moving the laser
		laser.run_action(Action.sequence(
			Action.move_by(0,1000),
			Action.remove()))
		
		# sound to the laser
		
		sound.play_effect('arcade:Laser_1')
		self.list_of_lasers.append(laser)
		
		

		
						
run(Game(), PORTRAIT)

 

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.