Post by johnno56 on Sept 28, 2019 3:58:51 GMT -6
Here is a simple tutorial - using some eyfenna's game layout principles - of basic player movement.
I have heavily commented sections of the program, primarily to explain 'how' things work, to any "newcommers" to RCBasic.
The methods I have used are probably NOT the only way to do things.
I have used 'primitives' instead of sprites. As usual, if anyone can improve on this work, please do. After all, "I" am still learning, as well... lol
I hope that this is useful....
I have heavily commented sections of the program, primarily to explain 'how' things work, to any "newcommers" to RCBasic.
The methods I have used are probably NOT the only way to do things.
I have used 'primitives' instead of sprites. As usual, if anyone can improve on this work, please do. After all, "I" am still learning, as well... lol
xmax = 640
ymax = 480
title$ = "Player Movement - Restricted to the screen."
' These variables are optional but are only used
' for convenience. The values can be added directly
' to the WindowOpen() and CanvasOpen() statements.
' eg: WindowOpen(1,"My Game",0,0,640,480)
' eg: CanvasOpen(1,640,480,0,0,640,480,0)
' xmax - screenwidth in pixels
' ymax - screenheight in pixels
' title$ - name of the program will be displayed
' - in the center of the title bar.
'===========================================================
' Open a graphics window. Give it a number,top left (0,0) coordinates,
' bottom right coordinates (xmax,ymax) and make it windowed or
' full screen. (1 = full, 0 = windowed)
WindowOpen(1,title$,0,0,xmax,ymax,0)
' Open a canvas on which to draw.
' Canvas number (1), width (xmax) and height (ymax) of the canvas
' The viewport size (xmax and ymax). Will the viewport be
' transparent (1) or solid (0)?
CanvasOpen(1,xmax,ymax,0,0,xmax,ymax,0)
' Clear or erase the current canvas
ClearCanvas
' For THIS example, the mouse will not be required.
HideMouse()
dim playerWidth
dim playerHeight
dim playerX
dim playerY
dim playerSpeed
sub initialise()
'======================================
' Initialize the player attributes
'======================================
' Player dimentions x and y
playerWidth = 32 ' pixels wide
playerHeight = 32 ' pixels high
' Center the player on the screen
playerX = (xmax - playerWidth) / 2
' playerX = (640 - 32) / 2 which is: 304
playerY = (ymax - playerHeight) / 2
' playerY = 480 - (32 * 2) which is: 416
playerSpeed = 4
' move 4 pixels while the left or right arrow key is pressed
playerColor = rgb(0, 128, 0) ' Dark Green
' RGB() will be a lesson in itself....
setColor(playerColor) ' Let's make the player dark green
end sub
sub UpdatePlayer()
'==================================
' update the player - controls
'==================================
' Do nothing until the left arrow key is pressed
if key(k_left) then
' Decrease the value of playerX by playerSpeed
playerX = playerX - playerSpeed
' Move left at the value of playerSpeed
end if
' Do nothing until the right arrow key is pressed
if key(k_right) then
' Increase the value of playerX by playerSpeed
playerX = playerX + playerSpeed
' Move right at the value of playerSpeed
end if
' Do nothing until the up arrow key is pressed
if key(k_up) then
' Decrease the value of playerY by playerSpeed
playerY = playerY - playerSpeed
' Move up at the value of playerSpeed
end if
' Do nothing until the down arrow key is pressed
if key(k_down) then
' Increase the value of playerY by playerSpeed
playerY = playerY + playerSpeed
' Move down at the value of playerSpeed
end if
'======================================================
' Let's restrict the player movement so as to keep
' the player "on the screen".
'======================================================
' Horizontal
if playerX <= 0 then
' Has the lefthand edge of the player reached the lefthand
' edge of the screen (x coordinate: 0)
playerX = 0
' if so, make playerX equal to 0: Player stops moving.
end if
if playerX >= xmax - playerWidth then
' Has the righthand edge of the player reached the righthand
' edge of the screen (x coordinate: xmax - playerWidth)
playerX = xmax - playerWidth
' if so, make sure that the player is placed the width of the
' player from the righthand edge of the screen. (640 - 32)
' player stops moving.
end if
' Vertical
if playerY <= 0 then
' Has the top edge of the player reached the top
' edge of the screen (y coordinate: 0)
playerY = 0
' if so, make playerY equal to 0: Player stops moving.
end if
if playerY >= ymax - playerHeight then
' Has the bottom edge of the player reached the bottom
' edge of the screen (y coordinate: ymax - playerHeight)
playerY = ymax - playerWidth
' if so, make sure that the player is placed the heioght of
' the player from the bottom edge of the screen. (480 - 32)
' player stops moving.
end if
end sub
sub DrawPlayer()
'=============================================================
' draw the player - no sprites just primative graphics...
'=============================================================
RectFill(playerX, playerY, playerWidth, playerHeight)
' The player will be a filled rectangle, with the dimensions
' playerWidth and playerHeight and will be displayed at
' playerX and PlayerY
' Alternatively: Replacing "RectFill" with "Rect" will draw a
' "wireframe" or unFulled rectangle... Try it...
end sub
'==========================
' Now the FUN part....
'==========================
' Call the routine to initialise variables and constants.
initialise()
' Let's perform a continuous loop until the Escape key is pressed.
' Key values (in this case) are: 0 = not pressed; 1 = pressed.
do
ClearCanvas
' Call the routine to control and update the player's position
UpdatePlayer()
' Call the routine to actually display the player.
DrawPlayer()
Update()
' Update all graphics and events since the last Update was called.
loop until key(k_escape)
' If the escape key is pressed (1) then permission is given to EXIT
' the do...loop. In this case the program just ends.
I hope that this is useful....