今天电脑课老师给了个这个程序,然后叫我们设置按住Shift的时候往左往右的速度会是平常速度的三倍,而上下的速度改成直接是平常速度的三倍不用按Shift,求各位大神帮忙。。。
import java.util.Iterator;
int x, y;
boolean upPressed, leftPressed, downPressed, rightPressed;
boolean enterPressed;
ArrayList<Bullet> bullets; // List of bullet objects
ArrayList<UFO> ufos; // List of the ufo objects
boolean canShoot, sprint;
PImage bg, shooter, ufo, loser, winner;
boolean moveLeft;
PFont tFont, font;
int screenNum, roundNum, score, ufoSpeed;
void setup() {
size(600, 600);
smooth();
background(0);
imageMode(CENTER);
rectMode(CENTER);
stroke(255);
x = width / 2;
y = 500;
bullets = new ArrayList<Bullet>();
ufos = new ArrayList<UFO>();
canShoot = true;
bg = loadImage("orion.jpg");
shooter = loadImage("StarShooter.gif");
ufo = loadImage("UFO.gif");
loser = loadImage("loser.gif");
winner = loadImage("winner.gif");
font = loadFont("TimesNewRomanPS-BoldMT-25.vlw");
tFont = loadFont("KristenITC-Regular-50.vlw");
screenNum = 0;
roundNum = 1;
score = 0;
ufoSpeed = 3;
}
void draw() {
background(bg);
// Title Screen (screenNum 0)
if (screenNum == 0) {
displayMenu();
if (keyCode == ENTER && !enterPressed) {
setUpUFOs(1);
bullets.clear();
screenNum = 1;
}
}
// Game Screen (screenNum 1)
else if (screenNum == 1) {
strokeWeight(1);
line(0, 550, height, 550);
// Display all the ufos
for (Iterator iterator = ufos.iterator(); iterator.hasNext();) {
UFO s = (UFO) iterator.next();
// The ufo returns True if it has been removed (ie hit by bullet)
if (s.show()) {
iterator.remove();
score++;
}
}
if (ufos.isEmpty()) {
screenNum = 2;
}
// Display all the bullets
for (Iterator iterator = bullets.iterator(); iterator.hasNext();) {
Bullet s = (Bullet) iterator.next();
s.show();
if (s.y < 0)
iterator.remove();
}
fill(255);
image(shooter, x, y);
displayScore();
int multi = sprint ? 2 : 1;
if (rightPressed && x + 26 < width)
x += 2 * multi;
if (leftPressed && x - 26 > 0)
x -= 2 * multi;
if (upPressed)
y -= 2 * multi;
if (downPressed && y < height - 26)
y += 2 * multi;
}
// Level Complete Screen (screenNum 2)
else if (screenNum == 2) {
fill(255);
textAlign(CENTER);
if (roundNum == 10) {
background(winner);
textFont(tFont, 45);
text("Congratulations!", width / 2, height / 2 - 100);
textFont(font, 18);
text("You must be able to press spacebar really quickly!", width / 2, height / 2 - 50);
text("Press Enter to go back to the main menu", width / 2, height - 40);
if (keyCode == ENTER) {
enterPressed = true;
setup();
}
} else {
textFont(font);
text("Press Enter to continue to the next round.", width / 2, height / 2);
if (keyCode == ENTER) {
roundNum++;
setUpUFOs(roundNum);
screenNum = 1;
bullets.clear();
x = width / 2;
y = 500;
}
}
}
// Game over screen (screeNum 3 or 4)
else if (screenNum == 3 || screenNum == 4) {
background(loser);
textAlign(CENTER);
textFont(font);
text("You were able to get " + score + " point(s) at Round " + roundNum + ".", width / 2, height / 2);
if (screenNum == 3) {
text("Try not to get hit!", width / 2, height / 2 + 50);
} else {
text("Try to destroy every UFO before it passes the border.", width / 2, height / 2 + 50);
}
if (keyCode == ENTER) {
enterPressed = true;
setup();
}
}
}
void displayMenu() {
textAlign(CENTER);
textFont(tFont);
text("Jacky's Space Shooter", width / 2, 200);
textFont(font);
text("Press Enter to start the game.", width / 2, height / 2);
}
void displayScore() {
fill(255);
textFont(font, 16);
textAlign(LEFT);
text("Score: " + score, 10, 20);
textAlign(RIGHT);
text("Round " + roundNum, width - 10, 20);
}
void moveDownUFOs() {
for (Iterator iterator = ufos.iterator(); iterator.hasNext();) {
UFO s = (UFO) iterator.next();
s.ufoY += 7 * roundNum;
if (s.ufoY > 550)
screenNum = 4;
}
}
void setUpUFOs(int level) {
int rows = 0;
for (int y = 10; y < height - 200; y += 25) {
for (int x = 100; x < width - 100; x += 35) {
ufos.add(new UFO(x, y));
}
rows++;
if (rows == level + 3)
return;
}
}
void keyPressed() {
if (keyCode == RIGHT) rightPressed = true;
if (keyCode == LEFT) leftPressed = true;
if (keyCode == UP) upPressed = true;
if (keyCode == DOWN) downPressed = true;
if (keyCode == ' ' && canShoot) {
bullets.add(new Bullet(x, y - 20));
canShoot = false;
}
if (keyCode == SHIFT) sprint = true;
}
void keyReleased() {
if (keyCode == RIGHT) rightPressed = false;
if (keyCode == LEFT) leftPressed = false;
if (keyCode == UP) upPressed = false;
if (keyCode == DOWN) downPressed = false;
if (keyCode == ENTER) enterPressed = false;
if (keyCode == ' ' && !canShoot) canShoot = true;
if (keyCode == SHIFT) sprint = false;
}
UFO
// You are allowed to edit this code
public class UFO {
int ufoX, ufoY;
UFO(int x, int y) {
ufoX = x;
ufoY = y;
}
boolean show() {
if (moveLeft) {
if (ufoX < 20) {
moveLeft = false;
moveDownUFOs();
} else
ufoX -= ufoSpeed;
} else {
if (ufoX > width - 20) {
moveLeft = true;
moveDownUFOs();
}
else
ufoX += ufoSpeed;
}
for (Iterator iterator = bullets.iterator(); iterator.hasNext();) {
Bullet s = (Bullet) iterator.next();
if (dist(s.x, s.y - 20, ufoX, ufoY) < 18) {
iterator.remove();
return true;
}
}
if (dist(ufoX, ufoY, x, y) < 20) {
screenNum = 3;
}
image(ufo, ufoX, ufoY);
return false;
}
}
Bullet
// You are allowed to edit this code
public class Bullet {
int x, y;
Bullet(int x, int y) {
this.x = x;
this.y = y;
}
void show() {
y -= 15;
strokeWeight(6);
stroke(0,255,0);
line(x, y, x, y - 20);
}
}
import java.util.Iterator;
int x, y;
boolean upPressed, leftPressed, downPressed, rightPressed;
boolean enterPressed;
ArrayList<Bullet> bullets; // List of bullet objects
ArrayList<UFO> ufos; // List of the ufo objects
boolean canShoot, sprint;
PImage bg, shooter, ufo, loser, winner;
boolean moveLeft;
PFont tFont, font;
int screenNum, roundNum, score, ufoSpeed;
void setup() {
size(600, 600);
smooth();
background(0);
imageMode(CENTER);
rectMode(CENTER);
stroke(255);
x = width / 2;
y = 500;
bullets = new ArrayList<Bullet>();
ufos = new ArrayList<UFO>();
canShoot = true;
bg = loadImage("orion.jpg");
shooter = loadImage("StarShooter.gif");
ufo = loadImage("UFO.gif");
loser = loadImage("loser.gif");
winner = loadImage("winner.gif");
font = loadFont("TimesNewRomanPS-BoldMT-25.vlw");
tFont = loadFont("KristenITC-Regular-50.vlw");
screenNum = 0;
roundNum = 1;
score = 0;
ufoSpeed = 3;
}
void draw() {
background(bg);
// Title Screen (screenNum 0)
if (screenNum == 0) {
displayMenu();
if (keyCode == ENTER && !enterPressed) {
setUpUFOs(1);
bullets.clear();
screenNum = 1;
}
}
// Game Screen (screenNum 1)
else if (screenNum == 1) {
strokeWeight(1);
line(0, 550, height, 550);
// Display all the ufos
for (Iterator iterator = ufos.iterator(); iterator.hasNext();) {
UFO s = (UFO) iterator.next();
// The ufo returns True if it has been removed (ie hit by bullet)
if (s.show()) {
iterator.remove();
score++;
}
}
if (ufos.isEmpty()) {
screenNum = 2;
}
// Display all the bullets
for (Iterator iterator = bullets.iterator(); iterator.hasNext();) {
Bullet s = (Bullet) iterator.next();
s.show();
if (s.y < 0)
iterator.remove();
}
fill(255);
image(shooter, x, y);
displayScore();
int multi = sprint ? 2 : 1;
if (rightPressed && x + 26 < width)
x += 2 * multi;
if (leftPressed && x - 26 > 0)
x -= 2 * multi;
if (upPressed)
y -= 2 * multi;
if (downPressed && y < height - 26)
y += 2 * multi;
}
// Level Complete Screen (screenNum 2)
else if (screenNum == 2) {
fill(255);
textAlign(CENTER);
if (roundNum == 10) {
background(winner);
textFont(tFont, 45);
text("Congratulations!", width / 2, height / 2 - 100);
textFont(font, 18);
text("You must be able to press spacebar really quickly!", width / 2, height / 2 - 50);
text("Press Enter to go back to the main menu", width / 2, height - 40);
if (keyCode == ENTER) {
enterPressed = true;
setup();
}
} else {
textFont(font);
text("Press Enter to continue to the next round.", width / 2, height / 2);
if (keyCode == ENTER) {
roundNum++;
setUpUFOs(roundNum);
screenNum = 1;
bullets.clear();
x = width / 2;
y = 500;
}
}
}
// Game over screen (screeNum 3 or 4)
else if (screenNum == 3 || screenNum == 4) {
background(loser);
textAlign(CENTER);
textFont(font);
text("You were able to get " + score + " point(s) at Round " + roundNum + ".", width / 2, height / 2);
if (screenNum == 3) {
text("Try not to get hit!", width / 2, height / 2 + 50);
} else {
text("Try to destroy every UFO before it passes the border.", width / 2, height / 2 + 50);
}
if (keyCode == ENTER) {
enterPressed = true;
setup();
}
}
}
void displayMenu() {
textAlign(CENTER);
textFont(tFont);
text("Jacky's Space Shooter", width / 2, 200);
textFont(font);
text("Press Enter to start the game.", width / 2, height / 2);
}
void displayScore() {
fill(255);
textFont(font, 16);
textAlign(LEFT);
text("Score: " + score, 10, 20);
textAlign(RIGHT);
text("Round " + roundNum, width - 10, 20);
}
void moveDownUFOs() {
for (Iterator iterator = ufos.iterator(); iterator.hasNext();) {
UFO s = (UFO) iterator.next();
s.ufoY += 7 * roundNum;
if (s.ufoY > 550)
screenNum = 4;
}
}
void setUpUFOs(int level) {
int rows = 0;
for (int y = 10; y < height - 200; y += 25) {
for (int x = 100; x < width - 100; x += 35) {
ufos.add(new UFO(x, y));
}
rows++;
if (rows == level + 3)
return;
}
}
void keyPressed() {
if (keyCode == RIGHT) rightPressed = true;
if (keyCode == LEFT) leftPressed = true;
if (keyCode == UP) upPressed = true;
if (keyCode == DOWN) downPressed = true;
if (keyCode == ' ' && canShoot) {
bullets.add(new Bullet(x, y - 20));
canShoot = false;
}
if (keyCode == SHIFT) sprint = true;
}
void keyReleased() {
if (keyCode == RIGHT) rightPressed = false;
if (keyCode == LEFT) leftPressed = false;
if (keyCode == UP) upPressed = false;
if (keyCode == DOWN) downPressed = false;
if (keyCode == ENTER) enterPressed = false;
if (keyCode == ' ' && !canShoot) canShoot = true;
if (keyCode == SHIFT) sprint = false;
}
UFO
// You are allowed to edit this code
public class UFO {
int ufoX, ufoY;
UFO(int x, int y) {
ufoX = x;
ufoY = y;
}
boolean show() {
if (moveLeft) {
if (ufoX < 20) {
moveLeft = false;
moveDownUFOs();
} else
ufoX -= ufoSpeed;
} else {
if (ufoX > width - 20) {
moveLeft = true;
moveDownUFOs();
}
else
ufoX += ufoSpeed;
}
for (Iterator iterator = bullets.iterator(); iterator.hasNext();) {
Bullet s = (Bullet) iterator.next();
if (dist(s.x, s.y - 20, ufoX, ufoY) < 18) {
iterator.remove();
return true;
}
}
if (dist(ufoX, ufoY, x, y) < 20) {
screenNum = 3;
}
image(ufo, ufoX, ufoY);
return false;
}
}
Bullet
// You are allowed to edit this code
public class Bullet {
int x, y;
Bullet(int x, int y) {
this.x = x;
this.y = y;
}
void show() {
y -= 15;
strokeWeight(6);
stroke(0,255,0);
line(x, y, x, y - 20);
}
}