我正在使用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;
}
}
答案 0 :(得分:0)
目前的方法存在两个问题:
为了解决这个问题,你不应该看看&#34;这个框架中的玩家在哪里&#34;,而应该检查&#34;玩家是否在最后一帧中移动了线路在线路的顶部或另一侧; - 并且在这样做之前他已经为所有N个检查站线做了同样的事情&#34;。由于这只能发生一次,而且每圈只发生一次,现在你可以正确计算圈数。
这意味着你不能单独依靠色调;除非你确定,无论车速如何,都不可能从一侧触发到另一侧,而不是在正确的位置上#34;在一个框架中。