カーレースゲーム

Processingで作ったカーレースゲームです。

Processingで作ったカーレースゲームです。 左上にフレームレートを表示しています。 道に当たり判定があるので、脱線はしません。 疑似3Ⅾのゲームで、割り算で遠近感(木の大きさ、道幅)を出しています。道がカーブする時、⇨ ⇦が出て、お知らせしてくれます。

ProcessingのZipダウンロードは、こちら

ソースコードです。⇩

PFont font;

float[] b = new float[2000];
void tex(String tex, int x, int y, int s, int r, int g, int b) {
  textFont(font, 20*s);
  fill(0);
  text(tex, 0+x+s+1, (20*s)+s+1+y);
  fill(r, g, b);
  text(tex, 0+x, (20*s)+y);
}
void stex(String tex, int x, int y, float s, int r, int g, int b) {
  textFont(font, 20*s);
  fill(0);
  for (int iy = -int(s); iy < int(s)+1; iy++) {
    for (int ix = -int(s); ix < int(s)+1; ix++) {
      text(tex, 0+x+ix+s, y+iy+s);
    }
  }
  fill(r, g, b);
  text(tex, 0+x+s, y+s);
}

int bba;

float ok;

float okh,okhh;

float speed;

float s;

float sy;

boolean right, left, down, up;

float dd;

PImage img;

PImage y;
PImage yh;

PImage k;

PImage bg = createImage(1080, 720, RGB);
PImage bga = createImage(1080, 720, RGB);

float ff;

int hhh;

void setup() {
  //textAlign(RIGHT, BOTTOM);
  img = loadImage("a.png");
  k = loadImage("b.png");
  y = loadImage("y.png");
  yh = loadImage("yh.png");
  font = loadFont("JF-Dot-Kappa-20-Regular-20.vlw"); 
  size(1080, 720);
  //noStroke();
  noSmooth();
  frameRate(60);
  background(255);
  //textAlign(CENTER, CENTER);
  textAlign(LEFT, TOP);
  for (int i = 0; i < 720; i++) {
    b[i] = sin(s)*16;
    s += 0.1;
  }

  for (int yyy = 0; yyy < 720; yyy++) {
    for (int xxx = 0; xxx < 1080; xxx++) {
      bg.pixels[hhh] = color(150+(yyy/3.3), 180+(yyy/4.5), 255);
      bga.pixels[hhh] = color(150+(yyy/3.3), 180+(yyy/4.5), 255);
      hhh++;
    }
  }
  hhh = 777600/2;
  dd = 100;
  for (int yyy = 720/2; yyy < 720; yyy++) {
    for (int xxx = 0; xxx < 1080; xxx++) {
      bg.pixels[hhh] = color(100+dd, 255, 60);
      hhh++;
    }
    dd -= 0.2;
  }
}

float fc = 1.01;

float nfc = 3;

float kj;

float bl;

float add;

float gg;

float aa;

float kkk;

float x = 1080/2-75;

float a;

float abb;

float ss = -100;

float xs;

float st = 0;

void draw() {
  background(255);
  aa = 1;
  a = 2;
  abb = nfc;
  gg = 1;
  for (float ys = 720/2; ys < 720; ys++) {
    stroke(0);
    line(1080/2-bl+(b[int(a)]*gg), ys, 1080/2+bl+(b[int(a)]*gg), ys);  
    bl += 1;
    add += 64/aa;
    if (aa > 0)aa += 0.7;
    if (add > 32)add = -32;
    a += abb;
    abb /= fc;
    gg *= 1.001;
  }

  a = 2;
  //add = ss;
  add = ss;
  if (ss < -180)ss = -100;
  ss -= 16;
  bl = 10;
  if (green(get(int(x+75)-100, 720/2+200)) > 100) {
    x += 16;
    //xs = 8;
    if (speed > 2)speed -= 2;
  }
  if (green(get(int(x+75)+100, 720/2+200)) > 100) {
    x -= 16;
    //xs = -8;
    if (speed > 2)speed -= 2;
  }
  
  
  image(bg, 0, 0);
  aa = 1;

  abb = nfc;
  gg = 1;
  for (float ys = 720/2; ys < 720; ys++) {

    stroke(250);
    line((1080/2-bl+(b[int(a)]*gg))-2, ys, (1080/2+bl+(b[int(a)]*gg))+2, ys);  

    stroke((255-(bl*0.7/16+128+64+32))+128+64);
    line(1080/2-bl+(b[int(a)]*gg), ys, 1080/2+bl+(b[int(a)]*gg), ys);  
    kkk += 1-((ys-360)/720);
    bl += 1;
    add += 64/aa;
    if (aa > 0)aa += 0.7;
    if (add > 32)add = -32;
    a += abb;
    if(abb > 0)abb /= fc;
    gg *= 1.001;
  }
  a = 2;
  //add = ss;
  add = ss;
  if (ss < -180)ss = -100;
  ss -= 16;
  bl = 20;


  aa = 1;

  abb = nfc;
  gg = 1;

  kkk = -kj;

  kj += speed;

  if (kj > 30)kj = 0;

  for (float ys = 720/2-25; ys < 720*1.5; ys++) {
    if (kkk > 0) {
      image(k, (1080/2-bl+(b[int(a)]*gg))-2-(100*((ys-360)/720)*4)-100-12, ys-(200*((ys-360)/720)*4)-25, (100*((ys-360)/720)*4)+25, (200*((ys-360)/720)*4)+50);
      image(k, (1080/2+bl+(b[int(a)]*gg))+2+(100*((ys-360)/720)*4)+100-12, ys-(200*((ys-360)/720)*4)-25, (100*((ys-360)/720)*4)+25, (200*((ys-360)/720)*4)+50);
      kkk = -30;
    }
    kkk += 1-((ys-360)/720)+0.003;
    bl += 1;
    add += 64/aa;
    if (aa > 0)aa += 0.7;
    if (add > 32)add = -32;
    a += abb;
    abb /= fc;
    gg *= 1.001;
  }
  
  //image(bga, 0, 0);
  
  a = 2;
  //add = ss;
  add = ss;
  if (ss < -180)ss = -100;
  ss -= 16;
  bl = 20;

  okh = 0;
  for (int ffi = 0; ffi < speed*2; ffi++) {
    okhh = ok;
    ok = (noise(s)*(st*2)-st);
    okh += ok-okhh;
    b[2] = ok;
    s += 0.0004;
    for (int i = 1; i < 720-1; i++) {
      b[(720-i)+1] = b[(720-i)];
    }
  }
  if (up) {
    speed += 0.1;
  }

  if (right)xs += 1;
  if (left)xs -= 1;

  xs /= 1.1;

  x += xs;

  if (speed > 0)speed -= 0.07;
  if (speed > 10)speed = 10;

  if (st < 480 && speed > 0.05)st += 5;
  noStroke();
  image(img, x, 720/2+100, 150, 100);
  /*
  fill(0);
   rect(1080/2-60, 720/2+150, 10, 10);
   fill(0);
   rect(1080/2+60, 720/2+150, 10, 10);
   */
  stex(str(frameRate), 0, 0, 2, 255, 255, 255);
  if (okh < -2) {
    image(y, 1080/2-100+(sin(sy)*25), 0, 200, 100);
  }
  if (okh > 2) {
    image(yh, 1080/2-100+(sin(sy)*25), 0, 200, 100);
  }
  sy += PI/30;
}

void keyPressed()
{
  if (keyCode == RIGHT || key == 'd') right = true;
  if (keyCode == LEFT || key == 'a')  left  = true;
  if (keyCode == UP || key == 'w')  up    = true;
  if (keyCode == DOWN || key == 's')    down  = true;
}

void keyReleased()
{
  if (keyCode == RIGHT || key == 'd') right = false;
  if (keyCode == LEFT || key == 'a')  left  = false;
  if (keyCode == UP || key == 'w')  up    = false;
  if (keyCode == DOWN || key == 's')    down  = false;
}

使用画像です。かっこいいオリジナル画像を作って、このソースコードを参考にチェレンジしてみてください!

 

Follow me!

コメントを残す