Create a Flash game like Pixel Purge step 1 – player movement

Read all posts about "" game

Are you playing Pixel Purge?

It’s a good mix between a shooter and an avoider.

I am going to show you how to create a replica of this game, and I’m starting from player movement.

In Pixel Purge, you control a spaceship with arrows or WASD keys in an level that’s as big as twice the visible area of the game. When you move the spaceship, it remains in the center of the stage, with the rest of the game scrolling consequently, until it travels too close to level borders. When it happens, the game itself stops scrolling and the spaceship leaves the center of the stage. Play the game to see what I mean.

This is the feature I am showing in this prototype. It’s quite simple, being a simple concept.

Classes are made for the sake of tutorial understanding, so experienced users may find them a little weird, but they will be improved as soon as I’ll add more elements to the game.

Anyway, this is how things will work: besides the routine to detect pressed keys, we have a background that’s twice as width and height than the movie itself. In this case, the movie is 640×480 and the background is 1280×960 pixels. The player starts in the middle of the background, with x and y properties respectively at 640 (1280/2) and 480 (960/2). The background is also centered in the stage, at x = -320 and y = -240.

This way at the very beginning of the game, both the background and the player are perfectly centered on the stage. Then, when the ship moves, in a way very similar to
Flash game creation tutorial – part 1 (with AS3 classes), the background scrolls in the opposite way, keeping the ship centered in the screen, until it gets too close to background borders. Then, the background stops scrolling. There is really nothing new, so I would start with the code:

This is the main class:

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    public class main extends Sprite {
        private var game_container:game_container_mc;
        private var left,up,right,down:Boolean;
        public function main() {
            left=up=right=down=false;
            game_container=new game_container_mc();
            addChild(game_container);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
            stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
            addEventListener(Event.ENTER_FRAME,on_enter_frame);
        }
        private function on_enter_frame(e:Event):void {
            if (left) {
                game_container.player.engine(-1,0)
            }
            if (right) {
                game_container.player.engine(1,0)
            }
            if (up) {
                game_container.player.engine(0,-1)
            }
            if (down) {
                game_container.player.engine(0,1)
            }
            game_container.player.move_player();
            game_container.move_container();
        }
        private function on_key_down(e:KeyboardEvent):void {
            switch (e.keyCode) {
                case 37 ://left
                    left=true;
                    break;
                case 38 :// up
                    up=true;
                    break;
                case 39 ://right
                    right=true;
                    break;
                case 40 ://down
                    down=true;
                    break;
            }
        }
        private function on_key_up(e:KeyboardEvent):void {
            switch (e.keyCode) {
                case 37 ://left
                    left=false;
                    break;
                case 38 :// up
                    up=false;
                    break;
                case 39 ://right
                    right=false;
                    break;
                case 40 ://down
                    down=false;
                    break;
            }
        }
    }
}

This is game_container_mc class, managing the background itself:

package {
    import flash.display.Sprite;
    public class game_container_mc extends Sprite {
        public var player:player_mc;
        public function game_container_mc() {
            var level_bg:level_bg_mc =  new level_bg_mc();
            addChild(level_bg);
            player=new player_mc();
            addChild(player);
            x=-320;
            y=-240;
        }
        public function move_container():void {
            // scrolling keeping the player centered in game screen
            x=320-player.x;
            y=240-player.y;
            // stop scrolling if the player gets too close to background borders
            if (x>0) {
                x=0;
            }
            if (x<640*-1) {
                x=-640;
            }
            if (y>0) {
                y=0;
            }
            if (y<480*-1) {
                y=-480;
            }
        }
    }
}

And finally player_mc class:

package {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class player_mc extends Sprite {
        private var thrust:Number=1;
        private var xspeed,yspeed:Number;
        private var friction:Number=0.95;
        public function player_mc() {
            xspeed=yspeed=0;
            x=640;
            y=480;
        }
        public function move_player():void {
            xspeed*=friction;
            yspeed*=friction;
            x+=xspeed;
            y+=yspeed;
            // preventing spaceship to fly off the stage
            if (x>1270) {
                x=1270;
            }
            if (x<10) {
                x=10;
            }
            if (y>950) {
                y=950;
            }
            if (y<10) {
                y=10;
            }
        }
        public function engine(x_eng:int,y_eng:int):void {
            xspeed+=thrust*x_eng;
            yspeed+=thrust*y_eng;
        }
    }
}

This is the result:

move the "ship" with arrow keys, get close to the edges to see what happens.

Download the source code. If you like this series, next time I'll show you how to fire.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers