View difference between Paste ID: Kz8J3Q6t and Vd1T7FJH
SHOW: | | - or go back to the newest paste.
1
package spritetest;
2
 
3
import java.util.ArrayList;
4
import java.util.Random;
5
import javafx.animation.AnimationTimer;
6
import javafx.application.Application;
7
import javafx.geometry.Rectangle2D;
8
import javafx.scene.Group;
9
import javafx.scene.Scene;
10
import javafx.scene.canvas.Canvas;
11
import javafx.scene.canvas.GraphicsContext;
12
import javafx.scene.image.Image;
13
import javafx.stage.Stage;
14
 
15
/**
16
 *
17
 * @author Compsci
18
 */
19
public class SpriteTest extends Application {
20
   private boolean down = true;
21
   private boolean up = true;
22
   private boolean left = true;
23
   private boolean right = true;
24
    @Override
25
    public void start(Stage primaryStage) {
26
        Random random = new Random();
27
        Sprite briefcase = new Sprite();
28
        briefcase.setImage("file:/C:/Users/Compsci/Pictures/briefcase_sm.png");
29
        Group root = new Group();
30
        Scene theScene = new Scene(root);
31
        primaryStage.setScene(theScene);
32-
        ArrayList<Sprite> imageList = new ArrayList<Sprite>();
32+
33
        Canvas canvas = new Canvas(512, 512);
34
        GraphicsContext gc = canvas.getGraphicsContext2D();
35
        briefcase.drawSprite(gc);
36
        ArrayList<Sprite> imageList = new ArrayList<>();
37
        Sprite moneybag;
38
        for (int i = 0; i < 10; i++){
39
            moneybag = new Sprite();
40
            moneybag.setImage("file:/C:/Users/Compsci/Pictures/moneybag_sm.png");
41
            moneybag.setPosition(random.nextInt(480), random.nextInt(480));
42
            imageList.add(moneybag);
43-
        theScene.setOnKeyPressed(e->{
43+
44-
            if (e.getCode().equals(e.getCode().DOWN)){
44+
45-
                gc.clearRect(0,0,512, 512);
45+
46-
                for (int i = 0; i < 10; i++){
46+
47-
                    imageList.get(i).drawSprite(gc);
47+
        new AnimationTimer()
48
        {
49-
                briefcase.addVelocity(0, 10);
49+
            public void handle(long currentNanoTime)
50-
                briefcase.update(1);
50+
            {
51
                gc.clearRect(0, 0, 512, 512);
52-
                briefcase.setVelocity(0, 0);
52+
                theScene.setOnKeyPressed(e->{
53
                    switch (e.getCode()){
54-
        });
54+
                        case DOWN:
55-
       
55+
                            if(down)
56
                                briefcase.addVelocity(0, 200); down = false; break;
57
                        case UP:
58
                            if(up)
59
                                briefcase.addVelocity(0, -200); up = false; break;
60
                        case LEFT:
61
                            if(left)
62
                                briefcase.addVelocity(-200, 0); left = false; break;
63
                        case RIGHT:
64
                            if(right)
65
                                briefcase.addVelocity(200, 0); right = false; break;
66
                    }
67
                });
68
                theScene.setOnKeyReleased(e-> {
69
                    switch (e.getCode()){
70
                        case DOWN:
71
                            briefcase.addVelocity(0, -200); down = true; break;
72
                        case UP:
73
                            briefcase.addVelocity(0, 200); up = true; break;
74
                        case LEFT:
75
                            briefcase.addVelocity(200, 0); left = true; break;
76
                        case RIGHT:
77
                            briefcase.addVelocity(-200, 0); right = true; break;
78
                    }
79
                });
80
81
                for (Sprite moneybag: imageList)
82
                    moneybag.drawSprite(gc);
83
84
                briefcase.update(0.01);
85
                for (int i = 0; i <  imageList.size(); i++){
86
                    if (briefcase.intersect(imageList.get(i)))
87
                        imageList.remove(i);
88
                }
89
                briefcase.drawSprite(gc);
90
                
91
            }
92
        }.start();  
93
    }
94
    
95
96
    
97
98
    
99
 
100
    /**
101
     * @param args the command line arguments
102
     */
103
    public static void main(String[] args) {
104
        launch(args);
105-
        
105+
106
   
107
    class Sprite {
108
        private double velocityX;
109
        private double velocityY;
110
        private double positionX;
111
        private double positionY;
112
        private double width;
113
        private double height;
114
        private Image img;
115
 
116
        private void setImage(String path){
117
            img = new Image(path);
118
            width = img.getWidth();
119
            height = img.getHeight();
120
        }
121
        private void setPosition(double x, double y){
122
            positionX = x;
123
            positionY = y;
124
        }
125
        private void setVelocity(double x, double y){
126
            velocityX = x;
127
            velocityY = y;
128
        }
129
        private void addVelocity(double x, double y){
130
            velocityX += x;
131
            velocityY += y;
132
        }
133
        private void update(double time){
134
            positionX += velocityX * time;
135
            positionY += velocityY * time;
136
        }
137
        private Rectangle2D getBoundry(){
138
            return new Rectangle2D(positionX,positionY,width,height);
139
        }
140
        private boolean intersect(Sprite s){
141
            return s.getBoundry().intersects(this.getBoundry());
142
        }
143
        private void drawSprite(GraphicsContext gc){
144
            gc.drawImage(img,positionX,positionY);
145
        }
146
    }
147
}