script.lua:
Code: Select all
-- The require function looks for the "colors" and "player" lua files inside the game's folder, sees if they've been run before, and loads the code inside said files.
require("colors")
require("player")
-- The "while" loop will check the condition, and if true (it is, in this case), will run the code in an infinite loop.
while true do
-- The "drawPlayer" function draws the player on the screen. More about it can be found inside player.lua.
drawPlayer()
-- The "movePlayer" function moves the player across the screen. More about it can be found inside player.lua.
movePlayer()
-- The "screen.print" function draws text on the screen. Please note that the PSP's resolution is 480 x 272, 480 pixels horizontally (X) and 272 pixels vertically (Y). In the code below, 5 is the horizontal (X) value for where the text will be printed on the screen, and 10 is the vertical (Y) value for such. The text inside the quotation marks, "Walking Simulation [Side-Scroller]", is what will be displayed on the screen. 0.6 is the font size for the text, and White is the font color, which was grabbed from colors.lua.
screen.print(5, 10, "Walking Simulation [Side-Scroller]", 0.6, white)
-- The "screen.flip" function is responsible for making things being drawn on the screen visible.
screen.flip()
-- Here the code ends.
end
Code: Select all
-- This is a table that holds the player's values. The values being: where the player appears on the screen (the X and Y value of the player), the speed in which the player walks (this is a horizontal movement, so this affects the X value), the speed in which the player raises while walking (this is a vertical movement, so this affects the Y value), and the status of the player's movements (walking or still).
player = {}
player.x = 40
player.y = 180
walkSpeed = 1
raiseSpeed = 2
playerStatus = "place holder text (this will change when the status is updated in-game with a button press"
-- The function "draw.fillrect" draws a filled rectangle, using the X and Y player values from the table above. The width of the filled rectangle is 80 and the height of the rectangle is 100. White is the color of the font, which was grabbed from colors.lua. All of this gets put under the variable playerImage.
function drawPlayer()
playerImage = draw.fillrect (player.x, player.y, 80, 100, white)
end
-- The function that moves the player.
function movePlayer()
-- "buttons.read" reads the button inputs.
buttons.read()
-- This prohibits the player from walking past the sides of the screen. This needs to be inside this function specifically. Otherwise it won't work.
if player.x <= 0 then player.x = 1 end
if player.x >= 398 then player.x = 397 end
-- If the player holds right or left on the dpad, then the player moves right or left, at a speed of 2.5. The player's status is also updated to "moving".
if buttons.held.left then player.x = player.x - 2.5
playerStatus = "moving left" end
if buttons.held.right then player.x = player.x + 2.5
playerStatus = "moving right" end
-- If the player stops holding right or left, then the player's status is updated to "still", as in "standing still", since the player is not "moving".
if buttons.released.left then playerStatus = "still" end
if buttons.released.right then playerStatus = "still" end
-- This raises the player up and down while he's moving horizontally, to simulate walking. The value in the less than (<=) comparison needs to match the player.y value at the beginning of this page, in the table.
if buttons.held.right or buttons.held.left then
player.y = player.y + walkSpeed
if player.y >= 195 then walkSpeed = walkSpeed * - 1 end
if player.y <= 180 then walkSpeed = walkSpeed * - 1 end
end
-- This raises back the player into place, when he's standing still. That way the player is not hanging under the screen if the player decides to stop walking.
if playerStatus == "still" then player.y = player.y - raiseSpeed end
if player.y <= 180 then player.y = 180 end
end
Code: Select all
-- Only the color white is being used for this example, as no other color is deemed necessary. The values (255,255,255) follow the RGB color model (Red, Green, Blue). 255 is the maximum value for each color, and as such, when all three colors are mixed together, White is the result.
white = color.new(255,255,255)
Advertising