Skip to content

Commit 3458ab8

Browse files
authored
Version 1.9.12 release
1 parent 3c94b3d commit 3458ab8

5 files changed

Lines changed: 205 additions & 15 deletions

File tree

Examples/demos/Snake Game

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Snake example #
2+
# This example uses the inbuild variable "keyCode", which detects a key input. #
3+
# IMPORTANT!!!! This variable only works in the GUI editor not, if executed in the console! #
4+
5+
6+
grid = [];
7+
size = 18;
8+
9+
foodX = 4;
10+
foodY = 4;
11+
foodSymbol = ",";
12+
13+
snake = [];
14+
direction = 0; # 0 up, 1 down, 2 left, 3 right #
15+
16+
score = 0;
17+
gameOver = $false;
18+
19+
# make a 10 by 10 grid #
20+
for i $size {
21+
rows = [];
22+
for j $size {
23+
push " " $rows;
24+
};
25+
push $rows $grid;
26+
};
27+
28+
# Expects an x and y #
29+
addSnakeElement = {
30+
push (new-object {
31+
x = $0;
32+
y = $1;
33+
prevX = 0;
34+
prevY = 0;
35+
}) $snake;
36+
};
37+
38+
updateSnake = {
39+
oldX = $snake[0].x;
40+
oldY = $snake[0].y;
41+
42+
for i (length $snake) {
43+
44+
snake[$i].prevX = $snake[$i].x;
45+
snake[$i].prevY = $snake[$i].y;
46+
47+
if ($i == 0) { #only the head is allowed to change direction #
48+
if ($direction == 0) {
49+
snake[$i].y = ($snake[$i].y - 1);
50+
} elseif ($direction == 1) {
51+
snake[$i].y = ($snake[$i].y + 1);
52+
} elseif ($direction == 2) {
53+
snake[$i].x = ($snake[$i].x - 1);
54+
} elseif ($direction == 3) {
55+
snake[$i].x = ($snake[$i].x + 1);
56+
};
57+
} else {
58+
prevI = ($i - 1);
59+
snake[$i].x = $snake[$prevI].prevX;
60+
snake[$i].y = $snake[$prevI].prevY;
61+
};
62+
63+
x = $snake[$i].x;
64+
y = $snake[$i].y;
65+
66+
if (($x lt 0) or ($x gteq $size) or ($y lt 0) or ($y gteq $size))) {
67+
gameOver = $true;
68+
return;
69+
};
70+
println $grid[$x][$y];
71+
if ($grid[$x][$y] == "O") {
72+
gameOver = $true;
73+
return;
74+
};
75+
76+
grid[$x][$y] = "O";
77+
78+
if (($x == $foodX) and ($y == $foodY)) {
79+
score = ($score + 100);
80+
call $spawnFood;
81+
call $addSnakeElement $oldX $oldY;
82+
};
83+
};
84+
};
85+
86+
87+
spawnFood = {
88+
foodX = (int ((random) * $size));
89+
foodY = (int ((random) * $size));
90+
grid[$foodX][$foodY] = $foodSymbol;
91+
};
92+
93+
drawGrid = {
94+
95+
try {
96+
exec "cls";
97+
clearConsole;
98+
};
99+
100+
101+
if $gameOver {
102+
println "GAME OVER!\nPress any key to start a new game!";
103+
};
104+
105+
println "Score " $score "\nUse wasd to change the snake direction";
106+
107+
for i $size {
108+
print "-";
109+
};
110+
println;
111+
for i $size {
112+
print "|";
113+
for j $size {
114+
print $grid[$i][$j];
115+
if ($grid[$i][$j] != $foodSymbol) {
116+
grid[$i][$j] = " ";
117+
};
118+
};
119+
print "|";
120+
println;
121+
};
122+
for i $size {
123+
print "-";
124+
};
125+
println;
126+
127+
};
128+
129+
call $addSnakeElement 5 5;
130+
call $spawnFood;
131+
132+
thread key-detect {
133+
loop {
134+
if($gameOver and ($keyCode != "")) {
135+
gameOver = $false;
136+
snake = [];
137+
call $addSnakeElement 5 5;
138+
call $spawnFood;
139+
score = 0;
140+
};
141+
142+
if ($keyCode == w) {
143+
direction = 2;
144+
} elseif ($keyCode == s) {
145+
direction = 3;
146+
} elseif ($keyCode == a) {
147+
direction = 0;
148+
} elseif ($keyCode == d) {
149+
direction = 1;
150+
};
151+
};
152+
};
153+
154+
loop {
155+
if (not $gameOver) {
156+
wait 80;
157+
call $updateSnake;
158+
call $drawGrid;
159+
}
160+
};

Examples/demos/raycast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# The view will rotate automatically, but you could enable controls if you want. You can also add more walls using the variables bofore the MAIN loop #
77
# If manual control is enabled, you can move and look around using the W A S D keys #
88

9-
MANUAL_CONTROL = $false;
9+
MANUAL_CONTROL = $true;
1010

1111

1212
sizeX = 100;

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ NOTICE:
1515
- Fixed a profanity in an error message
1616
- Division by zero now throws a readable error message (It previously resultet in "Infinity" which was not recognized as a number before, causing confusing errors
1717
- Added an ascii raycast example to show off the script capabilities (Take a look! Examples -> demos -> raycast)
18+
- Added a snake game as example
1819
- Fixed an issue where 0.000 == 0 would return false
20+
- Added a "keyPressed" event function to the devscript editor library so you can create simple games.
21+
- Improved performance
1922
- More minor fixes
2023

2124
1.9.11:

src/com/devkev/devscript/raw/Process.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Process {
2929

3030
Block main;
3131
//Main block is not never in the list, since the process gets terminated, if main is killed.
32-
private final ArrayList<Block> aliveBlocks = new ArrayList<Block>(1);
32+
//private final ArrayList<Block> aliveBlocks = new ArrayList<Block>(1);
3333
boolean breakRequested = false;
3434

3535
public long maxRuntime = 0; //Runtime in ms. If < 0, allowed runtime is infinite
@@ -157,8 +157,7 @@ public boolean isCaseSensitive() {
157157

158158
private void start(Block block) {
159159
if(block == null) return;
160-
161-
aliveBlocks.add(block);
160+
//aliveBlocks.add(block);
162161
StringBuilder command = block.blockCode;
163162

164163
block.exitCode = ExitCodes.DONE;
@@ -181,7 +180,7 @@ private void start(Block block) {
181180
}
182181

183182
block.alive = false;
184-
aliveBlocks.remove(block);
183+
//aliveBlocks.remove(block);
185184
}
186185

187186
/**@return false, if the block: <br>
@@ -666,18 +665,18 @@ public synchronized void kill(Block block, String errorMessage) {
666665

667666
finalizeExit(1, errorMessage);
668667

669-
for(int i = 0; i < aliveBlocks.size(); i++) {
668+
/*for(int i = 0; i < aliveBlocks.size(); i++) {
670669
aliveBlocks.get(i).cached.clear();
671670
aliveBlocks.get(i).alive = false;
672671
aliveBlocks.get(i).interrupted = true;
673-
}
672+
}*/
674673
block.alive = false;
675674
block.interrupted = true;
676675
block.currentCommand = "";
677676
}
678677

679678
block.exitCode = ExitCodes.ERROR;
680-
aliveBlocks.clear();
679+
//aliveBlocks.clear();
681680
garbageCollection(main);
682681
}
683682

src/com/devkev/gui/Window.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ public void done(int exitCode) {
175175
});
176176

177177
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
178+
179+
178180
@Override
179181
public boolean dispatchKeyEvent(KeyEvent e) {
180-
181182
if(e.getKeyCode() == KeyEvent.VK_ENTER && input.inputRequested() && waitForEnter && e.getID() == KeyEvent.KEY_RELEASED) {
182183
input.flush(console.getText().substring(inputStart, console.getCaretPosition()) + "\n");
183184
waitForEnter = false;
@@ -238,12 +239,15 @@ public void actionPerformed(ActionEvent e) {
238239

239240

240241
//Examples and tutorials //
241-
242-
JMenu examples = new JMenu("Examples");
243-
walkDemoPath("/Examples", examples);
244-
245-
m.add(examples);
246-
bar.add(m);
242+
try {
243+
JMenu examples = new JMenu("Examples");
244+
walkDemoPath("/Examples", examples);
245+
246+
m.add(examples);
247+
bar.add(m);
248+
} catch(NullPointerException e) {
249+
e.printStackTrace();
250+
}
247251

248252
JMenu m2 = new JMenu("Edit");
249253
JMenuItem undo = new JMenuItem(getFormattedBarText("Undo"));
@@ -303,6 +307,7 @@ public void actionPerformed(ActionEvent e) {
303307
window.setEnabled(false);
304308
p.setCaseSensitive(false);
305309
p.execute(textArea.getText(), true);
310+
p.setVariable("keyCode", "", false, true);
306311
}
307312
});
308313
m3.add(run);
@@ -379,6 +384,7 @@ public void keyPressed(KeyEvent e) {}
379384
initRunWindow();
380385
window.pack();
381386
window.setSize(500, 500);
387+
382388
}
383389

384390
private boolean isPath(String path) {
@@ -590,6 +596,28 @@ public void keyPressed(KeyEvent e) {
590596

591597
runWindow.pack();
592598
runWindow.setBounds((int) (Toolkit.getDefaultToolkit().getScreenSize().width *.5f-250), (int) (Toolkit.getDefaultToolkit().getScreenSize().height *.5f-100), 500, 200);
599+
600+
runWindow.addKeyListener(new KeyListener() {
601+
602+
@Override
603+
public void keyTyped(KeyEvent e) {
604+
}
605+
606+
@Override
607+
public void keyReleased(KeyEvent e) {
608+
if(p.isRunning()) {
609+
p.setVariable("keyCode", "", false, false);
610+
}
611+
}
612+
613+
@Override
614+
public void keyPressed(KeyEvent e) {
615+
if(p.isRunning()) {
616+
//Set the variable "keyPressed" to the char that is pressed
617+
p.setVariable("keyCode", e.getKeyChar(), false, false);
618+
}
619+
}
620+
});
593621
}
594622

595623
private String getFormattedBarText(String text) {

0 commit comments

Comments
 (0)