We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys recently i created this little game but i am not able to test it completely because i do not have a second computer on the same network i was hoping you guys could test it here is the code:
RPG_Game.pde:
import oscP5.*;
import netP5.*;
import processing.net.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
Player player;
Bullet bullet;
Bullet playerBullet;
Player player2;
String playerIP = "";
String myIP;
boolean setup = true;
boolean load = false;
float loadV;
boolean connected = false;
float previous;
float stepPrevious;
boolean error = false;
int x;
int y;
boolean changeStep = false;
int step = 1;
int stepValue = 5;
float previousWay = UP;
boolean walking = false;
int range = 800;
boolean fireable = false;
void setup()
{
bullet = new Bullet(color(255, 0, 0));
playerBullet = new Bullet(color(0, 255, 0));
background(255, 102, 0);
myIP = Server.ip();
fullScreen();
oscP5 = new OscP5(this, 23492);
textSize(48);
textAlign(CENTER);
player = new Player();
player2 = new Player();
x = width / 2;
y = height / 2;
}
void draw()
{
checkTime();
background(255, 102, 0);
set_up();
load();
player.show();
player2.show();
bullet.show();
playerBullet.show();
checkBulletPlayer();
checkBulletOtherPlayer();
showScore();
if (walking) moveCharacter();
if (bullet.show) bullet.move();
if (playerBullet.show) playerBullet.move();
if (!bullet.show) bullet.setDir(previousWay);
if (!playerBullet.show) playerBullet.setDir(previousWay);
}
void keyPressed()
{
keySetup();
if (keyCode == UP || keyCode == DOWN || keyCode == LEFT || keyCode == RIGHT) walking = true;
if (key == 'f' && bullet.show == false && !setup && !load)
{
bullet.fire();
OscMessage myMessage = new OscMessage("playerBullet");
myMessage.add(bullet.bx);
myMessage.add(bullet.by);
myMessage.add(bullet.bdir);
oscP5.send(myMessage, myRemoteLocation);
}
if (key == 'q' && !setup && !load) range = 300;
if (key == 'w' && !setup && !load) range = 500;
if (key == 'e' && !setup && !load) range = 800;
}
void keyReleased()
{
if (keyCode == UP || keyCode == DOWN || keyCode == LEFT || keyCode == RIGHT) walking = false;
}
public void oscEvent(OscMessage theOscMessage)
{
if (theOscMessage.checkAddrPattern("playerBullet"))
{
playerBullet.bx = theOscMessage.get(0).floatValue();
playerBullet.by = theOscMessage.get(1).floatValue();
playerBullet.setDir(theOscMessage.get(2).floatValue());
playerBullet.fire();
}
if (theOscMessage.checkAddrPattern("PlayerCoor"))
{
player2.move(theOscMessage.get(0).intValue(), theOscMessage.get(1).intValue(), theOscMessage.get(2).intValue(), theOscMessage.get(3).intValue());
}
if (theOscMessage.checkAddrPattern("ConnectReq"))
{
myRemoteLocation = new NetAddress(theOscMessage.get(0).stringValue(), 23492);
OscMessage myMessage = new OscMessage("Connect");
myMessage.add("yes");
oscP5.send(myMessage, myRemoteLocation);
}
if (theOscMessage.checkAddrPattern("Connect")) connected = true;
}
Bullet.pde:
class Bullet
{
float bx;
float by;
float bdir;
boolean show = false;
float previousX;
float previousY;
color clr;
Bullet(color a)
{
bdir = UP;
clr = a;
}
void move()
{
if (bdir == UP) by -= 55;
if (bdir == LEFT) bx -= 55;
if (bdir == DOWN) by += 55;
if (bdir == RIGHT) bx += 55;
if(dist(previousX, previousY, bx, by) > range) show = false;
}
void show()
{
if (show)
{
noStroke();
fill(clr);
ellipse(bx + 24, by + 24, 10, 10);
stroke(0);
fill(255);
}
if(!show) bx = by = -1000;
}
void fire()
{
bx = player.x;
by = player.y;
previousX = bx;
previousY = by;
show = true;
}
void setDir(float _dir)
{
bdir = _dir;
}
}
Draw.pde:
void set_up()
{
if (setup)
{
text("Enter Other Players IP:", width / 2, height / 3);
text("Your IP: " + myIP, width / 2, height / 7);
fill(0, 188, 255);
text(playerIP, width / 2, height / 2 + 15);
fill(255);
if (error) text("Failed to Connect or Other Machine did not Respond", width / 2, height / 2 + 200);
}
}
void load()
{
if (load)
{
if (connected) loadV += 5;
fill(255);
text("Connecting to: " + playerIP, width / 2, height / 7);
rectMode(CENTER);
rect(width / 2, height / 2, 500, 100);
fill(255, 0, 0);
rectMode(CORNER);
rect(width / 2 - 250, height / 2 - 50, loadV, 100);
if (millis() - previous > 5000 && loadV < 500)
{
loadV = 0;
setup = true;
load = false;
connected = false;
error = true;
}
if (loadV == 500) load = false;
}
}
void keySetup()
{
if (setup && keyCode != CONTROL && keyCode != SHIFT && keyCode != 524)
{
if (keyCode == BACKSPACE)
{
if (playerIP.length() > 0) playerIP = playerIP.substring(0, playerIP.length() - 1);
} else
{
if (keyCode == ENTER)
{
error = false;
if (!playerIP.equals(""))
{
myRemoteLocation = new NetAddress(playerIP, 23492);
setup = false;
load = true;
OscMessage myMessage = new OscMessage("ConnectReq");
myMessage.add(myIP);
oscP5.send(myMessage, myRemoteLocation);
previous = millis();
}
} else
{
playerIP += key;
}
}
}
if (keyCode == TAB)
{
loadV = 0;
setup = true;
load = false;
connected = false;
}
}
void moveCharacter()
{
if (!setup && !load)
{
if (keyCode == UP)
{
previousWay = UP;
if (y > 0) y -= stepValue;
if (changeStep)
{
if (step == 1) step = 2;
else if (step == 2) step = 1;
changeStep = false;
}
player.move(x, y, UP, step);
OscMessage myMessage = new OscMessage("PlayerCoor");
myMessage.add(x);
myMessage.add(y);
myMessage.add(UP);
myMessage.add(step);
oscP5.send(myMessage, myRemoteLocation);
}
if (keyCode == DOWN)
{
previousWay = DOWN;
if (y + 65 < height) y += stepValue;
if (changeStep)
{
if (step == 1) step = 2;
else if (step == 2) step = 1;
changeStep = false;
}
player.move(x, y, DOWN, step);
OscMessage myMessage = new OscMessage("PlayerCoor");
myMessage.add(x);
myMessage.add(y);
myMessage.add(DOWN);
myMessage.add(step);
oscP5.send(myMessage, myRemoteLocation);
}
if (keyCode == LEFT)
{
previousWay = LEFT;
if (x > 0) x -= stepValue;
if (changeStep)
{
if (step == 1) step = 2;
else if (step == 2) step = 1;
changeStep = false;
}
player.move(x, y, LEFT, step);
OscMessage myMessage = new OscMessage("PlayerCoor");
myMessage.add(x);
myMessage.add(y);
myMessage.add(LEFT);
myMessage.add(step);
oscP5.send(myMessage, myRemoteLocation);
}
if (keyCode == RIGHT)
{
previousWay = RIGHT;
if (x + 65 < width) x += stepValue;
if (changeStep)
{
if (step == 1) step = 2;
else if (step == 2) step = 1;
changeStep = false;
}
player.move(x, y, RIGHT, step);
OscMessage myMessage = new OscMessage("PlayerCoor");
myMessage.add(x);
myMessage.add(y);
myMessage.add(RIGHT);
myMessage.add(step);
oscP5.send(myMessage, myRemoteLocation);
}
}
}
void checkTime()
{
if (millis() - stepPrevious >= 200)
{
stepPrevious = millis();
changeStep = true;
}
}
void checkBulletPlayer()
{
if (playerBullet.bx > x - 15 && playerBullet.bx < x + 32 && playerBullet.by > y - 15 && playerBullet.by < y + 32)
{
player.health -= 20;
if (player.health <= 0)
{
player2.score++;
player.health = 100;
player.x = width / 2;
player.y = height / 2;
x = width / 2;
y = height / 2;
}
}
}
void checkBulletOtherPlayer()
{
if (bullet.bx > player2.x - 15 && bullet.bx < player2.x + 32 && bullet.by > player2.y - 15 && bullet.by < player2.y + 32)
{
player2.health -= 20;
if (player2.health <= 0)
{
player.score++;
player2.health = 100;
player2.x = width / 2;
player2.y = height / 2;
}
}
}
void showScore()
{
if (!setup && !load)
{
fill(255);
text((int) player.score, width / 10, height / 10);
text((int) player2.score, width - 150, height / 10);
}
}
Player.pde:
class Player
{
PImage player = loadImage("character.png");
PImage sprite = player.get(0, 97, 32, 32);;
float x;
float y;
float dir;
int foot = 1;
float health = 100;
float score = 0;
Player()
{
x = width / 2;
y = height / 2;
dir = UP;
}
void move(float _x, float _y, float _dir, int _foot)
{
x = _x;
y = _y;
dir = _dir;
foot = _foot;
if(dir == UP && foot == 1) sprite = player.get(0, 97, 32, 32);
if(dir == UP && foot == 2) sprite = player.get(66, 97, 32, 32);
if(dir == LEFT && foot == 1) sprite = player.get(0, 32, 32, 32);
if(dir == LEFT && foot == 2) sprite = player.get(66, 32, 32, 32);
if(dir == DOWN && foot == 1) sprite = player.get(0, 128, 32, 32);
if(dir == DOWN && foot == 2) sprite = player.get(66, 128, 32, 32);
if(dir == RIGHT && foot == 1) sprite = player.get(0, 191, 32, 32);
if(dir == RIGHT && foot == 2) sprite = player.get(66, 191, 32, 32);
}
void show()
{
if (!setup && !load)
{
sprite.resize(48, 48);
image(sprite, x, y);
fill(map(health, 20, 100, 255, 0), 0, 0);
ellipse(x, y, 10, 10);
fill(255);
}
}
}
I am open to any suggestions and opinions. Thanks
and here is the character.png file: