Skip to content

Commit

Permalink
Merge branch 'release/v0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm1986 committed Oct 15, 2015
2 parents 36e03ca + 1787d6e commit 8a49690
Show file tree
Hide file tree
Showing 8 changed files with 665 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
# Created by https://www.gitignore.io/api/java,netbeans,maven,windows,linux

### Java ###
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
.nb-gradle/


### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


*.class

# Mobile Tools for Java (J2ME)
Expand All @@ -10,3 +81,4 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/Pong_Game/target/
33 changes: 33 additions & 0 deletions Pong_Game/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>javase</groupId>
<artifactId>Pong_Game</artifactId>
<version>v0.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<name>Pong Game</name>
<description>This game has been developed to practise Java GUI and threads.</description>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<mainClass>javase/pong_game/Pong</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
203 changes: 203 additions & 0 deletions Pong_Game/src/main/java/gui/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* Copyright 2015 Javier Gusano Martinez.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import objects.BallObject;
import objects.BarObject;

/**
* Game panel
* @author <a href="mailto:jgm1986@hotmail.com">Javier Gusano Martinez</a>
*/
public class GamePanel extends JPanel{
// Game objects
private BarObject leftPlayer;
private BarObject rightPlayer;
private BallObject ball;
// Keys flags
private boolean key_w;
private boolean key_s;
private boolean key_up;
private boolean key_down;
// Timer for game refresh
private Timer timer;
// Point panel
GamePoints pointPanel;

public GamePanel(GamePoints pointPanel){
setBackground(new Color(97, 218, 146));
this.pointPanel = pointPanel;
// Players bars
leftPlayer = new BarObject(0, 190, Color.BLUE);
rightPlayer = new BarObject(720, 190, Color.RED);
ball = new BallObject(360, 240, Color.DARK_GRAY);

// Timer object
timer = new Timer(10, new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
updatePositions();
}
});
}

/**
* Pain components method override
* @param g Graphics object.
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw play area lines
g.drawLine(20, 0, 20, 480);
g.drawLine(370, 0, 370, 480);
g.drawLine(720, 0, 720, 480);
// Draw players bars
Graphics2D g2 = (Graphics2D) g;
g2.setColor(leftPlayer.getColor());
g2.fill(leftPlayer.getBarGraph());
g2.setColor(rightPlayer.getColor());
g2.fill(rightPlayer.getBarGraph());
// Draw ball
g2.setColor(ball.getColor());
g2.fill(ball.getBallGraph());
if(!gameRunning()){
// Show start text
g2.setFont(new Font(null, Font.BOLD, 20));
g2.setColor(Color.MAGENTA);
g2.drawString("Press SPACEBAR to start the game!", 375, 30);
this.pointPanel.setFocusableButton(true);
}
this.pointPanel.setFocusableButton(false);
}

/**
* Used to change the flags
* @param keyCode Code of the key pressed.
* @param value Value of the key (true if is pressed, false if not).
*/
public void setFlag(int keyCode, boolean value){
switch( keyCode ) {
case KeyEvent.VK_UP:
if(key_down == false){
key_up = value;
}
break;
case KeyEvent.VK_DOWN:
if(key_up == false){
key_down = value;
}
break;
case KeyEvent.VK_W:
if(key_s == false){
key_w = value;
}
break;
case KeyEvent.VK_S:
if(key_w == false){
key_s = value;
}
break;
}
}

/**
* Method for keyboard flags debug.
* Returns a string with the keyboard game keys flags.
* @return String with the flags status.
*/
public String debugKeys(){
return "Key status Up = " + key_up + " Down = " + key_down + " | W = " + key_w + " S = " + key_s;
}

private void updatePositions(){
// Ball position
ball.updatePosition();
switch(playerPoint()){
case -1:
pointPanel.rightPoint();
ball.resetBall();
break;
case 1:
pointPanel.leftPoint();
ball.resetBall();
break;
}

// Left player
if(key_w){
leftPlayer.moveUp();
}else if(key_s){
leftPlayer.moveDown();
}
// Right player
if(key_up){
rightPlayer.moveUp();
} else if(key_down){
rightPlayer.moveDown();
}
repaint();
}

public void timerStartStop(){
if(gameRunning()){
timer.stop();
repaint();
} else {
System.out.println("Entramos en START");
timer.start();
}
}

/**
* Return true if the game is running. False if not.
* @return True if the game is running. False if not.
*/
public boolean gameRunning(){
return timer.isRunning();
}

/**
* This method check if the ball has found a player bar to continue the game
* or not to point the player.
* @return Returns -1 if left player loss the ball, 0 player hit the ball and
* 1 if the right player loss the ball.
*/
private int playerPoint() {
// Check Left player.
if(ball.getX() <= 20){
if((ball.getY() < leftPlayer.getY()) || (ball.getY() > (leftPlayer.getY() + leftPlayer.getHeight()))){
return -1;
}
}
// Check Right player
if(ball.getX() >= 700){
if((ball.getY() < rightPlayer.getY()) || (ball.getY() > (rightPlayer.getY() + rightPlayer.getHeight()))){
return 1;
}
}
return 0;
}
}
Loading

0 comments on commit 8a49690

Please sign in to comment.