We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I'm trying to create an RPG game for my Computer Science class and I want to make the camera follow my sprite around the canvas and have one big background image that only shows a portion on the canvas and as the user moves it goes to a new part of the image. Essentially creating the map for my game which the player can navigate.
Currently, I am drawing each scene after the player passes the edge and resizing the canvas to fit the image. This isn't too good because it displaces the window and the user has to keep moving it in order to see the full map. Please help.
This is the code I have so far:
PImage [] jerryWalking;
PImage bg;
int jerryFrames = 4;
String currentDirection = "right";
Boolean left, right, up, down ;
int currentFrame = 0;
Boolean walking = false;
int speed = 2;
int frameSpeed = 200;
float timeSinceLastFrame;
int bottomBorder = 250;
int topBorder = 1;
int scene = 1;
Jerry j = new Jerry(200, 200, 32, 32);
void loadAssets () {
jerryWalking = new PImage[jerryFrames];
for ( int i = 0; i<jerryFrames; i++) {
jerryWalking[i]=loadImage("jerry_"+currentDirection+(i+1)+".png");
}
}
void setup () {
surface.setResizable(true);
left = false;
right = false;
down = false;
up = false;
timeSinceLastFrame = millis();
bg = loadImage("scene1.png");
}
void draw () {
surface.setSize(bg.width, bg.height);
image(bg, 0, 0);
loadAssets();
j.display();
j.update();
println(j.x);
println();
println(j.y);
// left 1094
// right 1118
}
void keyPressed () {
switch(keyCode) {
case 37:
left = true;
currentDirection = "left";
walking = true;
break;
case 39:
right = true;
currentDirection = "right";
walking = true;
break;
case 38:
up = true;
currentDirection = "up";
walking = true;
break;
case 40:
currentDirection = "down";
down = true;
walking = true;
break;
}
}
void keyReleased () {
switch(keyCode) {
case 37:
left = false;
currentDirection = "left";
walking = false;
break;
case 39:
right = false;
currentDirection = "right";
walking = false;
break;
case 38:
up = false;
currentDirection = "up";
walking = false;
break;
case 40:
down = false;
currentDirection = "down";
walking = false;
break;
case 70:
speed--;
break;
case 71:
speed++;
break;
}
}
Answers
I think it’s called ViewPort
Try to search this in the forum
You can have a image which is much bigger than the canvas
Only a small portion is visible
Think of an image that is 5000x5000 and your canvas is 1000x800 then you‘d display the image at -2000, -2000 (imgx,imgy)
Now when the player moves he stays in the center and the map moves (changing imgx,imgy)
Thank you!
@SantiArr16 -- for extremely large maps, where you must load new portions as you move in any direction, one general approach is to use a tilemap.
There are a lot of tools out there to support this kind of approach
There are also many tutorials. Most are not specific to Processing, but if there are in Java they may be very similar.
See also previous RPG projects on the forums, such as https://forum.processing.org/two/discussion/26086/a-quick-beta-build-of-a-adventure-maze-game-based-around-the-minotaur-s-labyrinth
here is a demo that you can download:
https://www.openprocessing.org/sketch/149191
@Chrisir I'm having trouble understanding the viewport example that you provided me, I don't really know how to implement this into my game. Is there any tutorial online that could help me?
I am not sure about your skill level.
Do you have an image as background? Is it a jpg file? Which size is it?
see
https://en.wikipedia.org/wiki/Viewport
can you post your class Jerry and upload the images somewhere?
I made an example where I create an internal image (map) artificially and then the player runs over it
then the map is treated via a viewPort
solution for right and left ONLY
As you can see, the map / entire dungeon is 5000x5000 pixels, much bigger than the canvas. (You had it the same size)
The two viewPort variables show this map and move the map so that the player is always inside the viewPort.
The viewPort variables are negative values so that the map is shown very far left and above the screen so that the canvas can show the right part of the map.
Imagine the canvas / monitor stays where it is and you move the huge map underneath it left and right , up and down
Thank you, I will look at the code you provided me and I will try to implement it. I'm currently in school (where imgur is blocked) so I can't really upload them anywhere so, if you could provide me with your email, I could send them to you. Also here is my Jerry class.
I currently have an image as my background , its not a tile map. It seems to be working fine. Thanks again. This is my updated code, if you know of any way that I can send you the images please let me know ASAP.
BORDER CLASS :
just implement my solution and use an image as map much bigger than your canvas