0% found this document useful (0 votes)
453 views

Marbles Java Program

1. The document defines a Java program that simulates marbles rolling around a game board and interacting with walls, a shooter, and a goal. 2. It creates classes for marbles, walls, a shooter, a goal, and an item that can split marbles, and defines their behaviors and interactions. 3. Key presses control the shooter to launch marbles, and the program animates the motion and collisions of marbles over time using a timer.

Uploaded by

Patrick Thompson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
453 views

Marbles Java Program

1. The document defines a Java program that simulates marbles rolling around a game board and interacting with walls, a shooter, and a goal. 2. It creates classes for marbles, walls, a shooter, a goal, and an item that can split marbles, and defines their behaviors and interactions. 3. Key presses control the shooter to launch marbles, and the program animates the motion and collisions of marbles over time using a timer.

Uploaded by

Patrick Thompson
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//WRITTEN BY GUS KRISTIANSEN import import import import java.awt.*; java.awt.event.*; java.util.ArrayList; javax.swing.

*;

public class Marbles extends JFrame implements ActionListener { int w, h, ga1; Timer timer; public ArrayList marbles, walls; public Shooter shooter, goal; public Item boom = new Item("boom", 650, 100); public Marbles() { walls=new ArrayList(); marbles=new ArrayList(); w=700; h=700; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addKeyListener(new keyf()); setSize(w, h); setVisible(true); setResizable(false); walls.add(new Wall(0, 200, 450, 50)); shooter=new Shooter(100, 100, 0, Color.CYAN); goal = new Shooter(500, 500, Color.PINK); //marbles.add(new Marble(300, 50, 5, 5, 25, 25)); //marbles.add(new Marble(300, 50, -3, 2, 25, 25)); timer=new Timer(10, this); timer.start(); } public static void main(String args[]){ new Marbles(); } public void paint(Graphics g){ g.setColor(Color.WHITE); g.fillRect(0,0,w,h); g.setColor(shooter.color); g.fillOval(shooter.x, shooter.y, shooter.w, shooter.h); g.setColor(Color.BLACK); g.drawLine(shooter.x+shooter.w/2, shooter.y+shooter.h/2, (int)(shooter.x +shooter.w/2+Math.cos(shooter.rads)*shooter.power), (int)(shooter.y+shooter.h/2+ Math.sin(shooter.rads)*shooter.power)); g.setColor(goal.color); g.fillOval(goal.x, goal.y, goal.w, goal.h); g.setColor(Color.MAGENTA); if(boom.active){g.fillOval(boom.x, boom.y, 25, 25);} if(!marbles.isEmpty()){

for(int j = 0; j < marbles.size(); j++){ Marble m = (Marble)marbles.get(j); g.setColor(m.color); g.fillOval((int)m.x, (int)m.y, m.w, m.h); } } g.setColor(Color.BLACK); if(!walls.isEmpty()){ for(int j = 0; j < walls.size(); j++){ Wall w = (Wall)walls.get(j); g.fillRect(w.x, w.y, w.w, w.h); } } g.drawString(""+ga1, 100, 500); } public void actionPerformed(ActionEvent e){ step(); repaint(); } public void step(){ if(!marbles.isEmpty()){ for(int j = 0; j < marbles.size(); j++){ Marble m = (Marble)marbles.get(j); m.move(); } } checkCol(); } public void checkCol(){ if(!marbles.isEmpty()){ for(int j = 0; j < marbles.size(); j++){ Marble m = (Marble)marbles.get(j); for(int i = 0; i < marbles.size(); i++){ Marble n = (Marble)marbles.get(i); if(j!=i&&m.getRect().intersects(n.getRect())){ //marble2marble collision if((int)m.vx==(int)n.vx&&(int)m.vy==(int)n.vy&&m.getRect ().intersects(n.getRect())){ marbles.remove(j); marbles.remove(i-1); return; } int c = 0; while(m.getRect().intersects(n.getRect())){ m.unmove(.01, 1); n.unmove(.01, 1); c++; ga1=c; } double ang = Math.atan2(m.y-n.y, m.x-n.x); double pang = Math.atan2(m.vy, m.vx); double qang = Math.atan2(n.vy, n.vx);

double double double double double double double double m.vx = +Math.PI/2)*fpys); Math.PI/2)*fpys);

pxs = m.getSpeed()*Math.cos(pang-ang); pys = m.getSpeed()*Math.sin(pang-ang); qxs = n.getSpeed()*Math.cos(qang-ang); qys = n.getSpeed()*Math.sin(qang-ang); fpxs = qxs; fqxs = pxs; fpys = pys; fqys = qys; (int) (Math.cos(ang)*fpxs+Math.cos(ang

m.vy = (int)(Math.sin(ang)*fpxs+Math.sin(ang+ n.vx = (int)(Math.cos(ang)*fqxs+Math.cos(ang+ Math.PI/2)*fqys); n.vy = (int)(Math.sin(ang)*fqxs+Math.sin(ang+ Math.PI/2)*fqys); //m.x+=c*.01*m.vx; //m.y+=c*.01*m.vy; m.move(.01, c); n.move(.01, c); //n.x+=c*.01*n.vx; //n.y+=c*.01*n.vy; } } for(int i = 0; i < walls.size(); i++){ Wall w = (Wall)walls.get(i); if(m.getRect().intersects(w.getRect())){ double double double double vert_ptop=Math.abs(m.x-(w.x+w.w)); vert_pbot=Math.abs((m.x+m.w)-w.x); hor_pright=Math.abs((m.y+m.h)-w.y); hor_pleft=Math.abs(m.y-(w.y+w.h));

double low=vert_ptop; if(vert_pbot<low){low=vert_pbot;} if(hor_pleft<low){low=hor_pleft;} if(hor_pright<low){low=hor_pright;} if(low==vert_pbot){ m.vx*=-1; m.x=w.x-m.w; }else if(low==vert_ptop){ m.vx*=-1; m.x=w.y+w.w; }else if(low==hor_pright){ m.vy*=-1; m.vy-=((w.y-m.h)-m.y)*.0001; m.y=w.y-m.h; }else if(low==hor_pleft){ m.vy*=-1; m.vy-=((w.y+w.h)-m.y)*.0001; m.y=w.y+w.h; } }

} if(m.getRect().intersects(goal.getRect())){ goal.color=Color.GREEN; } if(m.getRect().intersects(boom.getRect())&&boom.active){ boom.active=false; for(double z = 0; z < 2*Math.PI; z+=Math.PI/4){ marbles.add(new Marble(boom.x+Math.cos(z)*25, boom.y+Math.si n(z)*25, Math.cos(z)*m.getSpeed(), Math.sin(z)*m.getSpeed(), 25, 25)); } } }

} }//collision

private class keyf extends KeyAdapter { public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP) { shooter.power++; } if (key == KeyEvent.VK_DOWN) { shooter.power--; } if (key == KeyEvent.VK_LEFT) { shooter.rads+=Math.PI/10; } if (key == KeyEvent.VK_RIGHT) { shooter.rads-=Math.PI/20; } if (key == KeyEvent.VK_SPACE) { shooter.shoot(marbles); } if (key == KeyEvent.VK_P) { //step(); boom.active=true; } }

} public class Level{ public ArrayList marbles, walls; public Shooter shooter; public Shooter goal; public Level(Shooter shooter, Shooter goal, Wall w){ this.shooter=shooter; this.goal=goal; walls.add(w); }

} public class Marble{ public int w, h; public double x, y, vx, vy; public Color color; public Marble(double x, double y, double vx, double vy, int w, int h){ this.x=x; this.y=y; this.w=w; this.h=h; this.vx=vx; this.vy=vy; int c = (int)(Math.random()*4); switch(c){ case 0: color=Color.RED;break; case 1: color=Color.BLUE;break; case 2: color=Color.GREEN;break; case 3: color=Color.MAGENTA;break; } } public Marble(double x, double y, double vx, double vy, int w, int h, Co lor color){ this.x=x; this.y=y; this.vx=vx; this.vy=vy; this.w=w; this.h=h; this.color=color; } public double getSpeed(){ return (Math.sqrt(vx*vx+vy*vy)); } public Rectangle getRect(){ return new Rectangle((int)x, (int)y, w, h); } public void move(){ vy+=.2; x+=vx; y+=vy;

} public void move(double f, int c){ vy+=.2*f*c; x+=vx*f*c; y+=vy*f*c; } public void unmove(double f, int c){ vy-=.2*f*c; x-=vx*f*c; y-=vy*f*c; } } public class Wall{ public int x, y, w, h; public Wall(int a, int b, int c, int d){ x=a; y=b; w=c; h=d; } public Rectangle getRect(){ return new Rectangle(x, y, w, h); } } public class Item{ public String id; public int x, y; public boolean active; public Item(String id, int x, int y){ this.id=id; this.x=x; this.y=y; active = true; } public Rectangle getRect(){ return new Rectangle(x, y, 25, 25); } } public class Shooter{ public int x, y, w, h; public double rads, power; public Color color; public Shooter(int a, int b, double rads, Color c){ x=a; y=b; w=25; h=25; this.rads=rads; power = 5; color = c;

} public void shoot(ArrayList marbles){ marbles.add(new Marble(x, y, (int)(Math.cos(rads)*power), (int)(Math .sin(rads)*power), 25, 25)); } public Shooter(int a, int b, Color c){ x=a; y=b; w=25; h=25; color = c; } public Rectangle getRect(){ return new Rectangle(x, y, w, h); } } }

You might also like