Since the applets no longer work. I have decided to stop programming in Java. I never really gotten used to Java.
I will keep programming but in another language. I was thinking of buying Play Basic but for the moment I do my programming exercises in Blitz Basic.
(edit:)
I think the google site is returning errorous data from the applets. Maybe this will go away.
Learning how to program/code video games step by step in Java. Platformers, Shooters, Turn Based Strategy, Real Time Strategy.
dinsdag 19 juni 2012
maandag 11 juni 2012
Java applets do not work anymore on my pc
I have tried firefox, internet explorer and google chrome but the applets do not show anymore. Also the browsers freeze when I load the pages. This happened before but not with all the browsers. I have no idea what I can do to fix it. I will try to look up what the problem might be.
edit :
I could not find any information yet. I have also tried reinstalling the java runtime. This did not fix it. I also tried another computer and this gives the same error. Also older applet that used to work did not work anymore. I get a magic number error with the applets when they do load. I hope this problem will fix itself in the future. As I have no idea what to do.
edit :
I could not find any information yet. I have also tried reinstalling the java runtime. This did not fix it. I also tried another computer and this gives the same error. Also older applet that used to work did not work anymore. I get a magic number error with the applets when they do load. I hope this problem will fix itself in the future. As I have no idea what to do.
Manhattan Distance example
Manhattan distance example. Move the mouse over the applet to see the distance change.
import java.awt.*;
import java.applet.*;
public class manhattandistanceexample001 extends Applet implements Runnable {
// Graphics for double buffering.
Graphics bufferGraphics;
Image offscreen;
int mousex = 0;
int mousey = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public boolean mouseMove(Event e, int x, int y){
mousex = x;
mousey = y;
return true;
}
public void update(Graphics g){
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().width );
bufferGraphics.setColor( Color.red );
bufferGraphics.drawString( "Manhattan Distance " , 10 , 10 );
for ( int y = 0 ; y < 240/32 ; y++ ){
for ( int x = 0 ; x < 320/32 ; x++ ){
bufferGraphics.drawRect( x * 32 , y * 32 , 32 , 32 );
int dist = mdist( mousex / 32 , mousey / 32 , x , y );
bufferGraphics.drawString( "" + dist , x * 32 , y * 32 + 16 );
}
}
g.drawImage(offscreen,0,0,this);
}
public int mdist( int x1 , int y1 , int x2 , int y2 ){
return Math.abs( x2 - x1 ) + Math.abs( y2 - y1 );
}
}
Euclidean Distance example
Euclidean distance example. Move the mouse over the applet to see the distance change.
import java.awt.*;
import java.applet.*;
public class euclideandistanceexample001 extends Applet implements Runnable {
// Graphics for double buffering.
Graphics bufferGraphics;
Image offscreen;
int mousex = 0;
int mousey = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public boolean mouseMove(Event e, int x, int y){
mousex = x;
mousey = y;
return true;
}
public void update(Graphics g){
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().width );
bufferGraphics.setColor( Color.red );
bufferGraphics.drawString( "Euclidean Distance " , 10 , 10 );
for ( int y = 0 ; y < 240/32 ; y++ ){
for ( int x = 0 ; x < 320/32 ; x++ ){
bufferGraphics.drawRect( x * 32 , y * 32 , 32 , 32 );
int dist = edist( mousex / 32 , mousey / 32 , x , y );
bufferGraphics.drawString( "" + dist , x * 32 , y * 32 + 16 );
}
}
g.drawImage(offscreen,0,0,this);
}
public int edist( int x1 , int y1 , int x2 , int y2 ){
return (int)Math.sqrt( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) );
}
}
zaterdag 2 juni 2012
The blog still works.
It looks like the email I got from blogger support was wrong. The blog still works. I have 2 examples which I still need to place on the weblog. These are distance formula's examples. I am also stil thinking of what to program next. Though it is summer and I spend a lot of time outside. I have not programmed a lot. But I am reading programming books. I have an old Java 2 book which I am curently reading.
dinsdag 15 mei 2012
This blog might be blocked starting 30 may 2012
I got a reminder in the email that I need to transfer the blogger account into my google account. But my blogger username is not found on the conversion page. This means that my blog might stop after may 30th and that I need to start a new one. I have no idea what the login problem might be. I have a blogger username so why it cannot be found is a mystery to me.
Well if this is one of the last posts then I might have started a new blog about game programming. I might call it about the same name. Java game programming step by step or so.
Edit : I have replied to the support email of the notice that I can not converse so I hope to hear something soon. I hope this works.
Edit2: I did not get a reply from the support email adres of blogger but I did look into the support forum. There was a post about my problem and it said not to worry about this. You automatically are converted to the new method if you used the blog after the new license.
Well if this is one of the last posts then I might have started a new blog about game programming. I might call it about the same name. Java game programming step by step or so.
Edit : I have replied to the support email of the notice that I can not converse so I hope to hear something soon. I hope this works.
Edit2: I did not get a reply from the support email adres of blogger but I did look into the support forum. There was a post about my problem and it said not to worry about this. You automatically are converted to the new method if you used the blog after the new license.
donderdag 19 april 2012
Game Menu Example
Use the s and w keys to move the selection cursor.
import java.awt.*;
import java.applet.*;
public class gamemenuexample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
FontMetrics fm;
int nummenuitems = 4;
String gamemenu[] = new String[ nummenuitems ];
int menuchoice = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
Graphics g;
g = getGraphics();
fm = g.getFontMetrics();
gamemenu[ 0 ] = "New Game";
gamemenu[ 1 ] = "Load Game";
gamemenu[ 2 ] = "Options";
gamemenu[ 3 ] = "Credits";
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
bufferGraphics.setColor(Color.red);
String s = "";
for ( int i = 0 ; i < nummenuitems ; i++ ){
if ( i == menuchoice ){
s = "-> " + gamemenu[ i ];
bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80 + i * 20 );
}else{
s = gamemenu[ i ];
bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80 + i * 20 );
}
}
bufferGraphics.drawString("Game Menu Screen.",10,10);
bufferGraphics.drawString("w/s to move cursor.",10,237);
g.drawImage(offscreen,0,0,this);
}
public boolean keyDown (Event e, int key){
return true;
}
public boolean keyUp (Event e, int key){
// key w
if ( key == 119 ){
if ( menuchoice > 0 ){
menuchoice--;
}
}
// key s
if ( key == 115 ){
if ( menuchoice < nummenuitems - 1 ){
menuchoice++;
}
}
//System.out.println(""+key);
return true;
}
}
Topdown patrolling ai example
import java.awt.*;
import java.applet.*;
public class topdownpatrollingaiexample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
boolean ismovingleft;
boolean ismovingright;
boolean ismovingup;
boolean ismovingdown;
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
private int map[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
{1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
{1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int playerstartpositionx = playerx = 10*cellwidth;
int playerstartpositiony = playery = 13*cellheight;
int playerx = playerstartpositionx;
int playery = playerstartpositiony;
// For the pattern movement ai
int numai = 10;
int ai[][] = new int[ numai ][ 10 ]; // ,active,x,y,direction[0-up,1-right,2-down,3-left]
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// initiate ai
ai[ 0 ][ 0 ] = 1;
ai[ 0 ][ 1 ] = 1 * cellwidth;
ai[ 0 ][ 2 ] = 1 * cellheight;
ai[ 0 ][ 3 ] = 1;
//6 2
ai[ 1 ][ 0 ] = 1;
ai[ 1 ][ 1 ] = 6 * cellwidth;
ai[ 1 ][ 2 ] = 2 * cellheight;
ai[ 1 ][ 3 ] = 2;
// 13 10
ai[ 2 ][ 0 ] = 1;
ai[ 2 ][ 1 ] = 13 * cellwidth;
ai[ 2 ][ 2 ] = 10 * cellheight;
ai[ 2 ][ 3 ] = 2;
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
moveplayer();
updateai();
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public void updateai(){
for ( int i = 0 ; i < numai ; i++ ){
if ( ai[ i ][ 0 ] == 1 ){
// Movement up ( 0 )
if ( ai[ i ][ 3 ] == 0 ){
if ( ismapcollision( ai[ i ][ 1 ] , ai[ i ][ 2 ] - 1 ) ){
ai[ i ][ 3 ] = 2;
}else{
ai[ i ][ 2 ]--;
}
}
// Movement right ( 1 )
if ( ai[ i ][ 3 ] == 1 ){
if ( ismapcollision( ai[ i ][ 1 ] + 1 , ai[ i ][ 2 ] ) ){
ai[ i ][ 3 ] = 3;
}else{
ai[ i ][ 1 ]++;
}
}
// Movement down ( 2 )
if ( ai[ i ][ 3 ] == 2 ){
if ( ismapcollision( ai[ i ][ 1 ] , ai[ i ][ 2 ] + 1 ) ){
ai[ i ][ 3 ] = 0;
}else{
ai[ i ][ 2 ]++;
}
}
// Movement left ( 3 )
if ( ai[ i ][ 3 ] == 3 ){
if ( ismapcollision( ai[ i ][ 1 ] - 1 , ai[ i ][ 2 ] ) ){
ai[ i ][ 3 ] = 1;
}else{
ai[ i ][ 1 ]--;
}
}
// Collision with the ai
Rectangle rec1 = new Rectangle( playerx ,
playery ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( ai[ i ][ 1 ],
ai[ i ][ 2 ],
cellwidth,
cellheight);
if( rec1.intersects( rec2 ) ){
playerx = playerstartpositionx;
playery = playerstartpositiony;
}
}
}
}
public boolean ismapcollision(int x, int y){
int pcx = x / cellwidth;
int pcy = y / cellheight;
for (int y1 = pcy - 1 ; y1 < pcy + 2 ; y1++){
for (int x1 = pcx - 1 ; x1 < pcx + 2 ; x1++){
if( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x1 * cellwidth ,
y1 * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return true;
}
}
}
}
return false;
}
public void moveplayer(){
if (ismovingright == true && ismapcollision(playerx + 1,playery) == false){
playerx++;
}
if (ismovingup == true && ismapcollision(playerx,playery-1) == false){
playery--;
}
if (ismovingdown == true && ismapcollision(playerx,playery+1) == false){
playery++;
}
if (ismovingleft == true && ismapcollision(playerx-1,playery) == false){
playerx--;
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
// Draw map
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
// walls
if( map[y][x] == 1 ){
bufferGraphics.setColor( Color.white );
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
// Draw ai
for ( int i = 0 ; i < numai ; i++ ){
if ( ai[ i ][ 0 ] == 1 ){
bufferGraphics.setColor( Color.blue );
bufferGraphics.fillOval( ai[ i ][ 1 ] ,
ai[ i ][ 2 ] ,
cellwidth ,
cellheight );
}
}
bufferGraphics.setColor(Color.red);
bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);
bufferGraphics.drawString("2D Topdown Patrolling ai.",10,10);
bufferGraphics.drawString("w/s/a/d to move player.",10,237);
g.drawImage(offscreen,0,0,this);
}
public boolean keyDown (Event e, int key){
if(key==97)
{
ismovingleft = true;
}
if(key==100)
{
ismovingright = true;
}
if(key==119)
{
ismovingup = true;
}
if(key==115)
{
ismovingdown = true;
}
return true;
}
public boolean keyUp (Event e, int key){
if(key==97)
{
ismovingleft = false;
}
if(key==100)
{
ismovingright = false;
}
if(key==119)
{
ismovingup = false;
}
if(key==115)
{
ismovingdown = false;
}
// System.out.println(""+key);
return true;
}
}
Topdown Portals Example
Portals example. Move into the yellow blocks to switch levels.
import java.awt.*;
import java.applet.*;
public class topdownportalsexample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
boolean ismovingleft;
boolean ismovingright;
boolean ismovingup;
boolean ismovingdown;
int isonmap = 1;
boolean isonportal = false;
long portaltimeout = 0;
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
private int map[][] = new int[ mapheight ][ mapwidth ];
private int map2[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
{1,0,1,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
private int map1[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
{1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
{1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int playerx = 10*cellwidth;
int playery = 13*cellheight;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// Copy map1 to map so that we have something to show
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
map[ y ][ x ] = map1[ y ][ x ];
}
}
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
moveplayer();
updateportals();
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public void updateportals(){
if ( isportalcollision( playerx , playery ) == true ){
boolean selected = false;
if ( isonmap == 1 ){
selected = true;
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
map[ y ][ x ] = map2[ y ][ x ];
}
}
isonmap = 2;
}
if ( isonmap == 2 && selected == false ){
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
map[ y ][ x ] = map1[ y ][ x ];
}
}
isonmap = 1;
}
}
}
public boolean isportalcollision( int x , int y ){
int pcx = x / cellwidth;
int pcy = y / cellheight;
for (int y1 = pcy - 1 ; y1 < pcy + 2 ; y1++){
for (int x1 = pcx - 1 ; x1 < pcx + 2 ; x1++){
if( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 2 ){
Rectangle rec1 = new Rectangle( x1 * cellwidth ,
y1 * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return true;
}
}
}
}
return false;
}
public boolean ismapcollision(int x, int y){
int pcx = x / cellwidth;
int pcy = y / cellheight;
for (int y1 = pcy - 1 ; y1 < pcy + 2 ; y1++){
for (int x1 = pcx - 1 ; x1 < pcx + 2 ; x1++){
if( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x1 * cellwidth ,
y1 * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return true;
}
}
}
}
return false;
}
public void moveplayer(){
if (ismovingright == true && ismapcollision(playerx + 1,playery) == false){
playerx++;
}
if (ismovingup == true && ismapcollision(playerx,playery-1) == false){
playery--;
}
if (ismovingdown == true && ismapcollision(playerx,playery+1) == false){
playery++;
}
if (ismovingleft == true && ismapcollision(playerx-1,playery) == false){
playerx--;
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
// walls
if( map[y][x] == 1 ){
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
// portals
if( map[ y ][ x ] == 2 ){
bufferGraphics.setColor(Color.yellow);
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
bufferGraphics.setColor(Color.red);
bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);
bufferGraphics.drawString("2D Topdown Portals.",10,10);
bufferGraphics.drawString("w/s/a/d to move player.",10,237);
g.drawImage(offscreen,0,0,this);
}
public boolean keyDown (Event e, int key){
if(key==97)
{
ismovingleft = true;
}
if(key==100)
{
ismovingright = true;
}
if(key==119)
{
ismovingup = true;
}
if(key==115)
{
ismovingdown = true;
}
return true;
}
public boolean keyUp (Event e, int key){
if(key==97)
{
ismovingleft = false;
}
if(key==100)
{
ismovingright = false;
}
if(key==119)
{
ismovingup = false;
}
if(key==115)
{
ismovingdown = false;
}
// System.out.println(""+key);
return true;
}
}
zaterdag 7 april 2012
Platformer Time Dissapearing Tiles Example
In this example you can move the player across tiles that dissapear after 2 seconds after they have been touched.
import java.awt.*;
import java.applet.*;
public class tdtilesexample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
private int map[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,1,1,1},
{1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,2,2,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
double px = 132;
double py = 200;
int pwidth = cellwidth/2;
int pheight = cellheight;
boolean isjumping = false;
boolean isfalling = false;
double gravity = 0;
boolean ismovingright = false;
boolean ismovingleft = false;
double jumpforce = 3;
int numdoors = 10;
int numtdt = 32;
int[][] tdt = new int[ numtdt ][ 3 ]; // active , x , y , timeout
long[] tdttimeout = new long[ numtdt ];
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// read time dissapearing tiles
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
if ( map[ y ][ x ] == 2 ){
int n = freetdt();
tdt[ n ][ 0 ] = 1;
tdt[ n ][ 1 ] = x;
tdt[ n ][ 2 ] = y;
tdttimeout[ n ] = -1;
map[ y ][ x ] = 0;
}
}
}
new Thread(this).start();
}
public int freetdt(){
for ( int i = 0 ; i < numtdt ; i++ ){
if ( tdt[ i ][ 0 ] == 0 ){
return i;
}
}
return -1;
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
updateplayer();
updatetdt();
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
// Draw map
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
if( map[y][x] == 1 ){
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
// Draw time dissapearing tiles
bufferGraphics.setColor(Color.yellow);
for ( int i = 0 ; i < numtdt ; i++ ){
if ( tdt[ i ][ 0 ] == 1 ){
bufferGraphics.fillRect( tdt[ i ][ 1 ] * cellwidth ,
tdt[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
}
}
// Draw player
bufferGraphics.setColor(Color.red);
bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString("Platformer Time Dissapearing tiles Example.",10,10);
bufferGraphics.drawString("a - left, d - right, space - jump.",10,240);
g.drawImage(offscreen,0,0,this);
}
public void updatetdt(){
for ( int i = 0 ; i < numtdt ; i++ ){
if ( tdt[ i ][ 0 ] == 1 ){
if ( tdtcollision( (int)px , (int)py + 1 ) == i ){
tdttimeout[ i ] = System.currentTimeMillis() + 2000;
}
}
}
for ( int i = 0 ; i < numtdt ; i++ ){
if ( tdttimeout[ i ] > -1 ){
if ( tdttimeout[ i ] < System.currentTimeMillis() ){
tdttimeout[ i ] = -1;
tdt[ i ][ 0 ] = -1;
}
}
}
}
public void updateplayer(){
if ( isjumping == false && isfalling == false ){
if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false &&
tdtcollision( (int)px , (int)py + 1 ) == -1 ){
isfalling = true;
gravity = 0;
}
}
if (ismovingright){
if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false &&
tdtcollision( (int)px + 1 , (int)py ) == -1 ){
px += 1;
}
}
if (ismovingleft){
if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false &&
tdtcollision( (int)px - 1 , (int)py ) == -1 ){
px -= 1;
}
}
if ( isfalling == true && isjumping == false ){
for ( int i = 0 ; i < gravity ; i++ ){
if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false &&
tdtcollision( (int)px , (int)py + 1) == -1 ){
py += 1;
}else{
gravity = 0;
isfalling = false;
}
}
gravity += .1;
}
if ( isjumping == true && isfalling == false ){
for ( int i = 0 ; i < gravity ; i++){
if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false &&
tdtcollision( (int)px , (int)py - 1 ) == -1 ){
py -= 1;
//System.out.print("still jumping : " + gravity);
}else{
gravity = 0;
isfalling = true;
isjumping = false;
}
}
if( gravity < 1 ) {
gravity = 0;
isfalling = true;
isjumping = false;
}
gravity -= .1;
}
}
public int tdtcollision( int x , int y ){
for ( int i = 0 ; i < numtdt ; i++ ){
if ( tdt[ i ][ 0 ] == 1 ){
Rectangle rec1 = new Rectangle( x , y , pwidth , pheight );
Rectangle rec2 = new Rectangle( tdt[ i ][ 1 ] * cellwidth,
tdt[ i ][ 2 ] * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return i;
}
}
return -1;
}
public boolean mapcollision( int x , int y , int width , int height ){
int mapx = x / cellwidth;
int mapy = y / cellheight;
for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x , y , width , height );
Rectangle rec2 = new Rectangle( x1 * cellwidth,
y1 * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return true;
}
}
}
}
return false;
}
public boolean keyDown (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = true;
}
if(key== 100) // d key
{
ismovingright = true;
}
if( key == 32 ) // space bar for jump
{
if( isfalling == false && isjumping == false )
{
isjumping = true;
gravity = jumpforce;
}
}
System.out.println (" Integer Value: " + key);
return true;
}
public boolean keyUp (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = false;
}
if( key == 100 ) // d key
{
ismovingright = false;
}
return true;
}
}
donderdag 5 april 2012
2d Topdown Doors and keys Example
In this example there is a map where you can grab the yellow keys and open the blue doors. The controls hints are shown in the applet window. The sourcecode is below.
import java.awt.*;
import java.applet.*;
public class topdowndoorsandkeysexample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
boolean ismovingleft;
boolean ismovingright;
boolean ismovingup;
boolean ismovingdown;
private int map[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1},
{1,0,1,1,1,1,0,1,1,1,0,0,1,1,1,0,1,1,0,1},
{1,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1},
{1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,0,1,0,1},
{1,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1},
{1,0,1,0,2,0,0,1,0,2,0,0,1,0,2,0,0,1,0,1},
{1,0,1,1,1,0,0,1,1,1,0,1,1,1,1,0,0,0,0,1},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
{1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
{1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
{1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
{1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
int pwidth = 16;
int pheight = 16;
int playerx = 10*cellwidth;
int playery = 13*cellheight;
int numkeys = 4;
int[][] keys = new int[ numkeys ][ 3 ]; // active, x , y
int numdoors = 4;
int[][] doors = new int[ numdoors ][ 3 ]; // active, x , y
int playerkeys = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
readkeysanddoors();
new Thread(this).start();
}
public void readkeysanddoors(){
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
if ( map[ y ][ x ] == 2 ){ // if door
int n = findfreedoor();
doors[ n ][ 0 ] = 1;
doors[ n ][ 1 ] = x;
doors[ n ][ 2 ] = y;
map[ y ][ x ] = 0;
}
if ( map[ y ][ x ] == 3 ){ // if key
int n = findfreekey();
keys[ n ][ 0 ] = 1;
keys[ n ][ 1 ] = x;
keys[ n ][ 2 ] = y;
map[ y ][ x ] = 0;
}
}
}
}
public int findfreedoor(){
for ( int i = 0 ; i < numdoors ; i++ ){
if ( doors[ i ][ 0 ] == 0 ){
return i;
}
}
return -1;
}
public int findfreekey(){
for ( int i = 0 ; i < numkeys ; i++ ){
if ( keys[ i ][ 0 ] == 0 ){
return i;
}
}
return -1;
}
public void run() {
for(;;) { // animation loop never ends
moveplayer();
playerdoors();
playerkeys();
repaint();
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
}
}
}
public int isdoorcollision( int x , int y ){
for ( int i = 0 ; i < numdoors ; i++ ){
if ( doors[ i ][ 0 ] == 1 ){
Rectangle rec1 = new Rectangle( doors[ i ][ 1 ] * cellwidth ,
doors[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return i;
}
}
return -1; }
public int iskeycollision( int x , int y ){
for ( int i = 0 ; i < numkeys ; i++ ){
if ( keys[ i ][ 0 ] == 1 ){
Rectangle rec1 = new Rectangle( keys[ i ][ 1 ] * cellwidth ,
keys[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return i;
}
}
return -1;
}
public boolean ismapcollision(int x, int y){
int pcx = x / cellwidth;
int pcy = y / cellheight;
for (int y1 = pcy - 1 ; y1 < pcy + 2 ; y1++){
for (int x1 = pcx - 1 ; x1 < pcx + 2 ; x1++){
if( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x1 * cellwidth ,
y1 * cellheight ,
cellwidth ,
cellheight );
Rectangle rec2 = new Rectangle( x,
y,
cellwidth,
cellheight);
if(rec1.intersects(rec2)) return true;
}
}
}
}
return false;
}
public void playerkeys(){
if ( ismovingleft ){
int thekey = iskeycollision( playerx - 1 , playery );
if ( thekey > -1 ){
playerkeys++;
keys[ thekey ][ 0 ] = 0;
}
}
if ( ismovingright ){
int thekey = iskeycollision( playerx + 1 , playery );
if ( thekey > -1 ){
playerkeys++;
keys[ thekey ][ 0 ] = 0;
}
}
if ( ismovingup ){
int thekey = iskeycollision( playerx , playery - 1);
if ( thekey > -1 ){
playerkeys++;
keys[ thekey ][ 0 ] = 0;
}
}
if ( ismovingdown ){
int thekey = iskeycollision( playerx , playery + 1 );
if ( thekey > -1 ){
playerkeys++;
keys[ thekey ][ 0 ] = 0;
}
}
}
public void playerdoors(){
if ( ismovingleft == true ){
if ( playerkeys > 0 ){
int thedoor = isdoorcollision( playerx - 1 , playery );
if ( thedoor > -1 ){
playerkeys--;
doors[ thedoor ][ 0 ] = 0;
}
}
}
if ( ismovingright == true ){
if ( playerkeys > 0 ){
int thedoor = isdoorcollision( playerx + 1 , playery );
if ( thedoor > -1 ){
playerkeys--;
doors[ thedoor ][ 0 ] = 0;
}
}
}
if ( ismovingup == true ){
if ( playerkeys > 0 ){
int thedoor = isdoorcollision( playerx , playery - 1 );
if ( thedoor > -1 ){
playerkeys--;
doors[ thedoor ][ 0 ] = 0;
}
}
}
if ( ismovingdown == true ){
if ( playerkeys > 0 ){
int thedoor = isdoorcollision( playerx , playery + 1 );
if ( thedoor > -1 ){
playerkeys--;
doors[ thedoor ][ 0 ] = 0;
}
}
}
}
public void moveplayer(){
if ( ismovingright == true &&
ismapcollision( playerx + 1 , playery ) == false &&
isdoorcollision( playerx + 1 , playery ) < 0 ){
playerx++;
}
if ( ismovingup == true &&
ismapcollision( playerx , playery - 1 ) == false &&
isdoorcollision( playerx , playery - 1 ) < 0 ){
playery--;
}
if ( ismovingdown == true &&
ismapcollision( playerx , playery + 1 ) == false &&
isdoorcollision( playerx , playery + 1 ) < 0 ){
playery++;
}
if ( ismovingleft == true &&
ismapcollision( playerx - 1 , playery ) == false &&
isdoorcollision( playerx - 1 , playery ) < 0 ){
playerx--;
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
// Draw map
bufferGraphics.setColor(Color.white);
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
if( map[y][x] == 1 ){
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
// Draw doors
bufferGraphics.setColor( Color.blue );
for ( int i = 0 ; i < numdoors ; i++ ){
if ( doors[ i ][ 0 ] == 1 ){
bufferGraphics.fillRect( doors[ i ][ 1 ] * cellwidth ,
doors[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
}
}
// Draw keys
bufferGraphics.setColor( Color.yellow );
for ( int i = 0 ; i < numkeys ; i++ ){
if ( keys[ i ][ 0 ] == 1 ){
bufferGraphics.fillOval( keys[ i ][ 1 ] * cellwidth ,
keys[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
}
}
// Draw player
bufferGraphics.setColor(Color.red);
bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);
bufferGraphics.drawString("2D Topdown Doors and keys.",10,10);
bufferGraphics.drawString("Keys : " + playerkeys , 200 , 10 );
bufferGraphics.drawString("w/s/a/d = movement, yellow = key, blue = door.",10,237);
g.drawImage(offscreen,0,0,this);
}
public boolean keyDown (Event e, int key){
if(key==97)
{
ismovingleft = true;
}
if(key==100)
{
ismovingright = true;
}
if(key==119)
{
ismovingup = true;
}
if(key==115)
{
ismovingdown = true;
}
return true;
}
public boolean keyUp (Event e, int key){
if(key==97)
{
ismovingleft = false;
}
if(key==100)
{
ismovingright = false;
}
if(key==119)
{
ismovingup = false;
}
if(key==115)
{
ismovingdown = false;
}
// System.out.println(""+key);
return true;
}
}
Platformer Doors Example
Platformer Doors Example. The instructions are shown in the applet window. The yellow blocks are the doors. Sourcecode is below.
import java.awt.*;
import java.applet.*;
public class platformerdoorsexample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
private int map[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1},
{1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
double px = 132;
double py = 200;
int pwidth = cellwidth/2;
int pheight = cellheight;
boolean isjumping = false;
boolean isfalling = false;
double gravity = 0;
boolean ismovingright = false;
boolean ismovingleft = false;
double jumpforce = 3;
int numdoors = 10;
private int[][] door = new int[ numdoors ][ 4 ]; // door ] active , x , y , teleport_to
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// Door 1
door[ 0 ][ 0 ] = 1;
door[ 0 ][ 1 ] = 14;
door[ 0 ][ 2 ] = 13;
door[ 0 ][ 3 ] = 1;
// Door 2
door[ 1 ][ 0 ] = 1;
door[ 1 ][ 1 ] = 15;
door[ 1 ][ 2 ] = 10;
door[ 1 ][ 3 ] = 0;
// Door 3
door[ 2 ][ 0 ] = 1;
door[ 2 ][ 1 ] = 10;
door[ 2 ][ 2 ] = 10;
door[ 2 ][ 3 ] = 3;
// Door 4
door[ 3 ][ 0 ] = 1;
door[ 3 ][ 1 ] = 14;
door[ 3 ][ 2 ] = 3;
door[ 3 ][ 3 ] = 2;
//
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
updateplayer();
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
// Draw map
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
if( map[y][x] == 1 ){
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
if( map[y][x] == 2 ){
bufferGraphics.setColor(Color.yellow);
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
// Draw doors
bufferGraphics.setColor(Color.yellow);
for ( int i = 0 ; i < numdoors ; i++ ){
if ( door[ i ][ 0 ] == 1 ){
bufferGraphics.fillRect( door[ i ][ 1 ] * cellwidth ,
door[ i ][ 2 ] * cellheight ,
cellwidth ,
cellheight );
}
}
// Draw player
bufferGraphics.setColor(Color.red);
bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString("Platformer Doors Example.",10,10);
bufferGraphics.drawString("a - left, d - right, space - jump, w - enter door.",10,240);
g.drawImage(offscreen,0,0,this);
}
public void updateplayer(){
if ( isjumping == false && isfalling == false ){
if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false ){
isfalling = true;
gravity = 0;
}
}
if (ismovingright){
if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){
px += 1;
}
}
if (ismovingleft){
if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
px -= 1;
}
}
if ( isfalling == true && isjumping == false ){
for ( int i = 0 ; i < gravity ; i++ ){
if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){
py += 1;
}else{
gravity = 0;
isfalling = false;
}
}
gravity += .1;
}
if ( isjumping == true && isfalling == false ){
for ( int i = 0 ; i < gravity ; i++){
if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
py -= 1;
//System.out.print("still jumping : " + gravity);
}else{
gravity = 0;
isfalling = true;
isjumping = false;
}
}
if( gravity < 1 ) {
gravity = 0;
isfalling = true;
isjumping = false;
}
gravity -= .1;
}
}
public boolean mapcollision( int x , int y , int width , int height ){
int mapx = x / cellwidth;
int mapy = y / cellheight;
for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x , y , width , height );
Rectangle rec2 = new Rectangle( x1 * cellwidth,
y1 * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return true;
}
}
}
}
return false;
}
public Integer doorcollision( int x , int y , int w , int h ){
int returnvalue = -1;
for ( int i = 0 ; i < numdoors ; i++ ){
if ( door[ i ][ 0 ] == 1 ){
Rectangle rec1 = new Rectangle( x , y , w , h );
Rectangle rec2 = new Rectangle( door[ i ][ 1 ] * cellwidth,
door[ i ][ 2 ] * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return i;
}
}
return returnvalue;
}
public boolean keyDown (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = true;
}
if(key== 100) // d key
{
ismovingright = true;
}
if( key == 32 ) // space bar for jump
{
if( isfalling == false && isjumping == false )
{
isjumping = true;
gravity = jumpforce;
}
}
System.out.println (" Integer Value: " + key);
return true;
}
public boolean keyUp (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = false;
}
if( key == 100 ) // d key
{
ismovingright = false;
}
if( key == 119 ) // w key
{
int thedoor = -1;
thedoor = doorcollision( (int)px , (int)py , pwidth , pheight );
if ( thedoor > -1 ){
int tdoor = door[ thedoor ][ 3 ];
px = door[ tdoor ][ 1 ] * cellwidth + cellwidth / 4;
py = door[ tdoor ][ 2 ] * cellheight;
}
}
return true;
}
}
woensdag 4 april 2012
Platformer Ladders Example
Use w s a d to control the player block. Move to the ladders and press w or s to move up or down.
import java.awt.*;
import java.applet.*;
public class platformerladdersexample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
private int map[][] = new int[][]{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,1,2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,2,1,1,1,1,1,1,1,2,1,0,0,1},
{1,0,0,0,0,0,0,2,0,0,0,0,1,1,1,2,1,0,0,1},
{1,0,0,0,1,1,1,2,1,0,0,0,0,0,0,2,0,0,0,1},
{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,1,1,1},
{1,1,1,2,1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1},
{1,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
int mapwidth = 20;
int mapheight = 15;
int cellwidth = 16;
int cellheight = 16;
double px = 132;
double py = 200;
int pwidth = cellwidth/2;
int pheight = cellheight;
boolean isjumping = false;
boolean isfalling = false;
double gravity = 0;
boolean ismovingright = false;
boolean ismovingleft = false;
boolean ismovingup = false;
boolean ismovingdown = false;
double jumpforce = 3;
boolean onladder = false;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
updateplayer();
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g) {
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
// Draw map
for( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++){
if( map[ y ][ x ] == 1 ){
bufferGraphics.setColor ( Color.white );
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
if( map[ y ][ x ] == 2 ){ // Draw ladder
bufferGraphics.setColor ( new Color( 170 , 130 , 0 ) );
bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , 3 );
bufferGraphics.fillRect( x * cellwidth , y * cellheight +6 , cellwidth , 3 );
bufferGraphics.fillRect( x * cellwidth , y * cellheight + 12 , cellwidth , 3 );
}
}
}
// Draw player
bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );
bufferGraphics.setColor ( Color.green );
bufferGraphics.drawString( "Platformer Ladders Eample." , 10 , 10 );
g.drawImage(offscreen,0,0,this);
}
public void updateplayer(){
boolean ontheladder = laddercollision( (int)px , (int)py , pwidth , pheight );
if ( ontheladder ) {
if ( ismovingup ) {
py--;
}
}
if ( laddercollision( (int)px , (int)py+pheight , pwidth , 1 ) ||
mapcollision( (int)px , (int)py+pheight , pwidth , pheight ) == false ) {
if ( ismovingdown ) {
py++;
}
}
if ( isjumping == false && isfalling == false && ontheladder == false ){
if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false ){
isfalling = true;
gravity = 0;
}
}
if (ismovingright){
if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){
px += 1;
}
}
if (ismovingleft){
if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
px -= 1;
}
}
if ( isfalling == true && isjumping == false ){
for ( int i = 0 ; i < gravity ; i++ ){
if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false &&
laddercollision( (int)px , (int)py+1 , pwidth , pheight ) == false ){
py += 1;
}else{
gravity = 0;
isfalling = false;
}
}
gravity += .1;
}
if ( isjumping == true && isfalling == false ){
for ( int i = 0 ; i < gravity ; i++){
if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
py -= 1;
//System.out.print("still jumping : " + gravity);
}else{
gravity = 0;
isfalling = true;
isjumping = false;
}
}
if( gravity < 1 ) {
gravity = 0;
isfalling = true;
isjumping = false;
}
gravity -= .1;
}
}
public boolean laddercollision( int x , int y , int width , int height ){
int mapx = x / cellwidth;
int mapy = y / cellheight;
for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 2 ){
Rectangle rec1 = new Rectangle( x , y , width , height );
Rectangle rec2 = new Rectangle( x1 * cellwidth,
y1 * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return true;
}
}
}
}
return false;
}
public boolean mapcollision( int x , int y , int width , int height ){
int mapx = x / cellwidth;
int mapy = y / cellheight;
for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
if ( map[y1][x1] == 1 ){
Rectangle rec1 = new Rectangle( x , y , width , height );
Rectangle rec2 = new Rectangle( x1 * cellwidth,
y1 * cellheight,
cellwidth,
cellheight);
if( rec1.intersects( rec2 )) return true;
}
}
}
}
return false;
}
public boolean keyDown (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = true;
}
if(key== 100) // d key
{
ismovingright = true;
}
if( key == 119 ) // w key
{
ismovingup = true;
}
if(key== 115) // s key
{
ismovingdown = true;
}
if( key == 32 ) // space bar for jump
{
if( isfalling == false && isjumping == false )
{
isjumping = true;
gravity = jumpforce;
}
}
System.out.println (" Integer Value: " + key);
return true;
}
public boolean keyUp (Event e, int key){
if( key == 97 ) // a key
{
ismovingleft = false;
}
if( key == 100 ) // d key
{
ismovingright = false;
}
if( key == 119 ) // w key
{
ismovingup = false;
}
if( key == 115 ) // s key
{
ismovingdown = false;
}
return true;
}
}
vrijdag 16 maart 2012
a* (aStar) pathfinding example
I finally made the a* in Java. I spend the last two weeks typing the a* algorithm in Blitz basic. I typed it in about 13 times before I decided to type it into Java. I think I did it without creating errors.
The source code is below.
Anyone may use the code for their own. No credit is required.
Edit : there is a little problem with the code. The arraylist setup lacks the <Integer> parts. You need to add those to the code if you want to run it. It was removed becourse the < characters are HTML codes.
import java.awt.*;
import java.applet.*;
import java.util.ArrayList;
public class astarpathfindingexample01 extends Applet {
int map[][] ={
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,1,0,1,0,1,0,0},
{0,0,0,1,0,1,0,1,0,0},
{0,1,1,1,0,0,0,1,0,0},
{0,0,0,1,0,1,0,0,0,0},
{0,0,0,1,0,1,0,1,0,0},
{0,1,1,1,1,1,0,1,0,0},
{0,0,0,0,0,1,0,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
};
int mapwidth = 9;
int mapheight = 9;
int cellwidth = 32;
int cellheight = 24;
int sx,sy,ex,ey;
// Open list ( x, y, f, g, h, parentx, parenty )
ArrayList olx = new ArrayList();
ArrayList oly = new ArrayList();
ArrayList olf = new ArrayList();
ArrayList olg = new ArrayList();
ArrayList olh = new ArrayList();
ArrayList olpx = new ArrayList();
ArrayList olpy = new ArrayList();
// Closed list ( x, y, f, g, h, parentx, parenty )
ArrayList clx = new ArrayList();
ArrayList cly = new ArrayList();
ArrayList clf = new ArrayList();
ArrayList clg = new ArrayList();
ArrayList clh = new ArrayList();
ArrayList clpx = new ArrayList();
ArrayList clpy = new ArrayList();
// Path
ArrayList px = new ArrayList();
ArrayList py = new ArrayList();
public void init() {
setBackground(Color.black);
}
public void findpathback(){
boolean exitloop = false;
int x = ex;
int y = ey;
while ( exitloop == false ){
for ( int i = 0 ; i < clx.size() ; i++ ){
if ( clx.get( i ) == x && cly.get( i ) == y ){
x = clpx.get( i );
y = clpy.get( i );
px.add( x );
py.add( y );
}
}
if ( x == sx && y == sy ) {
exitloop = true;
}
}
}
public boolean removefromopenlist( int x , int y ){
for ( int i = 0 ; i < olx.size() ; i++ ){
if ( olx.get(i) == x && oly.get(i) == y ){
olx.remove(i);
oly.remove(i);
olf.remove(i);
olg.remove(i);
olh.remove(i);
olpx.remove(i);
olpy.remove(i);
return true;
}
}
return false;
}
public boolean isonclosedlist( int x , int y ){
for ( int i = 0 ; i < clx.size() ; i++ ){
if ( clx.get(i) == x && cly.get(i) == y ) {
return true;
}
}
return false;
}
public boolean isonopenlist( int x , int y ){
for ( int i = 0 ; i < olx.size() ; i++){
if ( olx.get(i) == x && oly.get(i) == y ){
return true;
}
}
return false;
}
public boolean openlistisempty(){
if ( olx.size() > 0 ) {
return false;
}
return true;
}
public void setcoordinates(){
boolean exitloop = false;
while ( exitloop == false ){
sx = (int)( Math.random() * mapwidth );
sy = (int)( Math.random() * mapheight );
ex = (int)( Math.random() * mapwidth );
ey = (int)( Math.random() * mapheight );
if ( map[ sy ][ sx ] == 0 && map[ ey ][ ex ] == 0 ){
if ( sx != ex && sy != ey ){
exitloop = true;
}
}
}
}
public void findpath(){
// Clear all the pathfinding data
olx.clear();
oly.clear();
olf.clear();
olg.clear();
olh.clear();
olpx.clear();
olpy.clear();
//
clx.clear();
cly.clear();
clf.clear();
clg.clear();
clh.clear();
clpx.clear();
clpy.clear();
//
px.clear();
py.clear();
//
// Move the start position onto the open list
olx.add( sx );
oly.add( sy );
olf.add( 0 );
olg.add( 0 );
olh.add( 0 );
olpx.add( 0 );
olpy.add( 0 );
//
boolean exitloop = false;
int tx = 0;
int ty = 0;
int tf = 0;
int tg = 0;
int th = 0;
int tpx = 0;
int tpy = 0;
int newx = 0;
int newy = 0;
int lowestf = 0;
while ( exitloop == false ){
// If the open list is empty then exit loop
if ( openlistisempty() == true ){
exitloop = true;
}
// Get the lowest f value position from the
// open list and use that.
lowestf = 100000;
for ( int i = 0 ; i < olx.size() ; i++ ){
if ( olf.get( i ) < lowestf ) {
lowestf = olf.get( i );
tx = olx.get( i );
ty = oly.get( i );
tf = olf.get( i );
tg = olg.get( i );
th = olh.get( i );
tpx = olpx.get( i );
tpy = olpy.get( i );
}
}
// if the current position is the end position then
// path was found.
if ( tx == ex && ty == ey ){
exitloop = true;
clx.add( tx );
cly.add( ty );
clf.add( tf );
clg.add( tg );
clh.add( th );
clpx.add( tpx );
clpy.add( tpy );
findpathback();
}else{
// Move the current position onto the closed list
clx.add( tx );
cly.add( ty );
clf.add( tf );
clg.add( tg );
clh.add( th );
clpx.add( tpx );
clpy.add( tpy );
// Remove the current position from the open is
removefromopenlist( tx , ty );
// Get the eight positions from around the current
// position and move them onto the open list.
//
newx = tx - 1;
newy = ty - 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx;
newy = ty - 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx + 1;
newy = ty - 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx - 1;
newy = ty;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx + 1;
newy = ty;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx - 1;
newy = ty + 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx;
newy = ty + 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
//
newx = tx + 1;
newy = ty + 1;
if ( newx > -1 && newy > -1 && newx < mapwidth + 1 && newy < mapheight + 1 ){
if ( isonopenlist( newx , newy ) == false ){
if ( isonclosedlist( newx , newy ) == false ){
if ( map[ newy ][ newx ] == 0 ){
olx.add( newx );
oly.add( newy );
olg.add( tg + 1 );
olh.add( distance( newx , newy , ex , ey ));
olf.add( ( tg + 1 ) + distance( newx , newy , ex , ey ) );
olpx.add( tx );
olpy.add( ty );
}
}
}
}
}
}
}
public int distance( int x1 , int y1 , int x2 , int y2 ){
int distance=(int)Math.sqrt( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return distance;
}
public void paint(Graphics g) {
setcoordinates();
findpath();
// Draw the map on the applet window
g.setColor( Color.blue );
for ( int y = 0 ; y < mapheight ; y++ ){
for ( int x = 0 ; x < mapwidth ; x++ ){
if ( map[ y ][ x ] == 1 ){
g.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
}
}
}
// Draw the start position on the applet window
g.setColor( Color.green );
g.fillOval( sx * cellwidth + 4 , sy * cellheight + 4 , 8 , 8 );
g.setColor( Color.red );
g.fillOval( ex * cellwidth + 4 , ey * cellheight + 4 , 8 , 8 );
// Draw the path
g.setColor( Color.yellow );
for ( int i = 0 ; i < px.size() ; i++ ){
g.fillOval( px.get( i ) * cellwidth + 8 , py.get( i ) * cellheight + 8 , 8 , 8 );
}
}
}
dinsdag 6 maart 2012
finally did it. a* working
I finally have a working a* function working on my computer and made it myself. I had bought the book "ai for game developers" and read through the chapter on pathfinding. I had a few mistypes that caused bugs but I was able to repair those. The code is in another language at the moment. In blitz basic 3d. This since I have more experience with that language. I will code the a* in Java in the future.
I will make a applet with a* pathfinding I think this weekend.
I will make a applet with a* pathfinding I think this weekend.
zondag 4 maart 2012
Random Obstacle Avoidance Example
import java.awt.*;
import java.applet.*;
public class RandomObstacleAvoidanceExample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
int sx , sy , ex , ey; // start and end position
int px , py; // player position
long delay;
private int map[][] = new int[][]{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0},
{0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
public void init(){
setBackground( Color.black );
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
setstartposition();
setendposition();
px = sx;
py = sy;
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
moveplayer();
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Random Obstacle Avoidance." , 10 , 10 );
bufferGraphics.setColor( Color.green );
// draw map
for ( int y = 0 ; y < 15 ; y++ ){
for ( int x = 0 ; x < 20 ; x++ ){
if ( map[ y ][ x ] == 1 ){
bufferGraphics.fillOval( x * 16 , y * 16 , 16 , 16 );
}
}
}
// draw end and start position
bufferGraphics.setColor( Color.red );
bufferGraphics.fillRect( sx * 16 , sy * 16 , 16 , 16 );
bufferGraphics.setColor( Color.yellow );
bufferGraphics.fillRect( ex * 16 , ey * 16 , 16 , 16 );
// draw player
bufferGraphics.setColor( Color.white );
bufferGraphics.fillRect( px * 16 , py * 16 , 16 , 16 );
g.drawImage(offscreen,0,0,this);
}
public void moveplayer(){
if ( delay < System.currentTimeMillis() ){
int newx = px;
int newy = py;
if ( px < ex ){
newx++;
}
if ( px > ex ){
newx--;
}
if ( py < ey ){
newy++;
}
if ( py > ey ){
newy--;
}
if ( map[ newy ][ newx ] == 1 ){
boolean newposfound = false;
newx = px;
newy = py;
while ( newposfound == false ){
if ( (int)(Math.random() * 2 ) == 0 ){
newx--;
}else{
newx++;
}
if ( (int)(Math.random() * 2 ) == 0 ){
newy--;
}else{
newy++;
}
if ( map[ newy ][ newx ] == 0 ){
newposfound = true;
}
}
}
px = newx;
py = newy;
if ( px == ex && py == ey ){
setstartposition();
setendposition();
px = sx;
py = sy;
}
delay = System.currentTimeMillis() + 200;
}
}
public void setstartposition(){
if ( (int)( Math.random() * 2 ) == 0 ){
sx = ( int )( Math.random() * 6 );
sy = 0;
}else{
sx = 0;
sy = ( int )( Math.random() * 6 );
}
}
public void setendposition(){
if ( (int)( Math.random() * 2 ) == 0 ){
ex = 20 - ( int )( Math.random() * 6 );
ey = 14;
}else{
ex = 19;
ey = 14 - ( int )( Math.random() * 6 );
}
}
}
woensdag 29 februari 2012
I received a new book - Ai for game developers.
I ordered a book yesterday. Today I got it. It is a book about Artificial Intelligence. It is for Novice programmers. There is a whole chapter on a* in it. I think I will be making more artificial intelligence examples for the weblog in the future.
On google books you can read through a large part of the book. Last year I made a random obstacle avoidance example in another language from this book. This method is great for moving ai through forrest map parts.
On google books you can read through a large part of the book. Last year I made a random obstacle avoidance example in another language from this book. This method is great for moving ai through forrest map parts.
woensdag 22 februari 2012
The Gimp - investing time in art creation.
It has been years ago that I last draw art for games. Sometimes I get the idea to spend time in drawing programs. I now downloaded the latest version of Gimp after looking around for drawing programs. I could find nothing better then the Gimp. I already found my favourite brush, the Smudge brush. This one can be used to smear the pixels around. It is easy to make clouds with the smudge brush for instance.
I want to spend time in the future drawing. I want to get good at it. This means getting to the 10.000 hours of experience with it. This means it will probably be old before I am good enough.
In blogger you can upload png files so I will be placing example drawings and step by step drawings in posts in the future. I hope that I will be productive. I also still think of things that I want to code.
I want to spend time in the future drawing. I want to get good at it. This means getting to the 10.000 hours of experience with it. This means it will probably be old before I am good enough.
In blogger you can upload png files so I will be placing example drawings and step by step drawings in posts in the future. I hope that I will be productive. I also still think of things that I want to code.
zaterdag 18 februari 2012
Copperbar Example
In the code below you can see how the copperbar shown above is made. Copperbars were used in games to create a filled background. The effect looks quite nice.
import java.awt.*;
import java.applet.*;
public class CopperBarExample001 extends Applet {
public void init() {
// Set the applet background color to black.
setBackground(Color.black);
}
public void paint(Graphics g) {
g.setColor( Color.white );
g.drawString( "Copperbar Example" , 10, 10 );
int y = 50;
double col = 0;
while ( y < 100 ){ // draw the first 50 lines of the copperbar , dark to light
g.setColor( new Color( 0 , 0 , (int)col ) );
col += 255 / 50;
g.drawLine( 0 , y , getSize().width , y );
y++;
}
while ( y < 150 ){ // draw the last 50 lines of the copperbar, light to dark.
g.setColor( new Color( 0 , 0 , (int)col ) );
col -= 255 / 50;
g.drawLine( 0 , y , getSize().width , y );
y++;
}
}
}
Sidescrolling Shooter Shooting Example
In this example you can control a block that shoots 3 types of blocks. The control instructions are on the applet screen.
import java.awt.*;
import java.applet.*;
public class ShooterShootingExample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
int px; // player x coordinate.
int py; // player y coordinate.
int pw = 16; // player width.
int ph = 16; // player height.
boolean pmoveup = false; // player move up.
boolean pmovedown = false; // player move down.
int shoottype = 0; // shooting , 0 = no shot, 1 = type 1, 2 = type 2, 3 = type 3
long lastshottime = 0;
int numshots = 32;
double shots[][] = new double[ numshots ][ 5 ]; //active, x, y, mx, my
int shotw = 3; // shot width.
int shoth = 3; // shot height.
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
px = 16;
py = getSize().height / 2 - ph / 2;
// Start the runnable thread.
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
moveplayer();
playershoot();
updateshots();
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Sidescroller Shooter Shooting Example." , 10 , 10 );
bufferGraphics.drawString( "use : w and s to move up and down." , 10 , 20 );
bufferGraphics.drawString( "use : i , o and p to shoot." , 10 , 30 );
bufferGraphics.setColor( Color.red );
// draw shots.
for ( int i = 0 ; i < numshots ; i++ ){
if ( shots[ i ][ 0 ] == 1 ){
bufferGraphics.fillOval( (int)shots[ i ][ 1 ] , (int)shots[ i ][ 2 ] , shotw , shoth );
}
}
// draw player.
bufferGraphics.fillRect( px , py , pw , ph );
g.drawImage(offscreen,0,0,this);
}
public boolean keyUp (Event e, int key){
if ( key == 119 ){ // w key, up
pmoveup = false;
}
if ( key == 115 ){ // s key, down
pmovedown = false;
}
if ( key == 105 ){ // i key, fire 1
shoottype = 0;
}
if ( key == 111 ){ // o key, fire 2
shoottype = 0;
}
if ( key == 112 ){ // p key, fire 3
shoottype = 0;
}
return true;
}
public boolean keyDown (Event e, int key){
if ( key == 119 ){ // w key, up
pmoveup = true;
}
if ( key == 115 ){ // s key, down
pmovedown = true;
}
if ( key == 105 ){ // i key, fire 1
shoottype = 1;
}
if ( key == 111 ){ // o key, fire 2
shoottype = 2;
}
if ( key == 112 ){ // p key, fire 3
shoottype = 3;
}
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
return true;
}
public void moveplayer(){
if ( pmoveup ){
if ( py > 30 ){
py--;
}
}
if ( pmovedown ){
if ( py < getSize().height - ph ){
py++;
}
}
}
public void playershoot(){
int n = -1;
if ( lastshottime < System.currentTimeMillis() ){
if ( shoottype == 1 ){
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = 0;
}
}
if ( shoottype == 2 ){
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = 0;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = -2;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = 2;
}
}
if ( shoottype == 3 ){
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = 0;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = -2;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 3;
shots[ n ][ 4 ] = 2;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 0;
shots[ n ][ 4 ] = -3;
}
n = getemptyshot();
if ( n > -1 ){
shots[ n ][ 0 ] = 1;
shots[ n ][ 1 ] = px + pw;
shots[ n ][ 2 ] = py + ph / 2;
shots[ n ][ 3 ] = 0;
shots[ n ][ 4 ] = 3;
}
}
lastshottime = System.currentTimeMillis() + 300;
}
}
public int getemptyshot(){
for ( int i = 0 ; i < numshots ; i++ ){
if ( shots[ i ][ 0 ] == 0 ){
return i;
}
}
return -1;
}
public void updateshots(){
for ( int i = 0 ; i < numshots ; i++ ){
if ( shots[ i ][ 0 ] == 1 ){
// Update the shots location on the screen.
shots[ i ][ 1 ] += shots[ i ][ 3 ];
shots[ i ][ 2 ] += shots[ i ][ 4 ];
// If the shots get outside of the screen then disable them.
if ( shots[ i ][ 1 ] > getSize().width ){
shots[ i ][ 0 ] = 0;
}
if ( shots[ i ][ 2 ] < 0 - shoth ){
shots[ i ][ 0 ] = 0;
}
if ( shots[ i ][ 2 ] > getSize().height ){
shots[ i ][ 0 ] = 0;
}
}
}
}
}
vrijdag 17 februari 2012
Rotating Side Scrolling Shooter Attack Wave Example
I made another example. This time a group of enemies fly in a circular pattern through the screen. In a shooter this would be a interesting challenge. When the enemies are done flying through the screen another wave starts. The code below shows how it is made.
import java.awt.*;
import java.applet.*;
public class SideScrollingAttackWave001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
int numenemies = 16;
double enemies[][] = new double[ numenemies ][ 8 ]; // active, x , y , mx , angle , radius , locx,locy
int enemyw = 16;
int enemyh = 16;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
initiateattackwave();
// Start the runnable thread.
new Thread(this).start();
}
public void initiateattackwave(){
// initiate the attack wave
for ( int i = 0 ; i < 7 ; i++ ){
enemies[ i ][ 0 ] = 1;
enemies[ i ][ 1 ] = 0;
enemies[ i ][ 2 ] = 0;
enemies[ i ][ 3 ] = -.2;
enemies[ i ][ 4 ] = i * 16;
enemies[ i ][ 5 ] = 100;
enemies[ i ][ 6 ] = getSize().width;
enemies[ i ][ 7 ] = getSize().height / 2 - 8;
}
}
public void run() {
boolean noleft;
for(;;) { // animation loop never ends
// update the enemies
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 1 ){
enemies[ i ][ 6 ] += enemies[ i ][ 3 ];
enemies[ i ][ 4 ] += .5;
if ( enemies[ i ][ 4 ] > 360 ){
enemies[ i ][ 4 ] = 0;
}
enemies[ i ][ 1 ] = Math.sin( Math.toRadians(
enemies[ i ][ 4 ] ) )
* enemies[ i ][ 5 ]
+ enemies[ i ][ 6 ];
enemies[ i ][ 2 ] = Math.cos( Math.toRadians(
enemies[ i ][ 4 ] ) )
* enemies[ i ][ 5 ]
+ enemies[ i ][ 7 ];
if ( enemies[ i ][ 6 ] < -70 ){
enemies[ i ][ 0 ] = 0;
}
}
}
// if there are no more active enemies then initiate new wave.
noleft = true;
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 1 ){
noleft = false;
}
}
if ( noleft ){
initiateattackwave();
}
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Sidescroller Attack Wave Example." , 10 , 10 );
bufferGraphics.setColor( Color.red );
// Draw the enemies
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 1 ){
bufferGraphics.fillRect( (int)enemies[ i ][ 1 ] ,
(int)enemies[ i ][ 2 ] ,
enemyw ,
enemyh );
}
}
g.drawImage(offscreen,0,0,this);
}
}
3 Attack Waves for Side Scrolling Shooter Example
I created an example where 3 attack waves are shown. The code checks if there are no enemies left and then creates a new attack wave. The enemies fly through the screen from right to left.
import java.awt.*;
import java.applet.*;
public class ThreeAttackWavesExample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
int numenemies = 64;
int enemyw = 16; // enemy width
int enemyh = 16; // enemy height
double enemies[][] = new double[ numenemies ][ 4 ]; // active,x,y,mx
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
createattackwave3();
// Start the runnable thread.
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
if ( noenemiesareleft() == true ){
int n = (int)( Math.random() * 4 );
if ( n == 1 ){
createattackwave1();
}
if ( n == 2 ){
createattackwave2();
}
if ( n == 3 ){
createattackwave3();
}
}
moveenemies();
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Sidescroller Shooter 3 attack waves Example." , 10 , 10 );
// Draw enemies
bufferGraphics.setColor ( Color.red );
for ( int i = 0 ; i < numenemies ; i++ ) {
if ( enemies[ i ][ 0 ] == 1 ){
bufferGraphics.fillRect( (int)enemies[ i ][ 1 ],
(int)enemies[ i ][ 2 ],
enemyw,
enemyh );
}
}
g.drawImage(offscreen,0,0,this);
}
public boolean noenemiesareleft(){
boolean noleft = true;
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 1 ){
noleft = false;
}
}
return noleft;
}
public void createattackwave1(){
int n;
for ( int i = 0 ; i < 4 ; i++ ){
n = findemptyenemy();
//System.out.println ("slot : " + n );
if ( n > -1 ){
enemies[ n ][ 0 ] = 1;
enemies[ n ][ 1 ] = getSize().width + 32 + 4 * enemyw - ( i * enemyw ) + ( enemyw * 3 );
enemies[ n ][ 2 ] = i * ( enemyh * 1.5 ) + 32;
enemies[ n ][ 3 ] = .5;
}
}
}
public void createattackwave2(){
int n;
for ( int i = 0 ; i < 4 ; i++ ){
n = findemptyenemy();
//System.out.println ("slot : " + n );
if ( n > -1 ){
enemies[ n ][ 0 ] = 1;
enemies[ n ][ 1 ] = getSize().width + 32 + ( i * enemyw ) + ( enemyw * 3 );
enemies[ n ][ 2 ] = i * ( enemyh * 1.5 ) + 132;
enemies[ n ][ 3 ] = .5;
}
}
}
public void createattackwave3(){
int n;
for ( int i = 0 ; i < 4 ; i++ ){
n = findemptyenemy();
//System.out.println ("slot : " + n );
if ( n > -1 ){
enemies[ n ][ 0 ] = 1;
enemies[ n ][ 1 ] = getSize().width + (int)(Math.random() * getSize().width / 2);
enemies[ n ][ 2 ] = (int)(Math.random() * (getSize().height - 100 ) + 50);
enemies[ n ][ 3 ] = .5;
}
}
}
public int findemptyenemy(){
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 0 ){
return i;
}
}
return -1;
}
public void moveenemies(){
for ( int i = 0 ; i < numenemies ; i++ ){
if ( enemies[ i ][ 0 ] == 1 ){
enemies[ i ][ 1 ] -= enemies[ i ][ 3 ];
if ( enemies[ i ][ 1 ] < 0 - enemyw ){
enemies[ i ][ 0 ] = 0;
}
}
}
}
}
Sin Wavy Movement Example
I got the idea to program parts of a SideScrolling Shooter. I downloaded several video's of game footage for it that I will be studying. I already noticed one type of enemy ship that flew in in a wavy pattern. I recreated that in this example. It uses the Java sin command. The enemy ship starts from the right and flies to the left and does this again if it gets outside of the screen.
import java.awt.*;
import java.applet.*;
public class SinWavyMovementExample001 extends Applet implements Runnable {
Graphics bufferGraphics;
Image offscreen;
int angle = 0;
double blockx;
double blocky;
double offsety = 0;
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
blockx = getSize().width;
blocky = getSize().height / 2 - 16;
// Start the runnable thread.
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
angle+= 3;
if ( angle > 360 ) {
angle = 0;
}
offsety = Math.sin(Math.toRadians(angle)) * 12;
blockx -= 0.5;
if ( blockx < -32 ){
blockx = getSize().width;
}
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Sin Wavy Movement Example" , 10 , 10 );
bufferGraphics.fillRect ( (int)blockx , (int)blocky + (int)offsety , 32 , 32 );
g.drawImage(offscreen,0,0,this);
}
}
dinsdag 14 februari 2012
Turrican Style Shooting Example
I have the Turrican remake on my notebook. In this game you can shoot around yourself. I remade this in Java. Shoot and then use the left and right keys to shoot around yourself.
import java.awt.*;
import java.applet.*;
public class TurricanShootingExample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
boolean playerMoveRight = false;
boolean playerMoveLeft = false;
double playershootangle = 90-45;
boolean shootlaser = false;
long shootingdelay;
double xPos=100;
double yPos=100;
boolean isFalling = false;
boolean isJumping = false;
double gravity;
double groundLevel = 100;
boolean isShooting;
boolean playerFacingLeft;
boolean playerFacingRight=true;
int numlasers = 50;
double lasers[][] = new double[ numlasers ][ 4 ]; // 0-active,1-x,2-y,3-angle
long lasertimeout[] = new long[ numlasers ]; // when does the laser end
public void init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// Start the runnable thread.
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
updateplayer();
updatelasers();
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void updatelasers(){
if ( shootlaser == true ) {
if ( shootingdelay < System.currentTimeMillis() ) {
shootingdelay = System.currentTimeMillis() + 60;
newlaser();
}
}
for ( int i = 0 ; i < numlasers ; i++ ) {
if ( lasers[ i ][ 0 ] == 1 ) {
lasers[ i ][ 1 ] += Math.sin( Math.toRadians( 2 * lasers[ i ][ 3 ] ));
lasers[ i ][ 2 ] += Math.cos( Math.toRadians( 2 * lasers[ i ][ 3 ] ));
if ( lasertimeout[ i ] < System.currentTimeMillis() ) {
lasers[ i ][ 0 ] = 0;
}
if ( lasers[ i ] [ 2 ] - 16 > groundLevel ) {
lasers[ i ][ 0 ] = 0;
}
}
}
}
public void newlaser(){
for ( int i = 0 ; i < numlasers ; i++ ) {
if ( lasers[ i ][ 0 ] == 0 ) {
lasers[ i ][ 3 ] = playershootangle;
lasers[ i ][ 0 ] = 1;
lasers[ i ][ 1 ] = xPos + 8
+ 10 * Math.sin( Math.toRadians( 2 * lasers[ i ][ 3 ] )) ;
lasers[ i ][ 2 ] = yPos + 8
+ 10 * Math.cos( Math.toRadians( 2 * lasers[ i ][ 3 ] )) ;
lasertimeout[ i ] = System.currentTimeMillis() + 1300;
break;
}
}
}
public void updateplayer(){
if( playerMoveRight && xPos < getSize().width - 16 && !shootlaser )
{
xPos++;
}
if( playerMoveLeft && xPos > 0 && !shootlaser )
{
xPos--;
}
if ( playerMoveRight && shootlaser ) {
playershootangle -= 2;
if ( playershootangle < 0 ) {
playershootangle = 360;
}
}
if ( playerMoveLeft && shootlaser ) {
playershootangle += 2;
if ( playershootangle > 360 ) {
playershootangle = 0;
}
}
if( isJumping )
{
yPos = (int)yPos - gravity;
gravity = gravity - .1;
if( gravity < 0 )
{
isJumping = false;
isFalling = true;
}
}
if( isFalling )
{
yPos = (int)yPos + gravity;
gravity = gravity + .1;
if( yPos >groundLevel )
{
isFalling = false;
yPos = groundLevel;
gravity =0;
}
}
}
public boolean keyUp (Event e, int key){
if ( key == 97 )
{
playerMoveLeft = false;
}
if ( key == 100 )
{
playerMoveRight = false;
}
if ( key == 109 ) { // m key
shootlaser = false;
// when the player releases the fire key then reset the shooting angle.
if ( playerFacingLeft ) {
playershootangle = 90+45;
}
if ( playerFacingRight ) {
playershootangle = 45;
}
}
return true;
}
public boolean keyDown (Event e, int key){
if ( key == 97 )
{
playerMoveLeft = true;
playerFacingLeft = true;
playerFacingRight = false;
}
if ( key == 100 )
{
playerMoveRight = true;
playerFacingRight = true;
playerFacingLeft = false;
}
if ( key == 120 ) // x character jump
{
if( isFalling == false && isJumping == false )
{
isJumping = true;
gravity = 3;
}
}
if ( key == 109 ){ // m shoot
shootlaser = true;
}
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
return true;
}
public void update (Graphics g)
{
bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
bufferGraphics.setColor ( Color.white );
bufferGraphics.drawString( "Turrican Shooting Example" , 10 , 10 );
bufferGraphics.drawString( "a - left , d - right , x jump , m - shoot" , 10 , 20 );
// draw lasers
for ( int i = 0 ; i < numlasers ; i++ ) {
if ( lasers[ i ][ 0 ] == 1 ) {
bufferGraphics.fillOval( (int)lasers[ i ][ 1 ] , (int)lasers[ i ][ 2 ] , 4 , 4 );
}
}
int x = (int) xPos;
int y = (int) yPos;
bufferGraphics.fillRect (x, y, 16, 16);
g.drawImage(offscreen,0,0,this);
}
}
stringWidth Example
import java.awt.*;
import java.applet.*;
public class stringWidthExample001 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
FontMetrics fm;
public void init() {
Graphics g;
g=getGraphics();
fm = g.getFontMetrics();
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
// Start the runnable thread.
new Thread(this).start();
}
public void run() {
for(;;) { // animation loop never ends
repaint();
try {
Thread.sleep(16);
}
catch (InterruptedException e) {
}
}
}
public void update(Graphics g){
String s;
bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString("Stringwidth Example",10,10);
bufferGraphics.setColor(Color.white);
s = "This text";
bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80);
s = "is centered";
bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 90);
s = "on the screen";
bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 100);
// Draw the Graphics buffer
g.drawImage(offscreen,0,0,this);
}
}
Exclamation Boolean Example
I was reading through sourcecode and found that I was not that familiar with the shown method of using a boolean. When placing a ! before a variable you can see if it is set to false.
import java.awt.*;
import java.applet.*;
public class exclamationbooleanvalueExample001 extends Applet {
// create a boolean and set its value to false
boolean the = false;
public void init() {
}
public void paint(Graphics g) {
g.drawString( "! Example" , 10, 10 );
g.drawString( "boolean the = false;" , 10 , 30 );
g.drawString( "if ( !the ) { " , 10 , 40 );
// if the is false then draw a message on the screen.
if ( !the ) {
g.drawString( "the value is false." , 10 , 70 );
}
}
}
Abonneren op:
Posts (Atom)