コンテンツにスキップ

ハードルゲーム

img01.png

クリック長押しで力を溜めてジャンプし、障害物を避けるゲームです。

class Player{
public float x;
public float y;
public float vy;
public float power;
public int status;
}
class Wall{
public float x;
public float y;
public float speed;
public float w;
public float h;
public int status = 0;
}
class GameData{
public int score;
public int status = 1;
}
Player player;
ArrayList<Wall> wallList = new ArrayList<Wall>();
GameData gameData;
void setup(){
size(600, 400);
gameInit();
}
void draw(){
background(0);
if(gameData.status == 1){
if(player.status == 1){
player.power += 1;
}
player.vy += 1;
player.y += player.vy;
if(player.y > 320){
player.y = 320;
player.vy = 0;
if(player.status == 2) player.status = 0;
}
for(int i=0; i<wallList.size(); i++){
Wall w = wallList.get(i);
w.x += w.speed;
if(w.status == 0){
if(w.x + w.w < player.x){
gameData.score += 1;
w.status = 1;
}
}
}
// 当たり判定
for(int i=0; i<wallList.size(); i++){
Wall w = wallList.get(i);
int hit = isHit(player.x, player.y, 30, 30, w.x, w.y, w.w, w.h);
if(hit == 1){
gameOver();
}
}
}
fill(255, 255, 255, 100);
textSize(100);
textAlign(CENTER, CENTER);
text(gameData.score / 2, width/2, height/2 - 100);
// プレイヤー
fill(255);
if(gameData.status == 99){
fill(200, 50, 50);
}
rect(player.x, player.y, 30, 30);
// メーター
fill(255);
rect(player.x, player.y - 20, 30, 10);
fill(0, 0, 255);
rect(player.x, player.y - 20, 30 * min(player.power / 60, 1), 10);
// 壁
for(int i=0; i<wallList.size(); i++){
Wall w = wallList.get(i);
fill(255, 255, 255);
rect(w.x, w.y, w.w, w.h);
}
// 土台
fill(255);
rect(0, 350, width, height);
}
void gameInit(){
player = new Player();
player.x = 100;
player.y = 300;
player.vy = 0;
for(int i=0; i<100; i++){
Wall w = new Wall();
w.x = i * width + width;
w.y = random(200, 300);
w.speed = -5;
w.w = 30;
w.h = height - w.y;
wallList.add(w);
Wall top = new Wall();
top.x = w.x;
top.y = 0;
top.speed = w.speed;
top.w = 30;
top.h = w.y - 200;
wallList.add(top);
}
gameData = new GameData();
}
void gameOver(){
gameData.status = 99;
}
void mousePressed(){
if(player.status == 0){
player.status = 1;
}
}
void mouseReleased(){
player.status = 2;
player.vy = player.power * -1 * 0.5;
player.power = 0;
}
int isHit(float px, float py, float pw, float ph, float ex, float ey, float ew, float eh){
if(px < ex + ew && px + pw > ex){
if(py < ey + eh && py + ph > ey){
return 1;
}
}
return 0;
}