Day 2
Day 3
In this episode, we will see how to stop the player movement when the user stops touching the screen.
def touch_ended(self, touch):
self.player.remove_action("move_action_key")
We use this touch_ended method to stop the action that we called “move_action_key” here:
def touch_began(self, touch): x, y = touch.location px, py = self.player.position self.move_action = Action.move_to(x, 32, 0.7, TIMING_SINODIAL) self.player.run_action(self.move_action, 'move_action_key')
So, this will do the job. The player character moves until we do not touch the screen anymore. Simple like that.
Here is the whole code:
from scene import *
import numpy
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):
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)
def touch_began(self, touch):
x, y = touch.location
px, py = self.player.position
self.move_action = Action.move_to(x, 32, 0.7, TIMING_SINODIAL)
self.player.run_action(self.move_action, 'move_action_key')
def touch_ended(self, touch):
self.player.remove_action("move_action_key")
run(Game(), LANDSCAPE, show_fps=True)
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game