Creating Lemmings Game, need platforms.
in
Programming Questions
•
1 year ago
Hi i am trying to build a game like lemmings, this ones called phlegmmings.... i have got as far as creating the array of phlegm but am stuck on the best way to create platforms that the phlegm will walk on / fall off.
How do i create levels or platforms to build my game, i think its some kind of collision detection but really need a hand.
any help would be appreciated.
import processing.opengl.*;
PImage myImage;
PFont f;
ArrayList<Platform> platforms = new ArrayList<Platform>();
ArrayList<Platform> platforms2 = new ArrayList<Platform>();
Phlegm[] phlegm; //an array of phlegm
int totalPhlegm = 0; //total phelgm
Timer timer; //one timer object
// A boolean to let us know if the game is over
boolean gameOver = false;
void setup() {
size(600, 600, OPENGL);
smooth();
myImage = loadImage("lungs.jpg");
textureMode(NORMALIZED);
frameRate(40);
phlegm = new Phlegm[10]; //creates 10 phlegm
timer = new Timer(600); //create a timer that goes off every 2 seconds
timer.start(); //start the timer
f = createFont("Arial", 12, true); // A font to write text on the screen
for (int i = 0; i < phlegm.length; i++) {
phlegm[i] = new Phlegm(width/2, height/2); //initialising each phlegm
}
}
void draw() {
background(0);
// image(myImage,0,0);
beginShape();
texture (myImage);//this calls the image
vertex(0, 0, 0, 0);
vertex(0, height, 0, 1);
vertex(width, height, 1, 1);
vertex(width, 0, 1, 0);
endShape(CLOSE);
// If the game is over
if (gameOver) {
textFont(f, 48);
textAlign(CENTER);
fill(0);
text("GAME OVER", width/2, height/2);
}
else {
// Check the timer
if (timer.isFinished()) {
// Deal with phlegm
// Initialize one phlegm
if (totalPhlegm < phlegm.length) {
phlegm[totalPhlegm] = new Phlegm(width/2, height/2);
// Increment totalPhlegm
totalPhlegm++;
}
timer.start();
}
for (int i = 0; i < phlegm.length; i++) {
phlegm[i].move();
phlegm[i].display();
}
}
}
void drawPlatforms()
{
for(Platform p: platforms)
{
p.display();
}
}
void map1()
{
Platform p1 = new Platform(0,250, 350, 250);
platforms.add( p1 );
Platform p2 = new Platform(425,250, 600,250);
platforms.add( p2 );
line (250,200,300,200);
line (500, 100, 500, 250);
line (500, 100, 600, 100);
line (500, 100, 600, 000);
rect (530, 175, 50, 75);
rect (540, 120, 25, 25);
//this code is to check for death
// if (phlegm.x > 530 && phlegm.x < 580 && phlegm.y > 175 && phlegm.y < 260 )
// screenNumber = 1 ;
//Reset();
}
class Phlegm {
float c; //color
float s = 10; //size of phlegm
float x, y; //variables for location of phlegm
float xspeed; // x speed of phlegm
float yspeed; // y speed of phlegm
float gravity;
// //new variable to keep track of whether phlegm is still alive
// boolean dead = false;
Phlegm (float x, float y) {
c = color(20, 200, 20); //all phlegms are the same color
this.x = x; //starting location x
this.y = y; //starting location y
xspeed = 1; //all plegms same speed
// gravity = 0.98;
yspeed = 1;
}
// void setLocation(float tempX, float tempY){
// x = tempX; //temporary location x
// y = tempY; //temporary location y
// }
//movement of phlegm
void move() {
//drop from start
y += yspeed;
if (y > height-10) {
y = height-10;
x = x + xspeed;
if ((x >= width-10) || (x < 10)) {
xspeed = xspeed * -1;
}
}
}
void display () {
stroke(0);
fill(20, 200, 20);
ellipse(x, y, s*2, s*2);
}
}
class Platform{ //all ground is the same
int x, y, w, h;
Platform(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
void display()
{
rect(x,y,w,h);
}
boolean below(Phlegm p)
{
if(p.x+p.s > x && p.x < x+w && abs(p.y-y) < p.s) return true;
return false;
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
}
else {
return false;
}
}
}
not sure if that is the best way to post code but here it is.....
thanks
How do i create levels or platforms to build my game, i think its some kind of collision detection but really need a hand.
any help would be appreciated.
import processing.opengl.*;
PImage myImage;
PFont f;
ArrayList<Platform> platforms = new ArrayList<Platform>();
ArrayList<Platform> platforms2 = new ArrayList<Platform>();
Phlegm[] phlegm; //an array of phlegm
int totalPhlegm = 0; //total phelgm
Timer timer; //one timer object
// A boolean to let us know if the game is over
boolean gameOver = false;
void setup() {
size(600, 600, OPENGL);
smooth();
myImage = loadImage("lungs.jpg");
textureMode(NORMALIZED);
frameRate(40);
phlegm = new Phlegm[10]; //creates 10 phlegm
timer = new Timer(600); //create a timer that goes off every 2 seconds
timer.start(); //start the timer
f = createFont("Arial", 12, true); // A font to write text on the screen
for (int i = 0; i < phlegm.length; i++) {
phlegm[i] = new Phlegm(width/2, height/2); //initialising each phlegm
}
}
void draw() {
background(0);
// image(myImage,0,0);
beginShape();
texture (myImage);//this calls the image
vertex(0, 0, 0, 0);
vertex(0, height, 0, 1);
vertex(width, height, 1, 1);
vertex(width, 0, 1, 0);
endShape(CLOSE);
// If the game is over
if (gameOver) {
textFont(f, 48);
textAlign(CENTER);
fill(0);
text("GAME OVER", width/2, height/2);
}
else {
// Check the timer
if (timer.isFinished()) {
// Deal with phlegm
// Initialize one phlegm
if (totalPhlegm < phlegm.length) {
phlegm[totalPhlegm] = new Phlegm(width/2, height/2);
// Increment totalPhlegm
totalPhlegm++;
}
timer.start();
}
for (int i = 0; i < phlegm.length; i++) {
phlegm[i].move();
phlegm[i].display();
}
}
}
void drawPlatforms()
{
for(Platform p: platforms)
{
p.display();
}
}
void map1()
{
Platform p1 = new Platform(0,250, 350, 250);
platforms.add( p1 );
Platform p2 = new Platform(425,250, 600,250);
platforms.add( p2 );
line (250,200,300,200);
line (500, 100, 500, 250);
line (500, 100, 600, 100);
line (500, 100, 600, 000);
rect (530, 175, 50, 75);
rect (540, 120, 25, 25);
//this code is to check for death
// if (phlegm.x > 530 && phlegm.x < 580 && phlegm.y > 175 && phlegm.y < 260 )
// screenNumber = 1 ;
//Reset();
}
class Phlegm {
float c; //color
float s = 10; //size of phlegm
float x, y; //variables for location of phlegm
float xspeed; // x speed of phlegm
float yspeed; // y speed of phlegm
float gravity;
// //new variable to keep track of whether phlegm is still alive
// boolean dead = false;
Phlegm (float x, float y) {
c = color(20, 200, 20); //all phlegms are the same color
this.x = x; //starting location x
this.y = y; //starting location y
xspeed = 1; //all plegms same speed
// gravity = 0.98;
yspeed = 1;
}
// void setLocation(float tempX, float tempY){
// x = tempX; //temporary location x
// y = tempY; //temporary location y
// }
//movement of phlegm
void move() {
//drop from start
y += yspeed;
if (y > height-10) {
y = height-10;
x = x + xspeed;
if ((x >= width-10) || (x < 10)) {
xspeed = xspeed * -1;
}
}
}
void display () {
stroke(0);
fill(20, 200, 20);
ellipse(x, y, s*2, s*2);
}
}
class Platform{ //all ground is the same
int x, y, w, h;
Platform(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
void display()
{
rect(x,y,w,h);
}
boolean below(Phlegm p)
{
if(p.x+p.s > x && p.x < x+w && abs(p.y-y) < p.s) return true;
return false;
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
}
else {
return false;
}
}
}
not sure if that is the best way to post code but here it is.....
thanks
1