hello guys, i'm trying to create a 2d game, but i have a problem.
I create all the objects on the map, but only the objects that are 500px away from my main character are displayed, but for my character to move I need to take the current position of the character and add more speed in pixels, right?
as it is a multiplayer game, I need to go to the X and Y positions being negative or positive .
there is some way to make the "camera" move with the player, as it is impossible for me to "bring" the objects into the still view camera so that the player is everything ...
I don't know if I transmitted what I want to say clearly, but I need the camera on the screen to move with the player, not to bring the objects into the player's field of view within his resolution ...
Can someone help me ?
2D Games camera X and Y
Re: 2D Games camera X and Y
When displaying all your objects, just add a camera offset to their coordinates.
You can then modify your camera position to set the view, for example setting the camera position to your players position when the player has moved.
Code: Select all
display(player, playerX - cameraX, playerY - cameraY)
display(enemy, enemyX - cameraX, enemyY - cameraY)
- skinkairewalker
- Enthusiast
- Posts: 637
- Joined: Fri Dec 04, 2015 9:26 pm
Re: 2D Games camera X and Y
thanks by tou answer #Null#NULL wrote:When displaying all your objects, just add a camera offset to their coordinates.You can then modify your camera position to set the view, for example setting the camera position to your players position when the player has moved.Code: Select all
display(player, playerX - cameraX, playerY - cameraY) display(enemy, enemyX - cameraX, enemyY - cameraY)
Where can i found this function : " Display " ?
Re: 2D Games camera X and Y
As I get, your question is about game architecture.
Unfortunately I can't post some of my real game code for now (I have few 2D), and writing a more complex example would be too long and take too much efforts.
So nothing to to.
Here is some quick example, where everything is extremely simplified (but generally, the same logic works if replacing here pixels with game objects, and rewriting all for that). Hope it is +- close to what you want and will help at least a bit
// PS. I would suggest you anyway first to learn about game engines. making a games is really not a simplest of all things, especially from scratch. for me it took a few hardcore months at first attempt.
Unfortunately I can't post some of my real game code for now (I have few 2D), and writing a more complex example would be too long and take too much efforts.
So nothing to to.
Here is some quick example, where everything is extremely simplified (but generally, the same logic works if replacing here pixels with game objects, and rewriting all for that). Hope it is +- close to what you want and will help at least a bit
![Cool 8)](./images/smilies/icon_cool.gif)
// PS. I would suggest you anyway first to learn about game engines. making a games is really not a simplest of all things, especially from scratch. for me it took a few hardcore months at first attempt.
Code: Select all
EnableExplicit
DisableDebugger
; SuperMegaExplosiveGame 4000k example
; v 1.0.0.0
; 2021 (c) Luna Sole
; this is like a map/game level/world (where each pixel represents some object)
Define WorldW = 6000, WorldH = 3000
Dim GameWorldMapWhatever.l (WorldW, WorldH)
Define Tx, Ty
; fill it with random
For Tx = 1 To WorldW
For Ty = 1 To WorldH
If Random(100, 1) <= 90
; leave it black
Else
GameWorldMapWhatever(tX, tY) = RGB(Random(255,128), 0, 0)
EndIf
Next Ty
Next Tx
; init
InitSprite()
InitKeyboard()
Define ScreenW = 800, ScreenH = 600
Define tWnd = OpenWindow(#PB_Any, 0,0, ScreenW, ScreenH, "SuperMegaExplosiveGame 4000k Overworld Earth by Lunasole INC, extremely dirty version only for 99.99$ buy right now and save -100$!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(tWnd), 0, 0, ScreenW, ScreenH, #False, 0, 0, #PB_Screen_SmartSynchronization)
; those 2 variables simulating a very primitive player ^^
; set initial player coordinates (center of a map)
Define PlayerX = WorldW * 0.5, PlayerY = WorldH * 0.5
Repeat
If WindowEvent() = #PB_Event_CloseWindow
Break
EndIf
; process keyboard controls to "move a player"
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
PlayerX - 1
EndIf
If KeyboardPushed(#PB_Key_Right)
PlayerX + 1
EndIf
If KeyboardPushed(#PB_Key_Up)
PlayerY - 1
EndIf
If KeyboardPushed(#PB_Key_Down)
PlayerY + 1
EndIf
; draw "objects"
; first determine which map area must be displayed
; only start of it is determined here for simplicity, EndX, EndY not used
Define StartX = PlayerX - ScreenW*0.5
Define StartY = PlayerY - ScreenH*0.5
; now draw
If StartDrawing(ScreenOutput())
;
For tX = 0 To ScreenW-1
For tY = 0 To ScreenH-1
; pixel color is taken from game level at given X/Y
Plot(tX, tY, GameWorldMapWhatever(tX+StartX+1, tY+StartY+1))
Next Ty
Next tX
; this circle represents a "primitive player" model, lol
; again, for simplicity it's always at screen center
; also there are more nice variants to move camera, applying for example simple linear interpolation to X and Y
Circle(ScreenW * 0.5, ScreenH * 0.5, 5, #Red)
StopDrawing()
EndIf
; change on-screen frame
FlipBuffers()
ForEver
End
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Re: 2D Games camera X and Y
That was just a generic example. The actual display function would be DisplaySprite/DisplayTransparentSprite/DrawImage etc. or whatever you are using.skinkairewalker wrote:Where can i found this function : " Display " ?
- skinkairewalker
- Enthusiast
- Posts: 637
- Joined: Fri Dec 04, 2015 9:26 pm
Re: 2D Games camera X and Y
Amazing !Lunasole wrote:As I get, your question is about game architecture.
Unfortunately I can't post some of my real game code for now (I have few 2D), and writing a more complex example would be too long and take too much efforts.
So nothing to to.
Here is some quick example, where everything is extremely simplified (but generally, the same logic works if replacing here pixels with game objects, and rewriting all for that). Hope it is +- close to what you want and will help at least a bit![]()
// PS. I would suggest you anyway first to learn about game engines. making a games is really not a simplest of all things, especially from scratch. for me it took a few hardcore months at first attempt.
I tried to study and replicate the algorithm, but I still have doubts ...
i am with this example i am doing, how would i make this algorithm above work in my example effectively?
My example :
Code: Select all
EnableExplicit
DisableDebugger
Structure mapStarsStruct
x.d
y.d
size.f
cor.i
EndStructure
Global NewList mapStars.mapStarsStruct()
#RandMax = 2147483647
; SuperMegaExplosiveGame 4000k example
; v 1.0.0.0
; 2021 (c) Luna Sole
; this is like a map/game level/world (where each pixel represents some object)
Define WorldW = 6000, WorldH = 3000
Dim GameWorldMapWhatever.l (WorldW, WorldH)
Define Tx, Ty
; fill it with random
For Tx = 1 To WorldW
For Ty = 1 To WorldH
If Random(100, 1) <= 90
; leave it black
Else
GameWorldMapWhatever(tX, tY) = RGB(Random(255,128), 0, 0)
EndIf
Next Ty
Next Tx
For Tx = 0 To 10000
AddElement(mapStars())
mapStars()\size = Random(#RandMax)/#RandMax
mapStars()\cor = RGB(255, 255, 255)
mapStars()\x = Random(WorldW)
mapStars()\y = Random(WorldH)
Next Tx
; init
InitSprite()
InitKeyboard()
Define ScreenW = 800, ScreenH = 600
Define tWnd = OpenWindow(#PB_Any, 0,0, ScreenW, ScreenH, "SuperMegaExplosiveGame 4000k Overworld Earth by Lunasole INC, extremely dirty version only for 99.99$ buy right now and save -100$!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(tWnd), 0, 0, ScreenW, ScreenH, #False, 0, 0, #PB_Screen_SmartSynchronization)
; those 2 variables simulating a very primitive player ^^
; set initial player coordinates (center of a map)
Define PlayerX = WorldW * 0.5, PlayerY = WorldH * 0.5
Repeat
If WindowEvent() = #PB_Event_CloseWindow
Break
EndIf
; process keyboard controls to "move a player"
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
PlayerX - 10
ForEach mapStars()
If (mapStars()\x >= 0 And mapStars()\x <= WorldW ) And (mapStars()\y >= 0 And mapStars()\x <= WorldH )
mapStars()\x - 10
EndIf
Next
EndIf
If KeyboardPushed(#PB_Key_Right)
PlayerX + 10
ForEach mapStars()
If (mapStars()\x >= 0 And mapStars()\x <= WorldW ) And (mapStars()\y >= 0 And mapStars()\x <= WorldH )
mapStars()\x + 10
EndIf
Next
EndIf
If KeyboardPushed(#PB_Key_Up)
PlayerY - 10
ForEach mapStars()
If (mapStars()\x >= 0 And mapStars()\x <= WorldW ) And (mapStars()\y >= 0 And mapStars()\x <= WorldH )
mapStars()\y - 10
EndIf
Next
EndIf
If KeyboardPushed(#PB_Key_Down)
PlayerY + 10
ForEach mapStars()
If (mapStars()\x >= 0 And mapStars()\x <= WorldW ) And (mapStars()\y >= 0 And mapStars()\x <= WorldH )
mapStars()\y + 10
EndIf
Next
EndIf
; draw "objects"
; first determine which map area must be displayed
; only start of it is determined here for simplicity, EndX, EndY not used
Define StartX = PlayerX - ScreenW*0.5
Define StartY = PlayerY - ScreenH*0.5
; now draw
If StartDrawing(ScreenOutput())
;
; For tX = 0 To ScreenW-1
; For tY = 0 To ScreenH-1
; ; pixel color is taken from game level at given X/Y
; ;Plot(tX, tY, GameWorldMapWhatever(tX+StartX+1, tY+StartY+1))
; Circle(tX, tY,Random(#RandMax)/#RandMax,RGB(255, 255, 255))
; Next Ty
; Next tX
ForEach mapStars()
If (mapStars()\x >= 0 And mapStars()\x <= WorldW ) And (mapStars()\y >= 0 And mapStars()\x <= WorldH )
Circle(mapStars()\x,mapStars()\y,mapStars()\size,mapStars()\cor)
EndIf
Next
; this circle represents a "primitive player" model, lol
; again, for simplicity it's always at screen center
; also there are more nice variants to move camera, applying for example simple linear interpolation to X and Y
Circle(ScreenW * 0.5, ScreenH * 0.5, 5, #Red)
DrawText(0,0,"PlayerX= "+Str(PlayerX)+" PlayerY="+Str(playery))
StopDrawing()
EndIf
; change on-screen frame
FlipBuffers()
ForEver
End
![Image](https://www.mysocket.space/pb/pb-error.png)
![Image](https://www.mysocket.space/pb/pb-error2.png)
- skinkairewalker
- Enthusiast
- Posts: 637
- Joined: Fri Dec 04, 2015 9:26 pm
Re: 2D Games camera X and Y
thanks immensely for the tips#NULL wrote:That was just a generic example. The actual display function would be DisplaySprite/DisplayTransparentSprite/DrawImage etc. or whatever you are using.skinkairewalker wrote:Where can i found this function : " Display " ?
![Very Happy :D](./images/smilies/icon_biggrin.gif)
as it is my first trip to create a game in PB, I am a bit lost with having to do everything in a "raw" way, I am having difficulty understanding some basic algorithms of the 2d world of PB