tag:blogger.com,1999:blog-51443267669713986252024-02-02T11:55:29.581-08:00Java Game Programming Step by Step (Applets)Learning how to program/code video games step by step in Java. Platformers, Shooters, Turn Based Strategy, Real Time Strategy.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.comBlogger180125tag:blogger.com,1999:blog-5144326766971398625.post-48981450189175991182015-06-03T11:59:00.001-07:002015-06-03T11:59:39.550-07:00Twitch has Creative and Programming category - learn/studyOn Twitch you can watch live video streams. There is a creative and programming category that you can subscribe to. Here you can watch people broadcasting drawing game graphics ect. Monkey see Monkey do :)<br />
<br />
The broadcasters get money from donation and adds. Has a chat room.<br />
<br />
#education #hobby<br />
<br />
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-40442223287618435182014-05-28T17:18:00.001-07:002014-05-28T18:03:42.919-07:00getWitdh() and getHeight() example.<!-- code formatted by http://manoli.net/csharpformat/ -->
<br />
<pre class="csharpcode">I was using getSize().width and getSize().height. Now I discovered that you can just use getWidth() and getHeight() for getting the applet width and height. Below in the sourcecode I am using these commands.</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode"></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7w9YDSeBusPJV0fF8L-eZKBbPkVA1KExd7BqT-ZmiJkUHokMNz8ABtb9xgNChKB7DYhxumT8YkgIt6JSOogNkR_SpScHCmuwvXbPdfiSMB5vMj2eo_hbIwnesuU0UXz2tuZhtJlpj9Jvn/s1600/gwgh.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7w9YDSeBusPJV0fF8L-eZKBbPkVA1KExd7BqT-ZmiJkUHokMNz8ABtb9xgNChKB7DYhxumT8YkgIt6JSOogNkR_SpScHCmuwvXbPdfiSMB5vMj2eo_hbIwnesuU0UXz2tuZhtJlpj9Jvn/s1600/gwgh.png" height="314" width="320" /></a></div>
<pre class="csharpcode"></pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode"><span style="font-size: x-small;">package test001;</span></pre>
<pre class="csharpcode"><span style="font-size: x-small; text-align: center;">import java.awt.*;</span></pre>
<pre class="csharpcode"><span style="font-size: x-small;">import java.applet.*;
<span class="kwrd">public</span> <span class="kwrd">class</span> testing001 extends Applet implements Runnable {
<span class="rem">// Graphics for double buffering.</span>
Graphics bufferGraphics;
Image offscreen;
<span class="kwrd">public</span> <span class="kwrd">void</span> init() {
setBackground(Color.black);
offscreen = createImage(getWidth(),getHeight());
bufferGraphics = offscreen.getGraphics();
<span class="kwrd">new</span> Thread(<span class="kwrd">this</span>).start();
}
<span class="kwrd">public</span> <span class="kwrd">void</span> run() {
<span class="kwrd">for</span>(;;) { <span class="rem">// animation loop never ends</span>
repaint();
<span class="kwrd">try</span> {
Thread.sleep(16);
}
<span class="kwrd">catch</span> (InterruptedException e) {
}
}
}
<span class="kwrd">public</span> <span class="kwrd">void</span> update(Graphics g){
bufferGraphics.clearRect( 0 , 0 , getWidth() , getHeight() );
bufferGraphics.setColor( Color.red );
bufferGraphics.drawString( <span class="str">"getWidth() and getHeight()"</span> , 10 , 10 );
bufferGraphics.drawRect( 20 , 20 , getWidth()-40 , getHeight()-40 );
g.drawImage(offscreen,0,0,<span class="kwrd">this</span>);
}
}</span></pre>
Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-19388200072679341182014-04-08T16:12:00.001-07:002014-04-08T16:15:34.143-07:00Planning part of Civilization game codeOne of the type of games that I want to make is the civilization like game. For a long time now I am learning how to program a civilization clone. The last few weeks I have been programming the worker automation and artificial intelligence of it. I got to the point where I have code that creates a map with cities and worker units that improve the map with roads and irrigation. I am working my way towards creating a more completed and civilization alike version. I am getting better at it.<br />
<div>
<br /></div>
<div>
It has been months since I posted on this blog and I think that I need to keep adding code to it. I am currently in the process of repeatitly retyping what I have in my main language blitz basic. I get to about a thousand lines per version. I think I wil be able to create a java version of it. It wil have pathfinding and map generating in it. </div>
<div>
<br /></div>
<div>
I have no idea when I wil do this but I am sure I wil do it. Keep a look out for it.<br />
<br />
Here is a link to a video of a older version.<br />
<br />
<a href="http://youtu.be/6LNGoTIixJs">http://youtu.be/6LNGoTIixJs</a></div>
Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-75709810413294508032013-10-29T08:48:00.001-07:002013-10-29T08:48:15.485-07:00Space Invaders Example (moving wave/collision/shooting and moving player)I made a new example in Jcreator. The example applet size should be 320*240.<br />
I formatted the code with a site tool that I found. No applets anymore since they do not seem to work anymore with my google site.<br />
<br />
There is not a lot of comment in the code. I want to keep it clean.<br />
<br />
<br />
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="rem">/**</span>
<span class="rem"> * @(#)SpaceInvadersExample003.java</span>
<span class="rem"> *</span>
<span class="rem"> * SpaceInvadersExample003 Applet application</span>
<span class="rem"> *</span>
<span class="rem"> * @author Rudy van Etten</span>
<span class="rem"> * @version 1.00 2013/10/28</span>
<span class="rem"> */</span>
import java.awt.*;
import java.applet.*;
<span class="kwrd">public</span> <span class="kwrd">class</span> SpaceInvadersExample003 extends Applet implements Runnable{
Graphics bufferGraphics;
Image offscreen;
<span class="kwrd">double</span> px = 160-8;
<span class="kwrd">double</span> py = 200;
<span class="kwrd">int</span> pwidth = 16;
<span class="kwrd">int</span> pheight = 16;
boolean ismovingright = <span class="kwrd">false</span>;
boolean ismovingleft = <span class="kwrd">false</span>;
<span class="kwrd">int</span> wavedirection = 1; <span class="rem">// 1 = right 0 = left</span>
<span class="kwrd">long</span> firedelay = 0;
<span class="kwrd">double</span> bullet[][] = <span class="kwrd">new</span> <span class="kwrd">double</span>[ 32 ][ 5 ]; <span class="rem">//active,x,y,my</span>
<span class="kwrd">double</span> alien[][] = <span class="kwrd">new</span> <span class="kwrd">double</span>[10*5][5];<span class="rem">//active,x,y</span>
<span class="kwrd">public</span> <span class="kwrd">void</span> init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
<span class="kwrd">int</span> x=0;
<span class="kwrd">int</span> y=0;
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0;i<10*5;i++){
alien[i][0]=1;
alien[i][1]=x;
alien[i][2]=y;
x+=32;
<span class="kwrd">if</span>(x>getSize().width-32){
x=0;
y+=32;
}
}
<span class="kwrd">new</span> Thread(<span class="kwrd">this</span>).start();
}
<span class="kwrd">public</span> <span class="kwrd">void</span> run() {
<span class="kwrd">for</span>(;;) { <span class="rem">// animation loop never ends</span>
<span class="kwrd">if</span> (ismovingright==<span class="kwrd">true</span>){
<span class="kwrd">if</span> (px<getSize().width-pwidth)
px+=1;
}
<span class="kwrd">if</span> (ismovingleft==<span class="kwrd">true</span>){
<span class="kwrd">if</span>(px>0)
px-=1;
}
<span class="rem">// move the bullets and remove them if they get above the screen</span>
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0 ;i<32;i++){
<span class="kwrd">if</span>(bullet[i][0]==1){
bullet[i][2]+=bullet[i][3];
<span class="kwrd">if</span>(bullet[i][2]<0){
bullet[i][0]=0;
}
}
}
<span class="rem">// Collision with the bullets and the aliens.</span>
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0; i<32;i++){
<span class="kwrd">if</span>(bullet[i][0]==1){
<span class="kwrd">for</span> (<span class="kwrd">int</span> ii=0;ii<10*5;ii++){
<span class="kwrd">if</span> (alien[ii][0]==1){
Rectangle rec1 = <span class="kwrd">new</span> Rectangle((<span class="kwrd">int</span>)bullet[i][1],(<span class="kwrd">int</span>)bullet[i][2],16,16);
Rectangle rec2 = <span class="kwrd">new</span> Rectangle((<span class="kwrd">int</span>)alien[ii][1],(<span class="kwrd">int</span>)alien[ii][2],16,16);
<span class="kwrd">if</span>(rec1.intersects(rec2)){
<span class="rem">//g.drawString("Filled Rectangle Collision", 50, 60 );</span>
bullet[i][0]=0;
alien[ii][0]=0;
}
}
}
}
}
<span class="rem">// Move the alien wave</span>
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0;i<10*5;i++){
<span class="kwrd">if</span>(alien[i][0]==1){
<span class="kwrd">if</span>(wavedirection==0){
alien[i][1]-=0.1;
}<span class="kwrd">else</span>{
alien[i][1]+=0.1;
}
<span class="kwrd">if</span>(alien[i][1]>getSize().width-16){
wavedirection=0;
}
<span class="kwrd">if</span>(alien[i][1]<0){
wavedirection=1;
}
}
}
repaint();
<span class="kwrd">try</span> {
Thread.sleep(16);
}
<span class="kwrd">catch</span> (InterruptedException e) {
}
}
}
<span class="kwrd">public</span> <span class="kwrd">void</span> update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
bufferGraphics.setColor(Color.red);
bufferGraphics.drawString(<span class="str">"Space Invaders Example."</span>,20,30);
<span class="rem">// draw player ship</span>
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect((<span class="kwrd">int</span>)px,(<span class="kwrd">int</span>)py,pwidth,pheight);
<span class="rem">//draw aliens</span>
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0;i<10*5;i++){
<span class="kwrd">if</span>(alien[i][0]==1){
bufferGraphics.fillRect((<span class="kwrd">int</span>)alien[i][1],(<span class="kwrd">int</span>)alien[i][2],16,16);
}
}
<span class="rem">// Draw bullets</span>
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0;i<32;i++){
<span class="kwrd">if</span>(bullet[i][0]==1){
bufferGraphics.fillRect((<span class="kwrd">int</span>)bullet[i][1],(<span class="kwrd">int</span>)bullet[i][2],3,3);
}
}
g.drawImage(offscreen,0,0,<span class="kwrd">this</span>);
}
<span class="kwrd">public</span> <span class="kwrd">void</span> createBullet(){
<span class="kwrd">for</span>(<span class="kwrd">int</span> i=0;i<32;i++){
<span class="kwrd">if</span>(bullet[i][0]==0){
bullet[i][0]=1;
bullet[i][1]=px+8;
bullet[i][2]=py-16;
bullet[i][3]=-2;
<span class="kwrd">return</span>;
}
}
}
<span class="kwrd">public</span> boolean keyDown (Event e, <span class="kwrd">int</span> key){
<span class="kwrd">if</span>( key == Event.LEFT )
{
ismovingleft = <span class="kwrd">true</span>;
}
<span class="kwrd">if</span>(key==Event.RIGHT)
{
ismovingright = <span class="kwrd">true</span>;
}
<span class="kwrd">if</span>( key == 32 ) <span class="rem">// space bar for jump</span>
{
<span class="kwrd">if</span>(firedelay<System.currentTimeMillis()){
firedelay = System.currentTimeMillis() + 200;
createBullet();
}
}
System.<span class="kwrd">out</span>.println (<span class="str">" Integer Value: "</span> + key);
<span class="kwrd">return</span> <span class="kwrd">true</span>;
}
<span class="kwrd">public</span> boolean keyUp (Event e, <span class="kwrd">int</span> key){
<span class="kwrd">if</span>( key == Event.LEFT )
{
ismovingleft = <span class="kwrd">false</span>;
}
<span class="kwrd">if</span>( key == Event.RIGHT )
{
ismovingright = <span class="kwrd">false</span>;
}
<span class="kwrd">return</span> <span class="kwrd">true</span>;
}
}</pre>
Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-29718493299003522932013-10-27T12:38:00.001-07:002013-10-27T12:38:40.960-07:00Jcreator Le does not work on Windows 8 and other.I have seen that the news section on the Jcreator website has not been updated since 2012. I think that the program ends there. It does not work on Windows 8 (Jcreator forum post metioned it)<br />
<br />
I think I will have to find a new program to edit and compile Java Applets with when I get Windows 8.<br />
notepad++ I have been using for HTML5 and it works ok so I will be trying that. Jcreator is an easy and good editor and compiler and this one I will use for as long as I can.<br />
<br />
I have been thinking of what examples to create next for the blog. I will mention the ideas and thoughts on my Twitter.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-6743844058324285472013-06-03T11:34:00.001-07:002013-06-03T11:35:40.711-07:00Setting the drawing color<!-- code formatted by http://manoli.net/csharpformat/ -->
<br />
<pre class="csharpcode">Below is shown how to set the drawing color to a red green and blue value. This is done by using new Color folowed by the rgb colors.</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode">I am now posting the code directly and not a applet with it because that does not work anymore with google sites(with me anyway)</pre>
<pre class="csharpcode"></pre>
<pre class="csharpcode">import java.awt.*;
import java.applet.*;
<span class="kwrd">public</span> <span class="kwrd">class</span> testproject extends Applet implements Runnable {
<span class="rem">// Graphics for double buffering.</span>
Graphics bufferGraphics;
Image offscreen;
<span class="kwrd">public</span> <span class="kwrd">void</span> init() {
setBackground(Color.black);
offscreen = createImage(getSize().width,getSize().height);
bufferGraphics = offscreen.getGraphics();
<span class="kwrd">new</span> Thread(<span class="kwrd">this</span>).start();
}
<span class="kwrd">public</span> <span class="kwrd">void</span> run(){
<span class="kwrd">for</span>(;;){
repaint();
<span class="kwrd">try</span>{
Thread.sleep(10);
}
<span class="kwrd">catch</span> (InterruptedException e){
}
}
}
<span class="kwrd">public</span> <span class="kwrd">void</span> update(Graphics g){
bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
<span class="rem">// Here the color is set using new Color followed by</span>
<span class="rem">// the rgb values. In this case 200,0,0</span>
bufferGraphics.setColor(<span class="kwrd">new</span> Color(200,0,0));
bufferGraphics.drawString(<span class="str">"Test"</span>,10,10);
g.drawImage(offscreen,0,0,<span class="kwrd">this</span>);
}
}</pre>
Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-34573570955405595552013-06-03T11:04:00.001-07:002013-06-03T11:04:49.546-07:00Jcreator is no longer free I thinkFor about 10 years I have been using Jcreator to program and view java sourcecode. Today I saw that there was a new version out and I downloaded it and installed it. I should not have done this because the free version now is a trail version. There was a message that there were only 30 days available.<br />
I tried looking around and saw on the softpedia site that it was free but it downloads the same trail version. I put a comment there that has to be aproved first. In that I wrote that it was not the free version.<br />
<br />
I had to look back into my archives and now have a 2004 version of jcreator that works.<br />
<br />
I am thinking of picking up java again. Though I may not stick to using it. I am viewing java code atm. Maybe I will post new source code on the blog again.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-75953198383129245232012-06-19T06:58:00.001-07:002012-06-25T06:45:03.762-07:00I have decided to stop programming in JavaSince the applets no longer work. I have decided to stop programming in Java. I never really gotten used to Java.<br />
<br />
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.<br />
<br />
(edit:)<br />
I think the google site is returning errorous data from the applets. Maybe this will go away.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com2tag:blogger.com,1999:blog-5144326766971398625.post-54636044672696965932012-06-11T04:50:00.001-07:002012-06-11T05:10:01.904-07:00Java applets do not work anymore on my pcI 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.<br />
<br />
edit :<br />
<br />
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.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-27663089727709687852012-06-11T04:25:00.002-07:002012-06-11T04:25:54.831-07:00Manhattan Distance example<br />
<center><applet code="manhattandistanceexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"></applet></center>
<br />
Manhattan distance example. Move the mouse over the applet to see the distance change.<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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 );
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-24455749831600322802012-06-11T04:24:00.000-07:002012-06-11T04:24:34.591-07:00Euclidean Distance example<br />
<br />
<center><applet code="euclideandistanceexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"></applet></center>
<br />
<br />
Euclidean distance example. Move the mouse over the applet to see the distance change.
<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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 ) );
}
}
</code></pre>
<br />
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-50013374030542378002012-06-02T06:00:00.001-07:002012-06-02T06:00:54.110-07:00The 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.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-29465809739097648152012-05-15T12:04:00.001-07:002012-05-20T09:56:19.538-07:00This blog might be blocked starting 30 may 2012I 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.<br />
<br />
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.<br />
<br />
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.<br />
<br />
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.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-50874913150134636912012-04-19T11:52:00.002-07:002012-05-15T12:34:38.446-07:00Game Menu Example<br />
<br />
<br />
<center><applet code="gamemenuexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"><param name="_cx" value="8466" />
<param name="_cy" value="6350" />
<param name="_cx" value="8466" />
<param name="_cy" value="6350" />
</applet></center>
<br />
Use the s and w keys to move the selection cursor.<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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;
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-77542040312675708632012-04-19T11:51:00.002-07:002012-04-19T11:51:41.625-07:00Topdown patrolling ai example<br />
<br />
<center><applet code="topdownpatrollingaiexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"></applet></center>
<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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;
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com1tag:blogger.com,1999:blog-5144326766971398625.post-82182137158992126532012-04-19T11:50:00.001-07:002012-04-19T11:50:35.676-07:00Topdown Portals Example<br />
<br />
<center><applet code="topdownportalsexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"></applet></center>
<br />
<br />
Portals example. Move into the yellow blocks to switch levels.<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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;
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-28380521935782306342012-04-07T07:35:00.001-07:002012-04-07T07:35:34.680-07:00Platformer Time Dissapearing Tiles Example<br />
<center><applet code="tdtilesexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"><param name="_cx" value="8466" />
<param name="_cy" value="6350" />
</applet></center><br />
In this example you can move the player across tiles that dissapear after 2 seconds after they have been touched.<br />
<br />
<pre style="background-color: #eeeeee; border-bottom: rgb(153,153,153) 1px dashed; border-left: rgb(153,153,153) 1px dashed; border-right: rgb(153,153,153) 1px dashed; border-top: rgb(153,153,153) 1px dashed; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 10px; line-height: 10px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 98%;"><code>
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;
}
}
</code></pre>Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-20674551831644036082012-04-05T15:02:00.000-07:002012-04-05T15:02:00.172-07:002d Topdown Doors and keys Example<br />
<center><applet code="topdowndoorsandkeysexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"><param name="_cx" value="8466" />
<param name="_cy" value="6350" />
</applet></center><br />
<br />
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.<br />
<br />
<pre style="background-color: #eeeeee; border-bottom: rgb(153,153,153) 1px dashed; border-left: rgb(153,153,153) 1px dashed; border-right: rgb(153,153,153) 1px dashed; border-top: rgb(153,153,153) 1px dashed; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 10px; line-height: 10px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 98%;"><code>
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;
}
}
</code></pre>Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-27112055727958351032012-04-05T13:02:00.000-07:002012-04-05T13:02:03.437-07:00Platformer Doors Example<br />
<center><applet code="platformerdoorsexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"><param name="_cx" value="8466" />
<param name="_cy" value="6350" />
</applet></center><br />
Platformer Doors Example. The instructions are shown in the applet window. The yellow blocks are the doors. Sourcecode is below.<br />
<br />
<pre style="background-color: #eeeeee; border-bottom: rgb(153,153,153) 1px dashed; border-left: rgb(153,153,153) 1px dashed; border-right: rgb(153,153,153) 1px dashed; border-top: rgb(153,153,153) 1px dashed; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 10px; line-height: 10px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 98%;"><code>
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;
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-86601328191010212292012-04-04T11:23:00.001-07:002012-04-04T11:34:02.743-07:00Platformer Ladders Example<br />
<br />
<br />
<center><applet code="platformerladdersexample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" width="320"><param name="_cx" value="8466" />
<param name="_cy" value="6350" />
</applet></center><div align="left">
Use w s a d to control the player block. Move to the ladders and press w or s to move up or down.</div>
<br />
<br />
<pre style="background-color: #eeeeee; border-bottom: rgb(153,153,153) 1px dashed; border-left: rgb(153,153,153) 1px dashed; border-right: rgb(153,153,153) 1px dashed; border-top: rgb(153,153,153) 1px dashed; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 10px; line-height: 10px; overflow: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 98%;"><code>
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;
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-42387473545028319632012-03-16T15:54:00.001-07:002012-03-17T10:19:19.945-07:00a* (aStar) pathfinding example<br />
<br />
<br />
<br />
<center><applet code="astarpathfindingexample01.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" title="Java" width="320"></applet></center>
<br />
<br />
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.<br />
The source code is below.<br />
<br />
Anyone may use the code for their own. No credit is required.<br />
<br />
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.<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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<integer> olx = new ArrayList<integer>();
ArrayList<integer> oly = new ArrayList<integer>();
ArrayList<integer> olf = new ArrayList<integer>();
ArrayList<integer> olg = new ArrayList<integer>();
ArrayList<integer> olh = new ArrayList<integer>();
ArrayList<integer> olpx = new ArrayList<integer>();
ArrayList<integer> olpy = new ArrayList<integer>();
// Closed list ( x, y, f, g, h, parentx, parenty )
ArrayList<integer> clx = new ArrayList<integer>();
ArrayList<integer> cly = new ArrayList<integer>();
ArrayList<integer> clf = new ArrayList<integer>();
ArrayList<integer> clg = new ArrayList<integer>();
ArrayList<integer> clh = new ArrayList<integer>();
ArrayList<integer> clpx = new ArrayList<integer>();
ArrayList<integer> clpy = new ArrayList<integer>();
// Path
ArrayList<integer> px = new ArrayList<integer>();
ArrayList<integer> py = new ArrayList<integer>();
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 );
}
}
}
</integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-28612971131237263362012-03-06T12:31:00.002-08:002012-03-06T12:31:47.056-08:00finally did it. a* workingI 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.<br />
<br />
I will make a applet with a* pathfinding I think this weekend.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-57305434709065876722012-03-04T12:53:00.000-08:002012-03-19T14:40:51.558-07:00Random Obstacle Avoidance Example<br />
<br />
<br />
<br />
<center><applet code="RandomObstacleAvoidanceExample001.class" codebase="http://sites.google.com/site/javaappletgameprogramming/files" height="240" title="Java" width="320"></applet></center><center><br /></center><center style="text-align: left;">From the book ai for game developers. Random Obstacle Avoidance. The white block moves through the map and avoids the green trees. Below the code that shows how it is done.</center>
<br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 10px; line-height: 10px; overflow: auto; padding: 5px; width: 98%;"><code>
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 );
}
}
}
</code></pre>
<br />Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-89845579195704971772012-02-29T08:09:00.000-08:002012-02-29T08:09:04.540-08:00I 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.<br />
<br />
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.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com0tag:blogger.com,1999:blog-5144326766971398625.post-21672831907904628222012-02-22T15:33:00.000-08:002012-02-22T15:33:04.113-08:00The 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.<br />
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.<br />
<br />
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.Pakz001http://www.blogger.com/profile/10537740820223145710noreply@blogger.com2