A very simple car game in Java



Similar documents
public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener {

Java Mouse and Keyboard Methods

Using A Frame for Output

core 2 Handling Mouse and Keyboard Events

Remote Method Invocation

Chapter 2 Introduction to Java programming

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

CS 335 Lecture 06 Java Programming GUI and Swing

Image Processing. In this chapter: ImageObserver ColorModel ImageProducer ImageConsumer ImageFilter

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Construction of classes with classes

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Homework/Program #5 Solutions

Advanced Network Programming Lab using Java. Angelos Stavrou

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Event-Driven Programming

Threads 1. When writing games you need to do more than one thing at once.

JAVA Program For Processing SMS Messages

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

An Overview of Java. overview-1

CS506 Web Design and Development Solved Online Quiz No. 01

The following program is aiming to extract from a simple text file an analysis of the content such as:

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

Orders.java. ArrayList of Drinks as they are ordered. Functions to calculate statistics on Drinks

Twin A Design Pattern for Modeling Multiple Inheritance

OBJECT ORIENTED PROGRAMMING LANGUAGE

Implementing Positioning Algorithms Using Accelerometers

Computer Science 483/580 Concurrent Programming Midterm Exam February 23, 2009

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Crash Course in Java

Graphical User Interfaces

Principles of Software Construction: Objects, Design, and Concurrency. Course Introduction. toad. toad Fall School of Computer Science

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

Java Memory Model: Content

Java Interview Questions and Answers

JAVA - MULTITHREADING

Interactive Programs and Graphics in Java

WEEK 2 DAY 14. Writing Java Applets and Java Web Start Applications

Contents. 9-1 Copyright (c) N. Afshartous

There are some important differences between an applet and a standalone Java application, including the following:

11. Applets, normal window applications, packaging and sharing your work

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Intel Retail Client Manager Audience Analytics

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Working Model 2D Exercise Problem ME 114 Vehicle Design Dr. Jose Granda. Performed By Jeffrey H. Cho

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Building Java Programs

Introduction to Java

Web Development and Core Java Lab Manual V th Semester

How to Convert an Application into an Applet.

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Programming with Java GUI components

LAB4 Making Classes and Objects

History OOP languages Year Language 1967 Simula Smalltalk

Block IQ. Marko Boon Jacques Resing

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

1 Hour, Closed Notes, Browser open to Java API docs is OK

Session 12 Evolving IT Architectures: From Mainframes to Client-Server to Network Computing

Outline of this lecture G52CON: Concepts of Concurrency

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Building a Multi-Threaded Web Server

Java Appletek II. Applet GUI

Using Image J to Measure the Brightness of Stars (Written by Do H. Kim)

Basic Object-Oriented Programming in Java

java.applet Reference

Chapter 8 Implementing FSP Models in Java

Chapter 1 Java Program Design and Development

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

J a v a Quiz (Unit 3, Test 0 Practice)

How To Write A Program For The Web In Java (Java)

Week 1: Review of Java Programming Basics

Extending Desktop Applications to the Web

public class demo1swing extends JFrame implements ActionListener{

GUI Components: Part 2

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Topic 11 Scanner object, conditional execution

Collections.sort(population); // Método de ordenamiento

Interaction: Mouse and Keyboard DECO1012

Dev Articles 05/25/07 11:07:33

Java from a C perspective. Plan

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Sample CSE8A midterm Multiple Choice (circle one)

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Tutorial for Tracker and Supporting Software By David Chandler

Tutorial: Getting Started

Transcription:

A very simple car game in Java

package cargame; CarGame.java import javax.swing.*; public class CarGame { public static void main(string[] args) { // Create application fram and a game surface JFrame frame = new JFrame("Car game"); frame.setdefaultcloseoperation(windowconstants.exit_on_close); GameSurface s = new GameSurface(); // Create a few cars and assign behaviours to them Car mycar = new Car("car.png"); mycar.updateposition(450, 450); mycar.setmass(1.0f); mycar.setmaxspeed(100.0f); mycar.setmaxsteering(70.0f); mycar.addbehaviour(new RoamBehaviour(100, 100, 300, 300)); Car mycar2 = new Car("car.png"); mycar2.updateposition(50, 50); mycar2.setmass(1.0f); mycar2.setmaxspeed(120.0f); mycar2.setmaxsteering(100.0f); mycar2.addbehaviour(new PursuitBehaviour(myCar)); mycar2.addbehaviour(new BounceOffWallsBehaviour(30, 30, 470, 470)); Car mycar3 = new Car("playercar.png"); mycar3.updateposition(250, 250); mycar3.setmass(1.0f); mycar3.setmaxspeed(120.0f); mycar3.setmaxsteering(300.0f); mycar3.updatevelocity(120.0f, 0.0f);

PlayerSteeringBehaviour steering = new PlayerSteeringBehaviour(); mycar3.addbehaviour(steering); mycar3.addbehaviour(new BounceOffWallsBehaviour(30, 30, 470, 470)); s.addkeylistener(steering); // Add the cars to the game surface so that // they will be drawn s.getvehicles().add(mycar); s.getvehicles().add(mycar2); s.getvehicles().add(mycar3); // Display the game surface in the frame, and // make the frame visible frame.setcontentpane(s); frame.setsize(500, 500); frame.setvisible(true); // Since we want to receive keyboard events, // the game surface needs to have the input focus s.requestfocusinwindow(); // Create the animation thread and start it AnimationSystem a = new AnimationSystem(s); Thread t = new Thread(a); t.start();

Vehicle.java package cargame; import java.awt.*; import java.awt.geom.*; import java.util.*; public abstract class Vehicle { // Member variables protected Point2D.Float position = new Point2D.Float(); protected Point2D.Float orientation = new Point2D.Float(); protected Point2D.Float side = new Point2D.Float(); protected Point2D.Float velocity = new Point2D.Float(); protected Point2D.Float steering = new Point2D.Float(); protected float mass; protected float maxspeed; protected float maxsteering; // List of behaviours protected ArrayList behaviours = new ArrayList(10); // Getters and setters public Point2D.Float getposition() { return position; public void updateposition(point2d.float p) { position.x = p.x; position.y = p.y; public void updateposition(float x, float y) { position.x = x; position.y = y; public Point2D.Float getorientation() { return orientation; public void updateorientation(point2d.float o) { orientation.x = o.x; orientation.y = o.y; public void updateorientation(float x, float y) { orientation.x = x; orientation.y = y; public Point2D.Float getsidevector() { return side;

public Point2D.Float getvelocity() { return velocity; public void updatevelocity(point2d.float v) { velocity.x = v.x; velocity.y = v.y; public void updatevelocity(float x, float y) { velocity.x = x; velocity.y = y; public Point2D.Float getsteering() { return steering; public void updatesteering(point2d.float s) { steering.x = s.x; steering.y = s.y; public void updatesteering(float x, float y) { steering.x = x; steering.y = y; public float getmass() { return mass; public void setmass(float m) { mass = m; public void setmaxspeed(float m) { maxspeed = m; public float getmaxspeed() { return maxspeed; public void setmaxsteering(float f) { maxsteering = f; public float getmaxsteering() { return maxsteering; public void addbehaviour(behaviour b) { behaviours.add(b); // A few utility methods for working with vectors static float length(point2d.float v) { return (float)math.sqrt((v.x * v.x) + (v.y * v.y)); static public void scale(point2d.float v, float newlength) { float l = length(v); v.x *= newlength / l; v.y *= newlength / l;

// Update this vehicle public void update(float dt) { for (int i = 0; i < behaviours.size(); i++) { ((Behaviour)behaviours.get(i)).update(this, dt); // Truncate the length of the desired steering force vector Point2D.Float force = new Point2D.Float(steering.x, steering.y); float l = length(force); if (l > maxsteering) { force.x *= maxsteering / l; force.y *= maxsteering / l; // Newton's second law: steering force = mass * accelerataion Point2D.Float acc = new Point2D.Float(force.x / mass, force.y / mass); // Update velocity vector using Euler's method // and truncate its length to the maximum allowed velocity.x += dt * acc.x; velocity.y += dt * acc.y; l = length(velocity); if (l > maxspeed) { velocity.x *= maxspeed / l; velocity.y *= maxspeed / l; // Update position using Euler's method position.x += dt * velocity.x; position.y += dt * velocity.y;

// Set orientation to equal the velocity vector // and set the side vector accordingly l = length(velocity); if (l > 0.0f) { orientation.x = velocity.x / l; orientation.y = velocity.y / l; side.x = -orientation.y; side.y = orientation.x; // Abstract methods for drawing and intersection testing public abstract void draw(graphics2d g2); public abstract boolean intersects(vehicle v);

Car.java package cargame; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class Car extends Vehicle implements ImageObserver { protected Image img; protected float w2; protected float h2; public Car(String imagefilename) { ImageIcon iic = new ImageIcon(imageFileName); img = Transparency.makeColorTransparent(iic.getImage(), Color.black); public void draw(graphics2d g2) { AffineTransform savexform = g2.gettransform(); g2.translate(position.x, position.y); g2.rotate(math.atan2(orientation.y, orientation.x)); g2.drawimage(img, AffineTransform.getTranslateInstance(-img.getWidth(this) / 2.0, -img.getheight(this) / 2.0), this); g2.settransform(savexform); /* g2.setpaint(color.yellow); g2.drawline((int)math.floor(position.x), (int)math.floor(position.y), (int)math.floor(position.x + 50.0f * side.x), (int)math.floor(position.y + 50.0f * side.y)); g2.setpaint(color.blue);

g2.drawline((int)math.floor(position.x), (int)math.floor(position.y), (int)math.floor(position.x + velocity.x), (int)math.floor(position.y + velocity.y)); g2.setpaint(color.white); g2.drawline((int)math.floor(position.x), (int)math.floor(position.y), (int)math.floor(position.x + steering.x), (int)math.floor(position.y + steering.y)); */ public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return true; public boolean intersects(vehicle v) { if (v instanceof Car) { Car c = (Car)v; Point2D.Float d = new Point2D.Float(position.x - c.position.x, position.y - c.position.y); if (length(d) < 25.0f) { // Should probably compute the radius from the images instead... return true; return false;

GameSurface.java package cargame; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GameSurface extends JComponent implements MouseListener { protected ArrayList vehicles; public GameSurface() { vehicles = new ArrayList(10); addmouselistener(this); // For requesting input focus public void paint(graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension d = getsize(); g2.setpaint(new Color(91, 91, 91)); g2.fillrect(0, 0, d.width, d.height); for (int i = 0; i < vehicles.size(); i++) { Vehicle v = (Vehicle)vehicles.get(i); v.draw(g2); ArrayList getvehicles() { return vehicles;

public void mouseclicked(mouseevent e) { // Custom components need to request the // input focus when they are clicked, // otherwise they won't receive keyboard events requestfocusinwindow(); public void mousemoved(mouseevent e) { public void mouseexited(mouseevent e) { public void mousereleased(mouseevent e) { public void mouseentered(mouseevent e) { public void mousepressed(mouseevent e) { public void mousedragged(mouseevent e) {

AnimationSystem.java package cargame; import java.util.*; public class AnimationSystem implements Runnable { protected GameSurface game; public AnimationSystem(GameSurface gamesurface) { game = gamesurface; public void run() { long time = System.currentTimeMillis(); for (;;) { ArrayList vehicles = game.getvehicles(); // Update position, velocity etc. of vehicles long t = System.currentTimeMillis(); long dt = t - time; float secs = (float)dt / 1000.0f; // Convert to seconds for (int i = 0; i < vehicles.size(); i++) { Vehicle v = (Vehicle)vehicles.get(i); v.update(secs); // Check for collisions for (int i = 0; i < vehicles.size(); i++) { for (int j = i + 1; j < vehicles.size(); j++) { Vehicle vi = (Vehicle)vehicles.get(i); Vehicle vj = (Vehicle)vehicles.get(j); if (vi.intersects(vj)) {

// Collision detected! // For now, simply reset the positions of the vehicles vi.updateposition(50, 50); vj.updateposition(450, 450); time = System.currentTimeMillis(); game.repaint(); // Sleep for a short amount of time to allow the system to catch up. // This improves framerate substantially and avoids hiccups try { Thread.sleep(20); catch (InterruptedException e) {

Behaviour.java package cargame; public abstract interface Behaviour { public abstract void update(vehicle v, float dt);

PursuitBehaviour.java package cargame; import java.awt.geom.point2d; public class PursuitBehaviour implements Behaviour { protected Vehicle target; PursuitBehaviour(Vehicle v) { target = v; public void update(vehicle v, float dt) { // Steer vehicle towards target Point2D.Float p = v.getposition(); Point2D.Float tp = target.getposition(); Point2D.Float desired_velocity = new Point2D.Float(tp.x - p.x, tp.y - p.y); Vehicle.scale(desired_velocity, v.getmaxspeed()); v.updatesteering(desired_velocity.x, desired_velocity.y);

RoamBehaviour.java package cargame; import java.awt.geom.point2d; public class RoamBehaviour implements Behaviour { protected long lasttargetupdate; protected Point2D.Float target = new Point2D.Float(); protected float width; protected float height; protected float x; protected float y; public RoamBehaviour(float x, float y, float width, float height) { this.x = x; this.y = y; this.width = width; this.height = height; public void update(vehicle v, float dt) { // Update target if necessary long time = System.currentTimeMillis(); if (time - lasttargetupdate > 5000) { target.x = x + (float)math.random() * width; target.y = y + (float)math.random() * height; lasttargetupdate = time; // Steer vehicle towards target Point2D.Float p = v.getposition(); Point2D.Float desired_velocity = new Point2D.Float(target.x - p.x, target.y - p.y);

Vehicle.scale(desired_velocity, v.getmaxspeed()); v.updatesteering(desired_velocity.x, desired_velocity.y);

BounceOffWallsBehaviour.java package cargame; import java.awt.geom.point2d; public class BounceOffWallsBehaviour implements Behaviour { protected float x1; protected float y1; protected float x2; protected float y2; public BounceOffWallsBehaviour(float x1, float y1, float x2, float y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; public void update(vehicle v, float dt) { Point2D.Float p = v.getposition(); Point2D.Float vel = v.getvelocity(); if (p.x < x1) { p.x = x1; vel.x = -vel.x; if (p.x > x2) { p.x = x2; vel.x = -vel.x; if (p.y < y1) { p.y = y1; vel.y = -vel.y; if (p.y > y2) {

p.y = y2; vel.y = -vel.y;

PlayerSteeringBehaviour.java package cargame; import java.awt.event.*; import java.awt.geom.point2d; public class PlayerSteeringBehaviour implements Behaviour, KeyListener { protected boolean steering = false; protected float direction = 1.0f; public void keypressed(keyevent e) { if (e.getkeycode() == 37) { // Cursor left steering = true; direction = -1.0f; if (e.getkeycode() == 39) { // Cursor right steering = true; direction = 1.0f; public void keyreleased(keyevent e) { steering = false; public void keytyped(keyevent e) { public void update(vehicle v, float dt) { if (steering) { Point2D.Float side = v.getsidevector(); Point2D.Float desired_velocity = new Point2D.Float(side.x * direction, side.y * direction); Vehicle.scale(desired_velocity, v.getmaxsteering()); v.updatesteering(desired_velocity.x, desired_velocity.y);

else { v.updatesteering(v.getvelocity());

Transparency.java package cargame; import java.awt.*; import java.awt.image.*; public class Transparency { public static Image makecolortransparent(image im, final Color color) { ImageFilter filter = new RGBImageFilter() { public int markerrgb = color.getrgb() 0xFF000000; public final int filterrgb(int x, int y, int rgb) { if ( ( rgb 0xFF000000 ) == markerrgb ) { return 0x00FFFFFF & rgb; else { return rgb; ; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip);