Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Just a 2D Beginners code for sharing  (Read 4592 times)
0 Members and 1 Guest are viewing this topic.
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Posted 2012-03-26 23:29:21 »

Hello! I know I'm new and have only a few posts, but I wanted to be more active and post more, so I think i'll post some of my beginner/nub code! I've been coding in Java(General) for ~2 Months, and only beginning to get into 2D stuff more recently (Past 2 weeks or so). I've been following some 2D animating/coding tutorials, but they just don't make sense until I find some way to simplify and understand them. I like my code for reflecting simplicity. This will probably change though. Without further ado, here it is!

Finding you Character (The most simple, focused AI I know of)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void findCharacter() {
      int xChar = 0; // The x Position for the Character (Used when comparing changes/data for the zombie to follow)
     int yChar = 0; // The y Position for the character (Used when comparing changes/data for the zombie to follow)

      int xZombie = 0; // The x Position for the Zombie/Enemy (Used when painting the image/rectangle)
     int yZombie = 0; // The y Position for the Zombie/Enemy (Used when painting the image/rectangle)

      int moveSpeed = 1; // How fast to move towards character (Pixels)

      if (xZombie < xChar) {
         xZombie += moveSpeed;
      }
      if (xZombie > xChar) {
         xZombie -= moveSpeed;
      }
      if (yZombie < yChar) {
         yZombie += moveSpeed;
      }
      if (yZombie < yChar) {
         yZombie += moveSpeed;
      }
   }


There's also my Tile-Setter system Tongue
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void TileSystem() {

      int Width = 640;
      int Height = 480;
     
      int x = 0;
      int y = 0;

      int tileSize = 32; // How large the tiles are
     int rowTiles = 25; // How many tiles to put into a row (Width / tileSize)
     int columnTiles = 20; // How many tiles/rows to put into a column (Height / tileSize)

      for (x = 0; x < Width; x += tileSize) { //
        for (y = 0; y < Height; y += tileSize) { //
           x = 0;
         }
      }
     
      System.out.println("X: " + x + "Y: " + y);

   }

//Note: I had to re-write this off the top of my head, since I didn't feel like going across the room to grab my flash-drive which I keep all of my good Java Tid-Bits on. I think I screwed up somewhere Tongue

After-Post #1 - Screwed up the tile-rendering thing. It's an infinity loop. I'll try to fix it while still working and then pastah back.

Just some beginner's code! Hope I can develop a better organizational skill and also the ability to actually write this stuff better. 2D Animation is fun until you actually try to make it Tongue
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-26 23:31:09 »

Ehhh that double for loop is an infinite loop Wink

Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2012-03-26 23:33:07 »

Oh, really? No wonder it was screwing with me during test-driving. I knew  I had to toss it back to 0 for the next row. Time for experimentation Evil
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-03-26 23:33:57 »

You reset x back to 0...meaning that loop will go on forever since x will never be greater or equal to Width Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!