JScratch
Loading...
Searching...
No Matches
Sprite1.java
Go to the documentation of this file.
1package generated;
2
3import com.jscratch.*;
4import java.awt.Color;
5import java.awt.event.KeyEvent;
6
7/*
8 *
9 *
10 *
11 * THE PLAYER
12 *
13 *
14 *
15 */
16public class Sprite1 extends BSprite {
17 public int mv = 5;
18 public double turnSpeed = 0.25; //in seconds, time it takes to turn (smaller = faster)
19 /* slot start [id:"2_members" tags:"inClassNotMethod"] */
20 public Sprite1() {
21 super("Sprite1");
22
23 // Example: addCostume(new Costume("costume1", "path/to/img.png"));
24
25 /* slot start [id:"2_init" tags:"inInstanceMethod"] */
26 /*
27 *
28 *
29 *
30 * Add costumes (not in scratch by the way)
31 *
32 *
33 */
34
35
37
38 "costume1",
39 "jscratch_ws/assets/"+
40 "Player_costume1.png",
41 28, //change this to be half your sprite width, this is for rotation
42 33 //change this to be half your sprite height, this is for rotation
43 ));
44
46
47 "costume2",
48 "jscratch_ws/assets/"+
49 "Player_costume2.png",
50 29, //change this to be half your sprite width, this is for rotation
51 33 //change this to be half your sprite height, this is for rotation
52 ));
53 /*
54 *
55 * Go to center of screen
56 *
57 */
58
59
60 goTo(0,0);
61
62 this.startScript(() -> {
63
64 /*
65 *
66 * the game loop
67 *
68 */
69 int ws = 125; //walk speed (animation not movement speed) smaller = faster
70 while (true) {
71 if (
73 ) {
74 /* slot start [id:"3_key_body" tags:"inInstanceMethod"] */
75
76 this.startScript(() -> this.animateTurn(0, turnSpeed, "easeIn"));
77
78 movec(mv, 0);
80 this.goToFront();
81 try {
82 Thread.sleep(ws);
83 } catch (InterruptedException e) {
84 e.printStackTrace();
85 }
86 /* slot end [id:"3_key_body"] */
87 }
88
89 if (
91 ) {
92 /* slot start [id:"3_key_body" tags:"inInstanceMethod"] */
93 this.startScript(() -> this.animateTurn(270, turnSpeed, "easeIn"));
94 movec(mv, 3);
96 this.goToFront();
97 try {
98 Thread.sleep(ws);
99 } catch (InterruptedException e) {
100 e.printStackTrace();
101 }
102 /* slot end [id:"3_key_body"] */
103 }
104 if (
106 ) {
107 /* slot start [id:"3_key_body" tags:"inInstanceMethod"] */
108 this.startScript(() -> this.animateTurn(180, turnSpeed, "easeIn"));
109 movec(mv, 2);
110 nextCostume();
111 this.goToFront();
112 try {
113 Thread.sleep(ws);
114 } catch (InterruptedException e) {
115 e.printStackTrace();
116 }
117 /* slot end [id:"3_key_body"] */
118 }
119 if (
121 ) {
122 /* slot start [id:"3_key_body" tags:"inInstanceMethod"] */
123 this.startScript(() -> this.animateTurn(90, turnSpeed, "easeIn"));
124 movec(mv, 1);
125 nextCostume();
126 this.goToFront();
127 try {
128 Thread.sleep(ws);
129 } catch (InterruptedException e) {
130 e.printStackTrace();
131 }
132
133
134
135 /* slot end [id:"3_key_body"] */
136 }
138 }
139 });
140
141
142
143
144
145
146
147
148
149
150
151
152 /* slot end [id:"2_init"] */
153 }
154
155 public void movec(int amt, int dir) {
156 switch (dir) {
157 case 0:
158 yy += amt;
159 break;
160 case 1:
161 xx += amt;
162 break;
163 case 2:
164 yy -= amt;
165 break;
166 case 3:
167 xx -= amt;
168 break;
169 default:
170 }
171
173 s.x = -xx;
174 s.y = -yy;
175 s.update();
176 super.update();
177 }
178 /* slot end [id:"2_members"] */
179}
void startScript(Runnable script)
void addCostume(Costume c)
Definition Sprite.java:240
static Stage getInstance()
Definition Stage.java:337
boolean isKeyPressed(String keyName)
Definition Stage.java:117
void animateTurn(int dir, double time)
Definition BSprite.java:80
void goTo(int x, int y)
Definition BSprite.java:47
BSprite(String name)
Definition BSprite.java:17
static Scroller2d getInstance()
void movec(int amt, int dir)
Definition Sprite1.java:155