Java 使用色调增加检查点计数器

Java 使用色调增加检查点计数器,java,hue,Java,Hue,我正在用java创建一个赛车游戏,有一个检查点计数器和一个圈计数器。我在轨道上画了线来增加检查点计数器。当检查点计数器达到9并且他们通过蓝线时,圈数计数器就会增加。问题是,当汽车越过一条线时,检查点计数器或圈数计数器要么继续增加,要么根本不移动 Player player; float scaleFactor, translateX, translateY; color c; int counterlap=0; int countercheck=0; void setup() { size(d

我正在用java创建一个赛车游戏,有一个检查点计数器和一个圈计数器。我在轨道上画了线来增加检查点计数器。当检查点计数器达到9并且他们通过蓝线时,圈数计数器就会增加。问题是,当汽车越过一条线时,检查点计数器或圈数计数器要么继续增加,要么根本不移动

Player player;
float scaleFactor, translateX, translateY;
color c;
int counterlap=0;
int countercheck=0;
void setup() { 
size(displayWidth, displayHeight);
frame.setResizable(true); //make the screen adjustable
background=loadImage("track.png");
player =  new Player(100, 100); //initialize the player object
scaleFactor = 2;
} 

PImage background; //define an image for the background
void draw() {
  background(0);
  imageMode(CORNER);
  pushMatrix();
  translate(width/2, height/2);
  scale(scaleFactor);
  translate(-width/2, -height/2);
  pushMatrix();
  translate(-player.position.x+width/2, -player.position.y+height/2);
  image(background, 0, 0, background.width, background.height); //draw background
  popMatrix();
  popMatrix();
  c = get(width/2, height/2);
  println(hue(c));
  player.draw(); //draws the player
  text("Laps "+counterlap, width-50, 20);//lapcounter
  if (countercheck>=9) { 
    if  ((hue(c)>135)&&(hue(c)<141)) {
      counterlap++;
      if ((counterlap>=1)&&(countercheck>=9)) {
        countercheck=0;
      }
    }
  }  
  text("Checkpoint "+countercheck+"/9", width-200, 20);//checkpoint counter
  if ((hue(c)>249)&&(hue(c)<254)) {
    countercheck++;
  }
}
class Player { //creates the player class for the player object
  PVector position, velocity; //defines position & velocity vectors
  float vel, accel, heading; //scalar magnitudes
  PImage car;
  Player(float xposition, float yposition) {
    position = new PVector(xposition, yposition); //initialize everything and pass the spawn point
    velocity = new PVector(0, 0);
    accel=0;
    car = loadImage("car.png"); //loads the image for the player
  }
  void draw() {
    pushMatrix();
    translate(width/2, height/2); //translates car origin to the position vector
    scale(scaleFactor);
    rotate(heading+PI); //rotates to the current heading
    imageMode(CENTER); //centers the car image for better rotation
    tint(color(255, 0, 255)); //colour of the car
    image(car, 0, 0, 64, 40); //draws the image and resizes to 64x40
    noTint(); //disable tint for remainder of frame
    popMatrix();
    if (up) { //acclerate when up is pressed
      accel=0.15;
    } else { //otherwise no accleration
      accel=0;
    }
    if (down) { //brake/reverse
      accel=-0.15;
    }
    if (left) {
      heading-=0.04*sqrt(abs(vel)); //
    }
    if (right) {
      heading+=0.04*sqrt(abs(vel));
    }
    if (!(up||down||left||right)) {
    }
    vel+=accel;
    velocity=PVector.fromAngle(heading);
    velocity.setMag(vel);
    if (hue(c)==97.48252) {
      vel*=0.9;
    } else {
      vel*=0.99;
    }
    velocity.limit(50);
    position.add(velocity);
    if (position.x>background.width) {
      position.x=background.width;
      velocity.x*=-0.1;
    }
    if (position.x<0) {
      position.x=0;
      velocity.x*=-0.1;
    }
    if (position.y>background.height) {
      position.y=background.height;
      velocity.y*=-0.1;
    }
    if (position.y<0) {
      position.y=0;
      velocity.y*=-0.1;
    }
  }
}
boolean up, down, left, right;

void mouseWheel(MouseEvent e)
{
  scaleFactor *= pow(2, e.getAmount() /10F);

  translateX -= e.getAmount() * mouseX / 100;

  translateY -= e.getAmount() * mouseY / 100;
}

void keyPressed() {
  if (keyCode == UP) {
    up = true;
    text("D", width-500, 20);
  }
  if (keyCode == DOWN) {
    down = true;
    text("R", width-500, 20);
  }
  if (keyCode == LEFT) {
    left = true;
  }
  if (keyCode == RIGHT) {
    right = true;
  }
  if (key == 'r')
  {
    scaleFactor = 2;

    translateX = 0;

    translateY = 0;
  }
}

void keyReleased() {
  if (keyCode == UP) {
    up = false;
  }
  if (keyCode == DOWN) {
    down = false;
  }
  if (keyCode == LEFT) {
    left = false;
  }
  if (keyCode == RIGHT) {
    right = false;
  }
}
Player;
float scaleFactor、translateX、translateY;
颜色c;
int反向搭接=0;
int反检查=0;
无效设置(){
尺寸(显示宽度、显示高度);
frame.setresizeable(true);//使屏幕可调整
背景=加载图像(“track.png”);
player=newplayer(100100);//初始化player对象
scaleFactor=2;
} 
年龄背景//为背景定义一个图像
作废提款(){
背景(0);
图像模式(角落);
pushMatrix();
平移(宽度/2,高度/2);
尺度(scaleFactor);
平移(-宽度/2,-高度/2);
pushMatrix();
平移(-player.position.x+宽度/2,-player.position.y+高度/2);
图像(背景,0,0,background.width,background.height);//绘制背景
popMatrix();
popMatrix();
c=get(宽度/2,高度/2);
println(色调(c));
player.draw();//绘制播放器
文本(“圈数”+反圈数,宽度-50,20);//圈数
如果(反检查>=9){
如果((色调(c)>135)和&(色调(c)=1)和&(反检查>=9)){
反检查=0;
}
}
}  
文本(“检查点”+反检查+“/9”,宽度-200,20)//检查点计数器
如果((色调(c)>249)和&(色调(c)背景宽度){
位置x=背景宽度;
速度x*=-0.1;
}
if(位置xbackground高度){
位置y=背景高度;
速度y*=-0.1;
}

如果(位置y当前方法中存在两个问题:

  • 如果你的球员停留在线上,他每停留一帧,你将计算一圈
  • 如果你的球员从一边的一点快速通过圈线到另一边的另一点…他已经越过了线,但是如果他站在正上方时没有中间帧,你将不计算圈数
要解决此问题,您不应查看“此帧中的播放器在哪里”,而应检查“在最后一帧中,播放器是否已从行的一侧移动到行的顶部或另一侧;--在执行此操作之前,他是否已对所有N个检查点行执行相同的操作”.由于这种情况只能发生一次,并且每圈只能发生一次,因此您现在可以正确计算圈数

这意味着你不能仅仅依靠色调;除非你确保,无论汽车的速度如何,都不可能在一个画面中“从一边到另一边”触发,而永远不会在顶部