Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  PulpCore tutorial 1: Stages, Scenes, Sprites, and Hello World  (Read 5055 times)
0 Members and 1 Guest are viewing this topic.
Offline stemkoski

Junior Newbie





« Posted 2011-07-07 03:50:27 »


In true computer programming spirit, I should probably start with "Part 0", so first here are some preliminaries:

PulpCore tutorial 0: Getting Started

IDE-specific setup guides:

Bookmark these PulpCore links:

And now, onto the main feature.

PulpCore tutorial 1: Stages, Scenes, Sprites, and Hello World

In this tutorial, I explain the test code provided in the "Getting Started with BlueJ" article.

Two core concepts of a PulpCore applet are the Stage and the Scene.

The Stage is where an applet is displayed. There is only one Stage per applet. The Stage contains static methods that handle loading, updating, and drawing Scenes. Stage also contains methods for getting the height and width of the applet and setting the frame rate.

A Scene is an object that updates the display and handles input from the user. All PulpCore apps must implement at least one Scene.
A typical game will have multiple Scenes, each corresponding to a component of the game: a title scene, main menu scene, main game scene, high score scene, help scene, etc.
The Scene2D class extends the Scene class and provides commonly used features like Sprite management, layer management (layers are Groups of Sprites; typical layers include the background image(s), player/NPC images, and the GUI), and Timeline management (Timelines control animations). Subclasses should override the following two methods:
  • load(), which executes when the scene is created, typically where resources are loaded
  • update(int), which executes once per frame, typically contains game logic, input handling, etc.

Rendering is handled by the drawScene() method; most apps will not override this method.
The first Scene loaded by an applet is defined by the "scene" applet parameter (in the HMTL file):
      <param name="scene" value="MyFirstScene" />
         
A Sprite is an image together with location information. PulpCore contains a number of Sprite classes:
  • ImageSprite, a standard sprite with an image that is static (CoreImage) or animated (AnimatedImage)
  • FilledSprite, a solid-colored rectangle (optionally with a colored border), typically used as background
  • Label, a sprite that displays text; image-based fonts may be loaded using the CoreFont class
  • some standard user-interface controls: Button, TextField, Slider
  • Group, a sprite that is itself a collection of sprites
Sprites are added to a scene using the add() method.

Below is the code for a "Hello World"-style class that illustrates the information above.
         
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
import pulpcore.Stage;
import pulpcore.scene.Scene2D;

import pulpcore.image.Colors;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreImage;

import pulpcore.sprite.Label;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.ImageSprite;

public class PulpyDemo extends Scene2D
{
    FilledSprite background;
    Label helloLabel;
    ImageSprite logoImage;

    public void load()
    {
        // Calculate coordinates of applet center
       int centerX = Stage.getWidth() / 2;
        int centerY = Stage.getHeight() / 2;

        // Create a background
       background = new FilledSprite(Colors.YELLOW);
        add(background);

        // Build a new label
       helloLabel = new Label(CoreFont.getSystemFont(), "Hello World!", centerX, 40);

        // Center the label horizontally and vertically
       helloLabel.setAnchor(0.5, 0.5);

        // Add the label to the scene
       add(helloLabel);

        // Load an image
       logoImage = new ImageSprite(CoreImage.load("logo.png"), centerX, centerY);

        // Center the image
       logoImage.setAnchor(ImageSprite.CENTER);

        // Add the image to the screen
       add(logoImage);
    }

    public void update(int elapsedTime)
    {
        // this method has been intentionally left blank
   }
}


Thanks for reading!
Offline stemkoski

Junior Newbie





« Reply #1 - Posted 2011-07-07 04:11:28 »

I have a number of further tutorials planned. Coming soon will be:

2. Image effects: crop, scale, rotate, split (great for working with spritesheets), and filters.
    Custom fonts, labels, buttons, sliders, textfields, and Scene transitions.

3. Animated images, path movement, and timelines. Getting keyboard input from the player. A player-controlled sprite.

4. Creating tilemaps, using Groups and Layers, and scrolling the visible region using Viewports.

Coming someday (hopefully) will be (in no particular order): collision detection, using a physics engine, particle systems, and network-based games.  If you have any requests, please post them below!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (32 views)
2013-05-17 21:29:12

alaslipknot (40 views)
2013-05-16 21:24:48

gouessej (70 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (137 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.16 seconds with 20 queries.