import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class Ski implements KeyListener{
Image imgSkier, imgTree, imgGoomba;
boolean crashed, leftArrow, rightArrow, upArrow, downArrow;
int skierX, skierY, treeX, treeY, goombaX, goombaY;
int skierWidth, skierHeight, treeWidth, treeHeight, goombaWidth, goombaHeight;
int score;
JFrame frame;
GamePanel gPanel;
public Ski() {
score = 0;
treeX = 10 + (int)(Math.random() * ((500 - 10) + 1));
treeY = 500;
goombaX = 10 + (int)(Math.random() * ((500 - 10) + 1));
goombaY = 600;
skierX = 100;
skierY = 10;
crashed = false;
leftArrow = false;
rightArrow = false;
upArrow = false;
downArrow = false;
frame = new JFrame();
gPanel = new GamePanel();
gPanel.setFocusable(true);
gPanel.addKeyListener(this);
getImages();
frame.add(gPanel);
frame.setSize(500,700);
frame.setVisible(true);
animate();
}
public void getImages(){
imgSkier = new ImageIcon("skier.png").getImage();
imgTree = new ImageIcon("tree.png").getImage();
imgGoomba = new ImageIcon("Goomba.png").getImage();
skierWidth = imgSkier.getWidth(null);
skierHeight = imgSkier.getHeight(null);
treeWidth = imgTree.getWidth(null);
treeHeight = imgTree.getHeight(null);
goombaWidth = imgGoomba.getWidth(null);
goombaHeight = imgGoomba.getHeight(null);
}
class GamePanel extends JPanel{//inner class to customize a JPanel for drawing
public void paintComponent(Graphics g){
g.setColor(Color.white);//Avoid trails
g.fillRect(0,0,this.getWidth(), this.getHeight());//Avoid trails
g.drawImage(imgSkier, skierX, skierY, 60, 100, null);
g.drawImage(imgTree, treeX, treeY, 60, 100, null);
g.drawImage(imgGoomba, goombaX, goombaY, 60, 100, null);
g.setColor(Color.black);
g.fillRect(400,0,84,30);
g.setColor(Color.yellow);
g.fillRect(404,4,76,22);
g.setColor(Color.blue);
g.setFont(new Font("Arial", Font.BOLD, 12));
g.drawString("Score: " + score, 415, 20);
if(crashed){
g.setColor(Color.black);
g.fillRoundRect(60,100,350,200,20,20);
g.setColor(Color.green);
g.fillRoundRect(65,105,340,190,20,20);
g.setColor(Color.blue);
g.fillRoundRect(70,110,330,180,20,20);
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 50));
g.drawString("GAME OVER", 80, 180);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Your Final Score: " + score, 140, 230);
}
}
}
public void animate(){
while(!crashed){
treeY = treeY - 2;
goombaY = goombaY - 3;
if(leftArrow) skierX -= 5;
if(rightArrow) skierX += 5;
if(upArrow) skierY -= 5;
if(downArrow) skierY += 5;
try{
Thread.sleep(10);
} catch(Exception e){}
Rectangle skierBounds = new Rectangle(skierX, skierY, 60, 100);
Rectangle treeBounds = new Rectangle(treeX, treeY, 60, 100);
Rectangle goombaBounds = new Rectangle(goombaX, goombaY, 60, 100);
if(skierBounds.intersects(treeBounds) ){
crashed = true;
}
if(skierBounds.intersects(goombaBounds) ){
crashed = true;
}
if(treeY < 1){
score = score + 1;
treeX = 10 + (int)(Math.random() * ((500 - 10) + 1));
treeY = 600;
}
if(goombaY < 1){
score = score + 1;
goombaX = 10 + (int)(Math.random() * ((500 - 10) + 1));
goombaY = 600;
}
gPanel.repaint();
}//close loop
} //close animate
public void keyTyped(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
leftArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
rightArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_UP){
upArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
downArrow = true;
}
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
leftArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
rightArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_UP){
upArrow = true;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
downArrow = true;
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
leftArrow = false;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
rightArrow = false;
}
if(e.getKeyCode() == KeyEvent.VK_UP){
upArrow = false;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
downArrow = false;
}
}
public static void main(String[] y)
{
Ski s = new Ski();
}
}