Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package isu;
- /**
- * @author James Muffitt
- * @date June 16, 2019
- * @title Breakout
- * @description Simple game very similar to space invaders.
- *
- */
- import java.awt.*;
- import java.applet.Applet;
- public class Breakout extends Applet implements Runnable
- {
- Dimension d;
- Font largefont = new Font("Helvetica", Font.BOLD, 24);
- Font smallfont = new Font("Helvetica", Font.BOLD, 14);
- FontMetrics fmsmall, fmlarge;
- Graphics goff;
- Image ii;
- Thread thethread;
- boolean oneplayer=false;
- boolean ingame=false; int player1score;int player2score; int player1lives; int player2lives;
- int player1pos;
- int player1dpos;
- boolean player1fire;
- int player2pos;
- int player2dpos;
- boolean player2fire;
- int player1y;
- int player2y;
- int bullet1xpos;
- int bullet1ypos;
- int bullet2xpos;
- int bullet2ypos;
- int[] ballx;
- int[] bally;
- int[] balldx;
- int[] ballampl;
- int[] ballpos;
- boolean[] ballinstore;
- final int numballs=8;
- final int storeheight=48;
- final int borderwidth=5;
- final int ballsize=6;
- final int bounceperiod=128;
- final int scoreheight=20;
- final int screendelay=300;
- final int gutterheight=24;
- final int turretwidth=17;
- final int bulletspeed=4;
- final int maxballdelta=48;
- final int minampl=32;
- int count;
- boolean showtitle=true;
- Color ballcolor;
- Color turret1color;
- Color turret2color;
- int ballsinplay;
- int balldelta;
- public String getAppletInfo()
- {
- return("James Muffitt - ISU");
- }
- public void init()
- {
- Graphics g;
- int i;
- d = size();
- g=getGraphics();
- g.setFont(smallfont);
- fmsmall = g.getFontMetrics();
- g.setFont(largefont);
- fmlarge = g.getFontMetrics();
- ballx=new int[numballs];
- bally=new int[numballs];
- balldx=new int[numballs];
- ballampl=new int[numballs];
- ballpos=new int[numballs];
- ballinstore=new boolean[numballs];
- ballcolor=new Color(255,0,0);
- turret1color=new Color(255,192,128);
- turret2color=new Color(192,255,128);
- GameInit();
- }
- public void GameInit()
- {
- int i;
- for (i=0; i<numballs; i++)
- {
- ballpos[i]=(int)(Math.random()*(float)(bounceperiod));
- ballx[i]=borderwidth*2+(int)(Math.random()*(float)(d.width-ballsize-4*borderwidth));
- ballampl[i]=storeheight-ballsize;
- bally[i]=storeheight+borderwidth-ballsize-
- (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
- if (Math.random()>=0.5)
- balldx[i]=1;
- else
- balldx[i]=-1;
- ballinstore[i]=true;
- }
- player1score=0;
- player2score=0;
- player1lives=3;
- player2lives=3;
- player1fire=false;
- player2fire=false;
- player1dpos=0;
- player2dpos=0;
- player1y=d.height-scoreheight-borderwidth*2-gutterheight-1;
- player2y=player1y;
- player1pos=0+borderwidth+32;
- player2pos=d.width-borderwidth-32-turretwidth;
- bullet1xpos=-1;
- bullet1ypos=-1;
- bullet2xpos=-1;
- bullet2ypos=-1;
- ballsinplay=0;
- balldelta=6;
- }
- public boolean keyDown(Event e, int key)
- {
- if (ingame)
- {
- if (!oneplayer)
- {
- if (key == '1')
- player2dpos=-3;
- if (key == '3')
- player2dpos=3;
- if (key == '2')
- player2fire=true;
- }
- if (key == 'z' || key == 'Z' )
- player1dpos=-3;
- if (key == 'c' || key == 'C' )
- player1dpos=3;
- if (key == 'x' || key == 'X' )
- player1fire=true;
- if (key == Event.ESCAPE)
- ingame=false;
- }
- else
- {
- if (key == '1')
- {
- oneplayer=true;
- GameInit();
- ingame=true;
- }
- if (key == '2')
- {
- oneplayer=false;
- GameInit();
- ingame=true;
- }
- }
- return true;
- }
- public boolean keyUp(Event e, int key)
- {
- if (key == '1' || key == '3')
- player2dpos=0;
- if (key == 'z' || key == 'Z' || key == 'c' || key == 'C')
- player1dpos=0;
- return true;
- }
- public void paint(Graphics g)
- {
- String s;
- Graphics gg;
- if (goff==null && d.width>0 && d.height>0)
- {
- ii = createImage(d.width, d.height);
- goff = ii.getGraphics();
- }
- if (goff==null || ii==null)
- return;
- goff.setColor(new Color(16,24,64));
- goff.fillRect(0, 0, d.width, d.height);
- if (ingame)
- PlayGame();
- else
- ShowIntroScreen();
- g.drawImage(ii, 0, 0, this);
- }
- public void PlayGame()
- {
- DrawPlayField();
- HandleKeys();
- DrawTurrets();
- CheckBallsInPlay();
- DoBalls();
- DoBullets();
- ShowScore();
- if (oneplayer)
- {
- if (player1lives<=0 && player1pos>=d.width)
- ingame=false;
- }
- else
- {
- if (player1lives<=0 && player2lives<=0 && player1pos>=d.width && player2pos>=d.width)
- ingame=false;
- }
- }
- void HandleKeys()
- {
- player1pos+=player1dpos;
- if (player1pos<=borderwidth)
- player1pos=borderwidth;
- if (player1pos>=(d.width-borderwidth-turretwidth-1) && player1lives>0)
- player1pos=d.width-borderwidth-turretwidth-1;
- if (player1fire && bullet1xpos<0 && bullet1ypos<0)
- {
- bullet1ypos=d.height-scoreheight-gutterheight-ballsize*2-borderwidth*2;
- bullet1xpos=player1pos+turretwidth/2;
- }
- player1fire=false;
- if (oneplayer)
- return;
- player2pos+=player2dpos;
- if (player2pos<=borderwidth)
- player2pos=borderwidth;
- if (player2pos>=(d.width-borderwidth-turretwidth-1) && player2lives>0)
- player2pos=d.width-borderwidth-turretwidth-1;
- if (player2fire && bullet2xpos<0 && bullet2ypos<0)
- {
- bullet2ypos=d.height-scoreheight-gutterheight-ballsize*2-borderwidth*2;
- bullet2xpos=player2pos+turretwidth/2;
- }
- player2fire=false;
- }
- void CheckBallsInPlay()
- {
- boolean none=true;
- int i;
- for (i=0; i<numballs; i++)
- {
- if (!ballinstore[i])
- none=false;
- }
- if (none)
- {
- if (ballsinplay<numballs)
- ballsinplay++;
- else
- if (balldelta<=maxballdelta)
- balldelta+=4;
- for (i=0; i<ballsinplay; i++)
- {
- ballinstore[i]=false;
- ballampl[i]=d.height-4*borderwidth-scoreheight-storeheight-ballsize-gutterheight;
- ballpos[i]=bounceperiod/2;
- }
- }
- }
- void ResetBall(int i)
- {
- ballinstore[i]=true;
- ballampl[i]=storeheight-ballsize;
- ballx[i]=borderwidth*2+(int)(Math.random()*(float)(d.width-ballsize-4*borderwidth));
- bally[i]=storeheight+borderwidth-ballsize-
- (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
- }
- boolean DrawBullet(int x, int y)
- {
- int i;
- goff.setColor(Color.white);
- goff.drawLine(x,y,x,y+ballsize);
- for (i=0; i<numballs; i++)
- {
- if ((bally[i]+ballsize)>=y && bally[i]<=(y+ballsize) &&
- (ballx[i]+ballsize)>=x && ballx[i]<=x)
- {
- ResetBall(i);
- return true;
- }
- }
- return false;
- }
- void DoBullets()
- {
- if (bullet1xpos>0 && bullet1ypos>0)
- {
- if (DrawBullet(bullet1xpos,bullet1ypos))
- {
- player1score+=10;
- bullet1xpos=-1;
- bullet1ypos=-1;
- }
- else
- {
- bullet1ypos-=bulletspeed;
- if (bullet1ypos<=(2*borderwidth+storeheight))
- {
- bullet1ypos=-1;
- bullet1xpos=-1;
- }
- }
- }
- if (oneplayer)
- return;
- if (bullet2xpos>0 && bullet2ypos>0)
- {
- if (DrawBullet(bullet2xpos,bullet2ypos))
- {
- player2score+=10;
- bullet2xpos=-1;
- bullet2ypos=-1;
- }
- else
- {
- bullet2ypos-=bulletspeed;
- if (bullet2ypos<=(2*borderwidth+storeheight))
- {
- bullet2ypos=-1;
- bullet2xpos=-1;
- }
- }
- }
- }
- boolean DrawTurret(int x, int y)
- {
- int x1=x;
- int y1=y;
- int x2=turretwidth+x;
- boolean hitball=false;
- int i;
- while (x1<x2)
- {
- for (i=0; i<numballs; i++)
- {
- if ((bally[i]+ballsize>=y1) && bally[i]<=y1 && (ballx[i]+ballsize)>=x1 && ballx[i]<=x2)
- {
- hitball=true;
- ResetBall(i);
- }
- }
- goff.drawLine(x1,y1,x2,y1);
- y1--;
- x1++;
- x2--;
- }
- return hitball;
- }
- void DrawBall(int x, int y)
- {
- goff.fillRect(x,y+1,ballsize,ballsize-2);
- goff.drawLine(x+1,y,x+ballsize-2,y);
- goff.drawLine(x+1,y+ballsize-1,x+ballsize-2,y+ballsize-1);
- }
- void DoBalls()
- {
- int i;
- goff.setColor(ballcolor);
- for (i=0; i<numballs; i++)
- {
- if (ballinstore[i])
- {
- bally[i]=storeheight+borderwidth-ballsize-
- (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
- ballpos[i]=(ballpos[i]+1)%bounceperiod;
- ballx[i]=ballx[i]+balldx[i];
- if (ballx[i]<=borderwidth || ballx[i]>=(d.width-borderwidth-ballsize))
- balldx[i]=-balldx[i];
- DrawBall(ballx[i],bally[i]);
- }
- else
- {
- if (bally[i]<borderwidth*2+storeheight)
- bally[i]+=2;
- else
- {
- bally[i]=d.height-gutterheight-2*borderwidth-scoreheight-ballsize-
- (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
- ballpos[i]=(ballpos[i]+1)%bounceperiod;
- if (ballpos[i]==0)
- {
- ballampl[i]-=balldelta;
- if (ballampl[i]<minampl)
- ballampl[i]=minampl;
- }
- }
- ballx[i]=ballx[i]+balldx[i];
- if (ballx[i]<=borderwidth || ballx[i]>=(d.width-borderwidth-ballsize))
- balldx[i]=-balldx[i];
- DrawBall(ballx[i],bally[i]);
- }
- }
- }
- public void ShowIntroScreen()
- {
- String s;
- DrawPlayField();
- ShowScore();
- DoBalls();
- goff.setFont(largefont);
- goff.setColor(new Color(96,128,255));
- if (showtitle)
- {
- s="Breakout";
- goff.drawString(s,(d.width-fmlarge.stringWidth(s)) / 2, (d.height-scoreheight-borderwidth)/2 - 30);
- goff.setFont(smallfont);
- s="A game dedicated to Mr. Gabriele";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2);
- goff.setColor(new Color(255,160,64));
- s="(c)2019 by James Muffitt";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 20);
- s="072937824";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 40);
- }
- else
- {
- goff.setFont(smallfont);
- goff.setColor(new Color(96,128,255));
- s="'1'=1 player game, '2'=2 players";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 - 20);
- goff.setColor(new Color(255,160,64));
- s="Player 1 use 'z', 'x' and 'c'";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 10);
- s="Player 2 use '1, '2' and '3'";
- goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 30);
- }
- count--;
- if (count<=0)
- { count=screendelay; showtitle=!showtitle; }
- }
- void DrawTurrets()
- {
- int delta;
- goff.setColor(turret1color);
- if (player1lives==0)
- {
- if (player1y<d.height-scoreheight-borderwidth*2-1)
- player1y++;
- else if (player1pos<d.width+turretwidth)
- player1pos++;
- }
- if (DrawTurret(player1pos,player1y))
- {
- player1lives--;
- player1y+=2;
- }
- if (oneplayer)
- return;
- goff.setColor(turret2color);
- if (player2lives==0)
- {
- if (player2y<d.height-scoreheight-borderwidth*2-1)
- player2y++;
- else if (player2pos<d.width+turretwidth)
- player2pos++;
- }
- if (DrawTurret(player2pos,player2y))
- {
- player2lives--;
- player2y+=2;
- }
- }
- public void DrawPlayField()
- {
- goff.setColor(Color.white);
- goff.fillRect(0,0,d.width,borderwidth);
- goff.fillRect(0,0,borderwidth,d.height-scoreheight);
- goff.fillRect(d.width-borderwidth,0,borderwidth,d.height-scoreheight);
- goff.fillRect(0,0+borderwidth+storeheight,d.width,borderwidth);
- goff.fillRect(0,d.height-gutterheight-2*borderwidth-scoreheight,d.width,borderwidth);
- goff.fillRect(0,d.height-scoreheight-borderwidth,d.width,borderwidth);
- goff.setColor(new Color(128,128,255));
- goff.fillRect(borderwidth,d.height-borderwidth-gutterheight-scoreheight,
- d.width-borderwidth,gutterheight);
- }
- public void ShowScore()
- {
- String s;
- goff.setFont(smallfont);
- goff.setColor(Color.white);
- s="Player 1: "+player1score;
- goff.drawString(s,borderwidth,d.height-5);
- if (!oneplayer)
- {
- s="Player 2: "+player2score;
- goff.drawString(s,d.width-borderwidth-fmsmall.stringWidth("Player 2: 00000"),d.height-5);}}
- public void run(){ long starttime; Graphics g;
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- g=getGraphics();
- while(true){
- starttime=System.currentTimeMillis();
- try{
- paint(g);
- starttime += 20;
- Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));}
- catch (InterruptedException e){
- break;}}}public void start(){
- if (thethread == null) {
- thethread = new Thread(this);
- thethread.start();
- }
- }
- public void stop()
- {
- if (thethread != null) {
- thethread.stop();
- thethread = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement