package gameMyself;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
public class ControllerMyself {
@FXML
private Canvas canvas;
private GraphicsContext graphicsContext;
private boolean isLeftPressed;
private boolean isRightPressed;
private boolean isBankPressed;
private boolean isEnemyVisible;
private boolean isBulletVisible;
private double xEnemy;
private double yEnemy;
private double xPlane;
private double yplane;
private double xBullet;
private double yBullet;
private boolean isUpPressed;
private boolean isDownPressed;
private int score;
public ControllerMyself() {
}
@FXML
private void initialize() {
graphicsContext = canvas.getGraphicsContext2D();
isBulletVisible = false;
isEnemyVisible = true;
isLeftPressed = false;
isRightPressed = false;
isBankPressed = false;
isUpPressed=false;
isDownPressed=false;
canvas.setWidth(800);
canvas.setHeight(650);
xEnemy = (Math.random() * (canvas.getWidth() - 50)) ;
yEnemy = (Math.random()*(canvas.getHeight()-350));
yplane = 600;
xPlane=350;
xBullet = xPlane;
yBullet = yplane ;
score = 0;
canvas.setFocusTraversable(true); // 设置画布可以接收焦点
canvas.setOnKeyPressed(this::onKeyPressed); // 注册键盘按下事件处理程序
canvas.setOnKeyReleased(this::onKeyReleased); // 注册键盘释放事件处理程序
new AnimationTimer() { // 创建一个动画计时器
@Override
public void handle(long now) { // 每帧动画的处理逻辑
update(); // 更新游戏状态
draw(); // 绘制游戏画面
}
}.start(); // 启动动画计时器
}
private void update(){
if(isLeftPressed&&xPlane>0){
xPlane=xPlane-5.5;
}
if(isRightPressed&&xPlane<canvas.getWidth()-50){
xPlane+=5.5;
}
if(isUpPressed&&yplane>0){
yplane-=5.5;
}
if (isDownPressed&&yplane+50<canvas.getHeight()){
yplane+=6;
}
if (isBankPressed&&!isBulletVisible){
xBullet=xPlane+25;
yBullet=yplane;
isBulletVisible =true;
}
if (isBulletVisible){
yBullet-=65;
if(yBullet<0){
isBulletVisible =false;
}
}
if (isEnemyVisible){
yEnemy+=4;
if (yEnemy>canvas.getHeight()){
// isEnemyVisible=false;
xEnemy=(int)(Math.random()*(canvas.getWidth()-50)) ;
yEnemy=Math.random()*(canvas.getHeight()-500);
}
if(isBulletVisible &&yBullet<yEnemy+50&&yBullet+30>yEnemy&&xBullet<xEnemy+50&&xBullet+5>xEnemy){
isBulletVisible =false;
//isEnemyVisible=false;
score++;
xEnemy=(int)( Math.random()*(canvas.getWidth()-50));
yEnemy=Math.random()*(canvas.getHeight()-350);
}
if(yplane<yEnemy+50&&xPlane<xEnemy+50&&xPlane+50>xEnemy&&yplane+50>yEnemy){
// isEnemyVisible=false;
gameOver();
}
}
}
private void draw(){
graphicsContext.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
graphicsContext.setFill(Color.BLUE);
graphicsContext.fillRect(xPlane,yplane,50,50);
if (!isEnemyVisible){
graphicsContext.setFill(Color.WHITE);
graphicsContext.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
graphicsContext.setFill(Color.BLACK);
graphicsContext.setFont(Font.font("Arial", 40));
graphicsContext.fillText("GAMEOVER!! your score:"+score, canvas.getWidth()/2-240, canvas.getHeight()/2);
return;
}
if (isEnemyVisible){
graphicsContext.setFill(Color.RED);
graphicsContext.fillRect(xEnemy,yEnemy,50,50);
}
if(isBulletVisible){
graphicsContext.setFill(Color.BLACK);
graphicsContext.fillRect(xBullet,yBullet,5,30);
}
graphicsContext.fillText("Score "+score,20,35);
graphicsContext.setFont(Font.font(30));
}
@FXML
void onKeyPressed(KeyEvent event) {
if (event.getCode() == KeyCode.LEFT)
{
isLeftPressed=true;
}
if (event.getCode()==KeyCode.UP){
isUpPressed=true;
}
if (event.getCode()==KeyCode.DOWN){
isDownPressed=true;
}
if (event.getCode()==KeyCode.RIGHT){
isRightPressed=true;
}
if (event.getCode()==KeyCode.SPACE){
isBankPressed=true;
}
}
@FXML
void onKeyReleased(KeyEvent event) {
if (event.getCode()==KeyCode.LEFT){
isLeftPressed=false;
}
if (event.getCode()==KeyCode.UP){
isUpPressed=false;
}
if (event.getCode()==KeyCode.DOWN){
isDownPressed=false;
}
if(event.getCode()==KeyCode.RIGHT){
isRightPressed=false;
}
if(event.getCode()==KeyCode.SPACE){
isBankPressed=false;
}
}
private void gameOver(){
isEnemyVisible =false;
}
}