Newman/Assignment7

From ggc

Jump to: navigation, search

001 package Newman;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * All about my game.
012  @author Moriah
013  */
014 public class Assignment7 extends Game
015 {
016   private ArrayList<Sprite> allBricks;
017   private RectangleSprite brick;
018   private OvalSprite paddle;
019   private OvalSprite ball;
020   private ProjectileTransformer projectile;
021   private OutlineSprite wall, wall2, wall3;
022   private StringSprite score;
023 
024   /**sets up the game*/
025   public void setup()
026   {
027     makeAndAddBricks();
028     makeAndAddPaddle();
029     makeAndAddBall();
030     makeAndAddWall();
031     makeAndAddScore();
032     projectile=new ProjectileTransformer(.1,.25);
033     ball.setTracker(projectile);
034 
035 
036   }
037   private void makeAndAddBricks()
038   {
039     allBricks=new ArrayList<Sprite>();
040     int brickAcross=9;
041     int brickDown=3;
042     for (int = 1; i<=brickAcross; i++)
043     {
044       for (int = 0; j<=brickDown; j++)
045       {
046         brick=new RectangleSprite(43);
047         brick.setSize(0.75/brickAcross);
048         double x=.95/brickAcross+0.9/brickAcross*(i-1);
049         double y=0.5/brickAcross+(j+0.5)/brickAcross;
050         brick.setLocation(x, y);
051         brick.setColor(Palette.getColor("Purple"));
052         brick.setVisible(true);
053         allBricks.add(brick);
054         addSprite(brick);
055       }
056     }
057 
058 
059 
060   }
061   private void makeAndAddPaddle()
062   {
063     paddle=new OvalSprite(3,1);
064     paddle.setSize(.25);
065     paddle.setLocation(.5,.9);
066     paddle.setColor(Palette.getColor("Green"));
067     addSprite(paddle);
068   }
069   private void makeAndAddBall()
070   {
071     ball=new OvalSprite(2,2);
072     ball.setSize(.045);
073     ball.setLocation(.5,.65);
074     ball.setColor(Palette.getColor("Hot Pink"));
075     addSprite(ball);
076   }
077   private void  makeAndAddWall()
078   {
079     wall=new OutlineSprite(new LineSprite(0010));
080     wall.setSize(2);
081     wall.setLocation(0,0);
082     wall.setColor(Palette.getColor("Light Blue"));
083     addSprite(wall);
084 
085     wall2=new OutlineSprite(new LineSprite(0001));
086     wall2.setSize(2);
087     wall2.setLocation(1,0);
088     wall2.setColor(Palette.getColor("Light Blue"));
089     addSprite(wall2);
090 
091     wall3=new OutlineSprite(new LineSprite(00,1));
092     wall3.setSize(2);
093     wall3.setLocation(0,0);
094     wall3.setColor(Palette.getColor("Light Blue"));
095     addSprite(wall3);
096 
097   }
098 
099   private void movePaddle()
100   {
101     paddle.setX(getMouseX());
102 
103   }
104 
105   private void handleCollisions()
106   {
107     ball.bounceOffOf(brick);
108     ball.bounceOffOf(paddle);
109     ball.bounceOffOf(wall);
110     ball.bounceOffOf(wall2);
111     ball.bounceOffOf(wall3);
112     handleBallBricksCollisions();
113   }
114 
115   private void makeAndAddScore()
116   {
117     score=new StringSprite("0");
118     score.setSize(0.15);
119     score.setColor(getColor("white"));
120     score.setLocation(0.90.8);
121     addSprite(score);
122 
123   }
124   private void  handleBallBricksCollisions()
125   {
126     for(Sprite single: allBricks)
127     {
128       if(single.isVisible()==true && single.intersects(ball))
129       {
130         ball.bounceOffOf(single);
131 
132         single.setVisible(false);
133         int startscores = getScore();
134         setScore(startscores+5);
135 
136       }
137 
138     }
139   }
140 
141 
142 
143 
144   /**handle input and game events*/
145   public void advance()
146   {
147     movePaddle();
148     handleCollisions();
149     handleBallBricksCollisions();
150     score.setText(""+getScore());
151 
152 
153   }
154 }


Download/View Newman/Assignment7.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter