Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2012, 07:26 PM
spudlogic spudlogic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 10 spudlogic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 33 sec
Reputation Power: 0
Homework - BreakOut the game

Hi. I'm at the first stage of making the game BreakOut. This is what I have so far. I was wondering if there is a more efficient way to write this? Thanks
Code:
import java.awt.Color;
import acm.program.*; 
import acm.graphics.*;

public class BreakOut extends GraphicsProgram
{
public static final int APP_WIDTH = 400;
public static final int APP_HEIGHT = 600;

private static final int PADDLE_Y_OFFSET = 30;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_STARTX = (APP_WIDTH / 2) - (PADDLE_WIDTH / 2);
private static final int PADDLE_STARTY =  APP_HEIGHT - PADDLE_Y_OFFSET;

private static final int BRICK_Y_OFFSET = 70;
private static final int BRICK_ROWS = 10;
private static final int BRICK_COLUMNS = 10;
private static final int BRICK_SEPARATION = 4;
private static final int BRICK_HEIGHT = 8;
private static final int BRICK_WIDTH = (APP_WIDTH - (BRICK_COLUMNS - 1) * BRICK_SEPARATION) / BRICK_COLUMNS;

private static final int BALL_RADIUS = 10;
private static final int BALL_STARTX = (APP_WIDTH / 2) - (BALL_RADIUS / 2);
private static final int BALL_STARTY =  APP_HEIGHT / 2;

private GOval ball;
private GRect paddle;

public void run() 
{
    addBricks();
    addBall();
    addPaddle();
}

private void addBricks()
{
    int brickY = BRICK_Y_OFFSET;
    GRect brick;
    for (int i = 0; i < BRICK_ROWS; i++) 
    {
        int brickX = BRICK_SEPARATION;

        for (int j = 0; j < BRICK_COLUMNS; j++) 
        {
            brick = new GRect(brickX, brickY, BRICK_WIDTH, BRICK_HEIGHT);
            add(brick);

            colorBricks(brick, Color.RED, 0, 1);
            colorBricks(brick, Color.ORANGE, 2, 3);
            colorBricks(brick, Color.YELLOW, 4, 5);
            colorBricks(brick, Color.GREEN, 6, 7);
            colorBricks(brick, Color.CYAN, 8, 9);

            brickX += BRICK_WIDTH + BRICK_SEPARATION;
        }
        brickY += BRICK_HEIGHT + BRICK_SEPARATION;
    }
}

private void colorBricks(GRect brick, Color color, int firstRow, int lastRow) 
{
    if (brick.getY() >= BRICK_Y_OFFSET + firstRow * (BRICK_HEIGHT + BRICK_SEPARATION) && brick.getY() <= BRICK_Y_OFFSET + lastRow * (BRICK_HEIGHT + BRICK_SEPARATION)) 
    {
        brick.setColor(color);
        brick.setFilled(true);
    }
}

private void addBall()
{
    ball = new GOval(BALL_STARTX, BALL_STARTY, BALL_RADIUS, BALL_RADIUS);
    ball.setFilled(true);
    add(ball);
}

private void addPaddle()
{
    paddle = new GRect(PADDLE_STARTX, PADDLE_STARTY, PADDLE_WIDTH, PADDLE_HEIGHT);
    paddle.setFilled(true);
    add(paddle);
}
}

Reply With Quote
  #2  
Old January 29th, 2012, 08:11 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Click here for more information.
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 1,758 NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level)NormR User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 20 h 51 sec
Reputation Power: 288
Where are you looking for more efficiency?
Perhaps pass an array to colorBricks and only call it one time

Reply With Quote
  #3  
Old January 29th, 2012, 11:48 AM
spudlogic spudlogic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 10 spudlogic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 38 m 33 sec
Reputation Power: 0
Quote:
Originally Posted by NormR
Where are you looking for more efficiency?
Perhaps pass an array to colorBricks and only call it one time

That's a good idea. It was recommended that I make three classes to separate the game objects from the game play.

Thanks for the tip!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - BreakOut the game


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap