Post by johnno56 on Sept 28, 2019 8:52:04 GMT -6
This instalment is a slight variation on "101"
In this example, two extra variables 'moveX' and 'moveY', have been added.
The UpdatePlayer() routine has been greatly modified. The actual 'movement' style is somewhat condensed
and the player can now "wrap around" the screen. The rest of the program is unchanged.
I hope this can be of some use...
In this example, two extra variables 'moveX' and 'moveY', have been added.
The UpdatePlayer() routine has been greatly modified. The actual 'movement' style is somewhat condensed
and the player can now "wrap around" the screen. The rest of the program is unchanged.
xmax = 640
ymax = 480
title$ = "Player Movement - Screen Wrap-a-round."
' 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
dim moveX
dim moveY
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
'==================================
moveX = (0 - key(k_left)) + key(k_right)
moveY = (0 - key(k_up)) + key(k_down)
' Remember: NOT pressed = 0 and pressed = 1
playerX = playerX + moveX * playerSpeed
playerY = playerY + moveY * playerSpeed
' CONCEPT:
' When BOTH left AND right keys are NOT pressed then
' key left = 0 AND key right = 0
' 0 + 0 NO movement
' When BOTH left AND right keys are pressed then
' key left = -1 and key right = 1
' -1 + 1 = 0 NO movement
' When ONLY the left key is pressed then
' key left = -1 and key right = 0
' -1 + 0 = -1 MOVE left ( * playerSpeed)
' When ONLY the right key is pressed then
' key left = 0 and key right = 1
' 0 + 1 = 1 MOVE right ( * playerSpeed)
' The same applies for the Up and Down keys.
if playerX < -playerWidth then
' if the player disappears off the left side of the screen...
playerX = xmax
' Place the left edge of the player to the right edge of the screen.
elseif playerX > xmax then
' If the player disappears off the right side of the screen...
playerX = -playerWidth
' Place the player, one width of the player, off the left side
' of the screen
end if
' In other words. If the player leaves the left side of the screen
' then "wrap around" to the right side.
' If the player leaves the right side of the screen then
' "wrap around" to the left side
' The same applies for Up and Down
if playerY < -playerHeight then
playerY = ymax
elseif playerY > ymax then
playerY = -playerHeight
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 this can be of some use...