java - How can I create an ArrayList and automatically assign variables to the drawn Galaga ship? Also to get it to move with arrow keys? -
my current code listed @ bottom. far have gotten draw galaga ship want in final edition, stars appear @ random. now, problem, or thing need figure out, how create array list, example:
arraylist = new arraylist()
where new arraylist has sets of variables assigned it, in regards g.drawline method. assigning arraylist variables (x1,y1,x2,y2), , basic thought given lines assigned array. also, create move, utilize if() loop, if("up" pressed), y1+=5; , y2+=5; it'll move up, other else if loops "right", "left", , "down" arows used. help appreciated, give thanks you.
import java.awt.*; import java.applet.*; public class galaga extends applet { public void paint(graphics g) { drawbackground(g); drawgalaga(g); } public void drawbackground(graphics g) { g.setcolor(color.black); g.fillrect(1,2,3000,4000); drawstars(g); } public void drawgalaga(graphics g) { g.setcolor(color.red); g.drawline(42,75,42,80); //far left gun g.drawline(43,75,43,80); g.drawline(44,75,44,80); g.drawline(45,75,45,80); g.setcolor(color.white); g.drawline(42,80,42,100); //far left piece g.drawline(43,80,43,100); g.drawline(44,80,44,100); g.drawline(45,80,45,100); g.drawline(46,83,46,96); //from left, 1st 1 g.drawline(47,83,47,96); g.drawline(48,83,48,96); g.drawline(49,83,49,96); g.drawline(50,79,50,92); //from left, 2nd 1 g.drawline(51,79,51,92); g.drawline(52,79,52,92); g.drawline(53,79,53,92); g.drawline(54,65,54,89); //from left, 3rd 1 g.drawline(55,65,55,89); g.drawline(56,65,56,89); g.drawline(57,65,57,89); g.drawline(58,75,58,89); //from middle 3rd 1 g.drawline(59,75,59,89); g.drawline(60,75,60,89); g.drawline(61,75,61,89); g.drawline(62,70,62,85); //from middle, 2nd left 1 g.drawline(63,70,63,85); g.drawline(64,70,64,85); g.drawline(65,70,65,85); g.drawline(66,60,66,95); //from middle, 1st left 1 g.drawline(67,60,67,95); g.drawline(68,60,68,95); g.drawline(69,60,69,95); g.drawline(70,45,70,100); //big middle piece g.drawline(71,45,71,100); g.drawline(72,45,72,100); g.drawline(73,45,73,100); g.drawline(74,45,74,100); g.drawline(75,45,75,100); g.drawline(76,45,76,100); g.drawline(77,60,77,95); //from middle, 1st right 1 g.drawline(78,60,78,95); g.drawline(79,60,79,95); g.drawline(80,60,80,95); g.drawline(81,70,81,85); //from middle, 2nd right 1 g.drawline(82,70,82,85); g.drawline(83,70,83,85); g.drawline(84,70,84,85); g.drawline(85,75,85,89); //from middle 3rd 1 g.drawline(86,75,86,89); g.drawline(87,75,87,89); g.drawline(88,75,88,89); g.drawline(88,65,88,89); //from right, 3rd 1 g.drawline(89,65,89,89); g.drawline(90,65,90,89); g.drawline(91,65,91,89); g.drawline(92,79,92,92); //from right, 2nd 1 g.drawline(93,79,93,92); g.drawline(94,79,94,92); g.drawline(95,79,95,92); g.drawline(96,83,96,96); //from right, 1st 1 g.drawline(97,83,97,96); g.drawline(98,83,98,96); g.drawline(99,83,99,96); g.drawline(100,80,100,100); //far right piece g.drawline(101,80,101,100); g.drawline(102,80,102,100); g.drawline(103,80,103,100); g.setcolor(color.red); g.drawline(100,75,100,80); //far right gun g.drawline(101,75,101,80); g.drawline(102,75,102,80); g.drawline(103,75,103,80); } public void drawstars(graphics g) { g.setcolor(color.yellow); int x, y; (int s = 0; s < 2000; s++) { x = (int)(math.random()*2000); y = (int)(math.random()*2000); g.drawline(x, y, x, y); } } public void delay(int n) { long startdelay = system.currenttimemillis(); long enddelay = 0; while (enddelay - startdelay < n) enddelay = system.currenttimemillis(); } }
this code doesn't honest.
first, utilize objects instead of implicit roles of array offsets.
list<line> lines = new arraylist<line>();
now each array element valid line. should want refactor code more, couple line color etc.
coming question: don't re-create array, store ship's points read-only , manipulate x,y offsets.
public void drawgalaga(graphics g) { g.setcolor(color.red); g.drawline(x0+42,y0+75,x0+42,y0+80); ... }
where x0
, y0
offsets point (0,0)
. write event handler:
void moveup(int dx, int dy) { x0 += dx; y0 += dy; }
watch out multi-threaded access x0
, y0
.
java graphics awt
No comments:
Post a Comment