News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Per-pixel Tile Engine

Started by Scott_Bro, February 07, 2021, 02:52:16 PM

Previous topic - Next topic

Scott_Bro

Here it is so far.

It can now handle .csv comma spaced variables with just a tweak.

I got the x & y offsets for the movement.

The screen is 16 X 12 tiles but world is 48 X 36.

Need help moving there the world while keeping player on screen.

Please help!

Scott B.

p.s. This can be used for a great many games!

kevin


  why not just work in pixels and then translated those values back to tiles.

kevin


kevin


   Tweaked this up and added scrolling.. 

PlayBASIC Code: [Select]
; PROJECT : Per-Pixel Tile Engine - Kevs Edit
; AUTHOR : Scott_Bro_1@outlook.com
; CREATED : 5/02/2021
; EDITED : 13/02/2021
; ---------------------------------------------------------------------

X_Screen_Res = 1024
Y_Screen_Res = 768
Color_Mode = 32
Windowed_Mode = 1

OpenScreen X_Screen_Res,Y_Screen_Res,Color_Mode,Windowed_Mode

TitleScreen "Per-Pixel Tile Engine!"

// Tile size.

constant X_Cell_Size = 64
constant Y_Cell_Size = 64

// Tiles per screen.

X_Number_Of_Cells = X_Screen_Res / X_Cell_Size
Y_Number_Of_Cells = Y_Screen_Res / Y_Cell_Size

// Tiles per level.

X_Scalar = 3
Y_Scalar = 3

X_World_Size = X_Number_Of_Cells * X_Scalar
Y_World_Size = Y_Number_Of_Cells * Y_Scalar

// World map.

Dim World_Map(X_World_Size,Y_World_Size)

LoadWorld("Dungeon_1.txt",X_World_Size,Y_World_Size)



// Player's starting position.

X1_Player = X_Cell_Size * 4
Y1_Player = Y_Cell_Size

// Player's current cell location.

X1_Player_Cell = X1_Player / X_Cell_Size
Y1_Player_Cell = Y1_Player / Y_Cell_Size

// Player's speed.

Speed = 1


// -------------------------------------------------------
// Camera Scrolling Position
// -------------------------------------------------------

CameraX = 0
CameraY = 0

Do

Cls


// ----------------------------------------------
// Scroll the Camera
// ----------------------------------------------
CameraX = mod(CameraX-1,-1000)


// ----------------------------------------------
// Render tiles to screen.
// ----------------------------------------------

DrawWorld(CameraX,CameraY, X_World_Size,Y_World_Size)

// ----------------------------------------------
// Draw the Player
// ----------------------------------------------

// Player one.

Ink RGB(0,0,255)
ScreenX = CameraX +(X1_Player_Cell * X_Cell_Size + X_Offset)
ScreenY = Y1_Player_Cell * Y_Cell_Size + Y_Offset
Box ScreenX,ScreenY,ScreenX+ X_Cell_Size ,ScreenY+ Y_Cell_Size,1



// Display readout.

// Player's current tile postion.
Text 0,0,"X1_Player_Cell: " + Str$(X1_Player_Cell) + " Y1_Player_Cell: " + Str$(Y1_Player_Cell)

// Player's movement offsets.
Text 0,16,"X_Offset: " + Str$(X_Offset) + " Y_Offset: " + Str$(Y_Offset)


// Camera X/Y positions
Text 0,32,"Camera X: " + Str$(CameraX) + " CameraY:" + Str$(CameraY)


// Bring up the curtains.
Sync

// Saved player's old position.

X1_Player_Old = X1_Player
Y1_Player_Old = Y1_Player

// Player's controls.

If Leftkey() = 1 Then X1_Player = X1_Player - Speed
If Rightkey() = 1 Then X1_Player = X1_Player + Speed

If Upkey() = 1 Then Y1_Player = Y1_Player - Speed
If Downkey() = 1 Then Y1_Player = Y1_Player + Speed

// Player's cell location.
X1_Player_Cell = X1_Player / X_Cell_Size
Y1_Player_Cell = Y1_Player / Y_Cell_Size

// World collision detection.

If World_Map(X1_Player_Cell,Y1_Player_Cell) > 1

// Restore player's last position before collision occured.

X1_Player = X1_Player_Old
Y1_Player = Y1_Player_Old

// Place player into safe position.

X1_Player_Cell = X1_Player / X_Cell_Size
Y1_Player_Cell = Y1_Player / Y_Cell_Size

Else

// Per-pixel movement.

X_Offset = Mod(X1_Player,X_Cell_Size)
Y_Offset = Mod(Y1_Player,Y_Cell_Size)

EndIf

Login required to view complete source code