Skip to content

Instantly share code, notes, and snippets.

@Mexicoder
Created May 8, 2015 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mexicoder/37df983d66639a658a45 to your computer and use it in GitHub Desktop.
Save Mexicoder/37df983d66639a658a45 to your computer and use it in GitHub Desktop.
my First Game - Pong
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Pong.iml" filepath="$PROJECT_DIR$/Pong.iml" />
</modules>
</component>
</project>
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/src/C-read-from-.CSV-to-database-tool" vcs="Git" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.awt.*;
import java.util.Random;
/**
* Created by john on 4/6/2015.
*/
public class Ball implements Runnable{
// Global variables
int x,y,xDir,yDir;
Paddle p1 = new Paddle(15,140, 1);
Paddle p2 = new Paddle(370,140, 2);
Rectangle ball;
int p1Score, p2Score;
public Ball(int x, int y){
this.x = x;
this.y = y;
//Set Ball moving randomly
Random r = new Random();
int rDir = r.nextInt(2);
if(rDir == 0)
rDir--;
setXDirection(rDir);
int yrDir = r.nextInt(2);
if(yrDir == 0)
yrDir--;
setYDirection(yrDir);
// create ball
ball = new Rectangle(this.x, this.y, 15,15);
p1Score = p2Score = 0;
}
public void setXDirection(int xdir){
xDir = xdir;
}
public void setYDirection(int ydir){
yDir = ydir;
}
public void draw(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(ball.x,ball.y,ball.width,ball.height);
}
public void move(){
ball.x += xDir;
ball.y += yDir;
//Bouce ball if enge is detected
if(ball.x <= 0){
setXDirection(+1);
p2Score++;
ball.x = 193;
ball.y = 143;
}
if(ball.x >= 385){
setXDirection(-1);
p1Score++;
ball.x = 193;
ball.y = 143;
}
if(ball.y <= 20){
setYDirection(+1);
}
if(ball.y >= 285){
setYDirection(-1);
}
if(ball.intersects(p1.paddle)){
if(xDir > 0)
setXDirection(-1);
else
setXDirection(+1);
}
if(ball.intersects(p2.paddle)){
if(xDir > 0)
setXDirection(-1);
else
setXDirection(+1);
}
}
@Override
public void run() {
try {
while(true){
move();
Thread.sleep(6);
}
}
catch(Exception ex){
System.err.println(ex.getMessage());
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Copyright MineStein 2014©
* All files included within the project are subject under the standard
* GNU license. Any and all assets are the sole property of MineStein.
*/
public class Main extends JFrame {
//double buffering
Image dbImage;
Graphics dbg;
//Ball objects
static Ball b = new Ball(193,143);
Dimension dimension = new Dimension(400, 300);
public Main() {
this.setTitle("PingPong");
this.setSize(dimension);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBackground(Color.DARK_GRAY);
addKeyListener(new AC());
}
public class AC extends KeyAdapter {
public void keyPressed(KeyEvent e) {
b.p1.keyPressed(e);
b.p2.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
b.p1.keyReleased(e);
b.p2.keyReleased(e);
}
}
public static void main(String[] args) {
Main core = new Main();
//create and start threads
Thread ball = new Thread(b);
ball.start();
Thread p1 = new Thread(b.p1);
Thread p2 = new Thread(b.p2);
p1.start();
p2.start();
}
public void paint(Graphics g){
dbImage = createImage(getWidth(),getHeight());
dbg = dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage,0,0,this);
}
public void draw(Graphics g){
b.draw(g);
b.p1.draw(g);
b.p2.draw(g);
g.setColor(Color.WHITE);
g.drawString(""+b.p1Score, 20, 50);
g.drawString(""+b.p2Score, 370, 50);
repaint();
}
}
import java.awt.*;
import java.awt.event.KeyEvent;
/**
* Created by john on 4/12/2015.
*/
public class Paddle implements Runnable{
int x, y, yDirection, id;
Rectangle paddle;
public Paddle(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
paddle = new Rectangle(x,y,10,50);
}
public void keyPressed(KeyEvent e){
switch(id){
default:
System.out.println("Please Enter a Valid ID in Paddle Constructor.");
break;
case 1:
if(e.getKeyCode() == e.VK_UP){
setYDirection(-1);
}
if(e.getKeyCode() == e.VK_DOWN){
setYDirection(1);
}
break;
case 2:
if(e.getKeyCode() == e.VK_W){
setYDirection(-1);
}
if(e.getKeyCode() == e.VK_S){
setYDirection(1);
}
break;
}
}
public void keyReleased(KeyEvent e){
switch(id){
default:
System.out.println("Please Enter a Valid ID in Paddle Constructor.");
break;
case 1:
if(e.getKeyCode() == e.VK_UP){
setYDirection(0);
}
if(e.getKeyCode() == e.VK_DOWN){
setYDirection(0);
}
break;
case 2:
if(e.getKeyCode() == e.VK_W){
setYDirection(0);
}
if(e.getKeyCode() == e.VK_S){
setYDirection(0);
}
break;
}
}
public void setYDirection(int ydir){
yDirection = ydir;
}
public void move(){
paddle.y += yDirection;
if(paddle.y <= 15){
paddle.y = 15;
}
if(paddle.y >= 250){
paddle.y = 250;
}
}
public void draw(Graphics g){
switch(id) {
default:
System.out.println("Please Enter a Valid ID in Paddle Constructor.");
break;
case 1:
g.setColor(Color.CYAN);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
break;
case 2:
g.setColor(Color.RED);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
break;
}
}
@Override
public void run() {
try {
while(true){
move();
Thread.sleep(8);
}
}
catch(Exception ex){
System.err.println(ex.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment