After a few tries, it still won’t get to work.
Somewhat created a game in which the object collides with multiple stuff on the map.
I have created a player, which is the image I can move with the cursor. What I actually want is that the player isn’t able to touch the border in the board, those are the 1 in the array. Also, a possibility when the player picks up the “chests” it disappears
FighterGame
int xblocks;
int yblocks;
int blockSize = 40;
int score = 0;
PImage friendly;
Friendly f1;
void setup() {
size(600, 600);
setupGame();
friendly = loadImage("spaceman.png");
f1 = new Friendly(30,50,128,50);
}
void draw() {
drawGame();
f1.display();
f1.keyPressed();
collision();
textAlign(RIGHT);
textSize(50);
text(score, width - 70, 100);
}
Friendly
class Friendly{
int friendlyX;
int friendlyY;
int friendlyWidth = 20;
int friendlyHeight = 50;
float direction;
boolean dead = false;
int maxHealth = 100;
int health = 100;
int healthDecrease = 1;
int healthBarWidth = 60;
Friendly(int tempX, int tempY, int tempWidth, int tempHeight){
friendlyX = tempX;
friendlyY = tempY;
friendlyWidth = tempWidth;
friendlyHeight = tempHeight;
}
void display(){
noStroke();
fill(189, 195, 199);
rectMode(CORNER);
rect(friendlyX-(healthBarWidth/2), friendlyY, healthBarWidth, 5);
if (health > 60) {
fill(46, 204, 113);
} else if (health > 30) {
fill(230, 126, 34);
} else {
fill(231, 76, 60);
}
rectMode(CORNER);
rect(friendlyX-(healthBarWidth/2), friendlyY, healthBarWidth*(health/maxHealth), 5);
image(friendly, friendlyX, friendlyY, friendlyWidth, friendlyHeight);
}
void decreaseHealth(){
health -= healthDecrease;
if (health <= 0) {
gameOver();
}
}
void gameOver() {
println("game over");
noLoop();
}
void keyPressed(){
if (keyPressed) {
if (keyCode == RIGHT){
friendlyX = friendlyX + 4;
direction = 0;
}else if (keyCode == LEFT){
friendlyX = friendlyX - 4;
direction = PI;
}else if (keyCode == UP){
friendlyY = friendlyY - 4;
direction = PI * 3/2;
}else if (keyCode == DOWN){
friendlyY = friendlyY + 4;
direction = PI/2;
}
}
}
}
Board
int[][] board = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
Game Board
final int boardwidth = board[0].length;
final int boardheight = board.length;
int blockDiameter;
void setupGame() {
blockDiameter = width/(boardwidth+2);
int chestAmount = 10;
placeChest(chestAmount);
}
void collision(){
}
void drawGame() {
background(#FFFFFF);
tekenBord();
}
void drawBoard() {
int marge = blockDiameter;
int x = marge, y = marge;
for (int r = 0; r < boardheight; r++) {
for (int k = 0; k < boardwidth; k++) {
noStroke();
if (board[r][k] == 0) {
fill(#FFFFFF);
}
if (board[r][k] == 1) {
fill(0);
}
if (board[r][k] == 2) {
fill(#FFFF00);
}
rect(x, y, blockDiameter, blockDiameter);
x = x+ blockDiameter;
}
y = y+ blockDiameter;
x = marge;
}
}
void placeChest(int amount) {
int r = round(random(boardheight-1));
int k = round(random(boardwidth-1));
for (int i = 0; i< amount; i++) {
while (board[r][k] > 0) {
r = round(random(boardheight-1));
k = round(random(boardwidth-1));
}
board[r][k] = 2;
}
}
Also would accept tips