Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Simple Challenge: Link vs Goombas

Name: Anonymous 2010-06-07 17:22

Program a game in which the 'player' controls the likeness of Link(Zelda). The objective of the game is to defeat as many 'Goombas'(Mario Bros.) as they can before dieing. The rest is up to you.

Only one factor of your program will be judged: fun-ness.

Get to work!

Name: Anonymous 2010-06-08 8:41

First entry?

~:javac Mario.java
~:java Mario


import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Mario extends JFrame implements KeyListener, Runnable {
    public static void main(String[] args) {
        Mario m = new Mario();
        while(true) m.restart();
    }
   
    private static final int G = 4;
    private static final int WIDTH = 50;
    private static final int HEIGHT = 30;
    private static final int WGRID = 20;
    private static final int HGRID = 20;
    private static final long GAMERATE = 50;

    private double px, py, pvx, pvy, pay;
    private BufferedImage canvas;
    private Random rand;
    private Thread painter;
    private boolean l, r, failure;
    private ArrayList<Integer> goombas;
    private long startTime;
    private int bounces;
   
    public Mario() {
        super("Link v. Goombas");
        setSize(WIDTH * WGRID, HEIGHT * HGRID);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(this);
        rand = new Random();
        setVisible(true);
        canvas = new BufferedImage(WIDTH * WGRID, HEIGHT * HGRID, BufferedImage.TYPE_3BYTE_BGR);
    }
    public void restart() {
        if(painter != null) painter.interrupt();
        goombas = new ArrayList<Integer>();
        px = WIDTH*WGRID/2 - WGRID/2;
        py = pvx = pvy = 0;
        pay = G;
        failure = false;
        bounces = 0;
        l = r = false;
        goombas.add((int)px);
        startTime = System.currentTimeMillis();
        painter = new Thread(this);
        painter.start();
        try {
            painter.join();
        }
        catch(Exception e) { }
    }
    public void run() {
        long ltime = System.currentTimeMillis();
        while(true) {
            long ctime = System.currentTimeMillis();
            if(ctime != ltime) {
                react((ctime - ltime)/(double)GAMERATE);
                paint(getGraphics());
                ltime = ctime;
            }
            if(failure) return;
            try {
                Thread.sleep(1);
            }
            catch(Exception e) {
                return;
            }
        }
    }
    public void react(double frac) {
        double vfrac = frac;
        if(l == true && pvx > -30) pvx -= 3*frac;
        else if(r == true && pvx < 30) pvx += 3*frac;
        else {
            if(pvx > 0) pvx = Math.max(0, pvx-1*frac);
            if(pvx < 0) pvx = Math.min(0, pvx+1*frac);
        }
        px += pvx*frac;
        if(py + pvy * frac + HGRID >= (HEIGHT - 2) * HGRID) {
            double nfrac = ((HEIGHT - 3) * HGRID - py)/pvy;
            py += pvy * nfrac;
            vfrac = (frac - nfrac);
            for(int i = 0; i < goombas.size(); i++) {
                if(goombas.get(i) <= px + HGRID && goombas.get(i) + 2*HGRID >= px) {
                    pvy = -pvy;
                    goombas.remove(i);
                    bounces++;
                    break;
                }
            }
        }
        py += pvy*vfrac;
        double opvy = pvy;
        if((pvy <= 60 || pay <= 0) && (pvy >= -60 || pay >= 0)) pvy += pay*vfrac;
        if(opvy <= 0 && pvy >= 0) {
            int expectedGoombas = Math.max(1, 5-((int)((System.currentTimeMillis() - startTime)/1000))/15);
            for(int i = goombas.size(); i < expectedGoombas; i++) {
                int k;
                while(true) {
                    k = rand.nextInt((WIDTH-2) * HGRID);
                    boolean col = false;
                    for(int g : goombas) if(k <= g + HGRID*2 && k + 2*HGRID >= g) col = true;
                    if(!col) break;
                }
                goombas.add(k);
            }
        }
        if(py >= (HEIGHT - 1) * HGRID) {
            JOptionPane.showMessageDialog(this, "You lost! You bounced " + bounces + " time(s) and survived " + (System.currentTimeMillis() - startTime)/1000 + " seconds.");
            failure = true;
        }
        if(px < 0 && pvx < 0) pvx = -pvx;
        if(px >= (WIDTH - 1) * WGRID && pvx > 0) pvx = -pvx;
    }
    public void paint(Graphics gr) {
        Graphics g = canvas.getGraphics();
        g.setColor(Color.gray);
        g.fillRect(0, 0, WIDTH * WGRID, HEIGHT * HGRID);
        g.setColor(Color.green);
        g.fillRect(0, (HEIGHT - 1) * HGRID, WIDTH * WGRID, HEIGHT * HGRID);
        g.setColor(new Color(139, 69, 19));
        for(Integer i : goombas) g.fillRect(i, (HEIGHT-2) * HGRID, WGRID*2, HGRID);
        g.setColor(Color.red);
        g.fillRect((int)px, (int)py, WGRID, HGRID);
        gr.drawImage(canvas, 0, 0, null);
    }
    public void keyPressed(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT: l = true; r = false; break;
            case KeyEvent.VK_RIGHT: r = true; l = false; break;
            default: break;
        }
    }
    public void keyReleased(KeyEvent e) {
        switch(e.getKeyCode()) {
            case KeyEvent.VK_LEFT: l = false; break;
            case KeyEvent.VK_RIGHT: r = false; break;
            default: break;
        }
    }
    public void keyTyped(KeyEvent e) {}
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List