Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Munchor

Pages: 1 ... 20 21 [22] 23 24 ... 424
316
Computer Programming / [Java] Timer inside event-based class
« on: September 04, 2011, 04:03:35 pm »
Code: [Select]
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements KeyListener {
  /* Game Panel class containing all functions */

  /* Global static variables */
  JFrame mainFrame = new JFrame("Falling Blocks");
  int y = 520;
  int[] locations = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800};

  /* Global dynamic variables */
  int a; //Horizontal acceleration
  int x;
  boolean needs_repaint;
  boolean on_title_screen, on_game, on_game_over;

  public void init_variables() {
    /* Initates the dynamic variables in order to show Title Screen */
    a = 0;
    x = 0;
    on_title_screen = true;
    on_game = false;
    on_game_over = false;
    needs_repaint = true;
  }

  public void paintComponent(Graphics g) {
    /* Draws the elements on the screen */
    super.paintComponent(g);

    if (on_title_screen) {
      g.drawString("Press SPACE to start game", 10, 10);
      g.drawString("Made by David Gomes", 10, 30);
    }

    else if (on_game) {
      //Draw the player
      g.fillRect(x, y, 50, 50);
    }

    else { //Game Over
      g.drawString("Game Over", 10, 10);
    }

    //Repainting has finished
    needs_repaint = false;
  }

  public Game() {
    /* Defines the frame and the game panel */
    this.setFocusable(true);
    this.addKeyListener(this);

    /* Fixed Layout frame */
    mainFrame.setSize(852, 600);
    mainFrame.setResizable(false);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Add game panel to main frame
    mainFrame.getContentPane().add(this);
  }

  public void run() {
    /* Displays the frame, start the game */
    mainFrame.setVisible(true);
    this.requestFocus();
    init_variables();
  }

  @Override
  public void keyPressed(KeyEvent key) {
    /* Handles key presses */

    if (on_title_screen) {
      /* Start the game when user presses SPACE */
      switch (key.getKeyCode()) {
      case KeyEvent.VK_SPACE:
        on_title_screen = false;
        on_game = true;
        needs_repaint = true;
        break;
      }
    }

    else if (on_game) {
      switch (key.getKeyCode()) {
      case KeyEvent.VK_LEFT:  // Move x coordinate left
        if (x > 0) {
          x -= 50;
          needs_repaint = true;
        }
        break;

      case KeyEvent.VK_RIGHT: // Move x coordinate right
        if (x < 800) {
          x += 50;
          needs_repaint = true;
        }
        break;
      }
    }

    else { // Game Over
      init_variables(); //Go to title screen
      needs_repaint = true;
    }

    // Repaint only if needed
    if (needs_repaint)
      this.repaint();
  }

  /* Game Class is not abstract, and these functions are not needed */
  @Override
  public void keyReleased(KeyEvent arg0) { }
  @Override
  public void keyTyped(KeyEvent arg0) { }
}

I have that code (with syntax highlighting here), and I need to add a timer inside the Game class, associated with a function that runs every N seconds. And I need it to run at the same time as other functions run in the class (keyPressed, paint), etc.

I found a lot of information online on how to make timers in Java (javax.swing.Timer class and other ways), but I don't think it allows me to have the other functions running inside the class too. And I also don't know how to implement it in this specific context.

I appreciate any help, thanks!

317
TI Z80 / Re: Online Z80 Disassembler
« on: September 04, 2011, 01:11:00 pm »
Deep, if you could give me some indications on how to do that (timer thing), I'd be pleased, as I know you've done that before in Z80 Tables for example.

I may have originally missed this post but :D Sometimes I even code differently based on how a disassembler would see my code. But that's just a weird thing I do.

That's not so common indeed. Don't forget though this is an online disassembler, and it was made for quick stuff, not disassembling OSs and Apps.

318
Miscellaneous / Re: Going to be off for a long time
« on: September 04, 2011, 06:57:57 am »
* ephan lost.

Quote
But I'll be back and I'll continue Pokemon Topaze, so please, do not delete my account (hoping I'll remember my password).

Don't worry we'll never do that :)

I hope everything works fine and you make it in two years, and I hope you can come here still once a month or even more. Don't worry thought, we won't get a new Absol while you're away :)

319
Axe / Re: Axe Tilemapping and Optimization
« on: September 04, 2011, 05:52:49 am »
Code: [Select]
.DRAW MAP USING TILEMAPPING CODE HERE
StorePic

Repeat getKey(15)

RecallPic
DispGraph^^r
End

Something like that? The problem is the grayscale, and Builderboy told me how to do it (use an Appvar to get 768 extra bytes), but I'm thinking that's too complicated, so I'll have to think about it.

320
Casio PRIZM / Re: [Prizm 2011 Contest Entry] PrizmCity
« on: September 04, 2011, 05:29:52 am »
Ashbad, this seems like an ambitious project, the ideas you listed all seem neat, I hope you can make it before the deadline is over, unlike what happened in the Axe contest :S, good luck!

321
Miscellaneous / Re: Gaming at school
« on: September 04, 2011, 04:35:10 am »
We haven't touched any code at all yet. My teach promised that we'd get to use dreamweaver, said we won't learn any javascript :( and we'll mess with flash, of all things, for a bit.

Yesterday, I got to make a blog! What fun! [/sarcasm]

"get to use dreamweaver", too bad I don't see the advantage of it. I once tried Adobe Dreamweaver and it was a piece of crap. It's expensive, heavy, CPU Consuming, and all it does is nothing more than a notepad :)

322
Axe / Re: Axe Tilemapping and Optimization
« on: September 03, 2011, 04:14:35 pm »
Why not draw the tilemap once, save the screen to a buffer, and recall that buffer at the beginning of each render loop?  That's what I do whenever possible, and why TaNF/UCTI both ran at well over 45 FPS.

Which buffer would I save it to, though? And how to recall itl, do I use RecallPic? I am not familiar with those concepts, so thanks Ashbad :)

323
Axe / Axe Tilemapping and Optimization
« on: September 03, 2011, 01:19:47 pm »
I started a project for the Axe Progamming Contest but I soon quit it because of tilemapping, because I didnt't find a way of not having to redraw all the screen all the time, which made my game run quite slow.

Here's the full code (without tilemapping, I use graphing commands to design the map):

Code: [Select]
.A
#Icon(FFFF8001BC3DA5E5A525BD2581E59C3D95C19D7981C9BE89A289BEF98001FFFF)
[E0E0808000000000]->Pic1
ClrDraw^^r
Fix 5
1280->X
10240->Y
0->A->B
24->C
20->E
Repeat getKey(15)
  .CHECK IF USER IS TOUCHING FLAG
  If (X/256-C+4<=8) and (Y/256-E+4<=8)
    Return
  End

  .CHECK IF ENEMY ON THE LEFT
  0->J
  For(I,0,3)
    If (pxl-Test(X/256-1,Y/256+I)^^r)
      1->J
    End
  End

  .CHECK IF ENEMY ON THE RIGHT
  0->K
  For(I,0,3)
    If (pxl-Test(X/256+4,Y/256+I)^^r)
      1->K
    End
  End

  .CHECK IF ENEMY ON TOP
  0->M
  For(I,0,3)
    If (pxl-Test(X/256+I,Y/256-1)^^r)
      1->M
    End
  End

  .CHECK IF ENEMY BELOW THE PLAYER
  0->N
  For(I,0,3)
    If (pxl-Test(X/256+I,Y/256+4)^^r)
      1->N
    End
  End

  .IF AN ENEMY IS TOUCHING THE PLAYER
  If (N) or (M) or (J) or (K)
    Return
  End

  .CHECK IF WALL ON THE LEFT
  0->L
  For(I,0,3)
    If (pxl-Test(X/256-1,Y/256+I))
      1->L
    End
  End

  .CHECK IF WALL ON THE RIGHT
  0->R
  For(I,0,3)
    If (pxl-Test(X/256+4,Y/256+I))
      1->R
    End
  End

  .IF ANYTHING ON THE RIGHT OR ON THE LEFT, STOP
  If L or (R)
    0->A
  End

  .CHECK IF ANYTHING UNDER THE PLAYER
  0->D
  For(I,0,3)
    If (pxl-Test(X/256+I,Y/256+4))
      1->D
    End
  End

  .GRAVITY
  B+5->B
  For(I,0,3)
    If pxl-Test(X/256+I,Y/256-1+(B>>0*5))
      0->B
    End
  End

  .GO LEFT
  If getKey(2) and (A>>~255) and (L=0)
    A-32->A
  End

  .GO RIGHT
  If getKey(3) and (A<<256) and (R=0)
    A+32->A
  End

  .JUMP WHEN USER PRESSES UP
  If getKey(4) and (D)
    ~256->B
  End

  .WALL JUMP ON THE LEFT
  If (R) and (D=0) and (getKey(2)) and (getKey(4))
    ~100->B
    A-40->A
  End

  .WALL JUMP ON THE RIGHT
  If (L) and (D=0) and (getKey(3)) and (getKey(4))
    ~100->B
    A+40->A
  End

  .LIMIT SPEED AT 1 PIXEL
  If (B>>256)
    256->B
  End

  .FRICTION
  If A-1=>=>0
    A-2->A
  End

  .FRICTION
  If A//32768
    A+2->A
  End

  .UPDATE POSITIONS
  X+A->X
  Y+B->Y

  .DRAW THE MAP
  Rect(0,0,96,64)
  RectI(4,4,88,56)
  Rect(20,24,12,4)

  .DRAW THE PLAYER
  Rect(X/256,Y/256,4,4)

  .DRAW THE FLAG
  Pt-On(C,E,Pic1)

  DispGraph^^r
End

Basically, enemies are whatever is gray on the screen, so if the player touches something gray, the game stops.

There are two things I need help with:
  • Implementing tilemapping to draw levels in a way that doesn't make the game too slow
  • Make it so that the user doesn't go through walls when jumping

Here's a screenshot of the second:



I made this code a few weeks ago, so I don't exactly remember how everything works, but from what I understand, I'm never checking if there's anything above the player, am I?

I know my help request sounds a bit vague, but it's hard to make it more specific.

324
News / Re: Omnimaga turns 10 years old
« on: September 01, 2011, 02:28:08 pm »
Happy Birthday XD
EDIT: Where's mah cake?

I want some cake too :(

Happy birthday Omnimaga! 10 years of great stuff indeed!

325
News / Re: Only 2 weeks left to the Prizm contest!
« on: September 01, 2011, 02:27:11 pm »
heh I made the reminders for the other two parts, but I'm a bit away now, so thanks DJ!

Good luck to everybody and make sure you finish it on time! :)

326
News / Re: Omnimaga Contest 2011 Part 2 results
« on: September 01, 2011, 02:25:43 pm »
Ouch, last place by the judges feels a bit bad, but my game was all about being addictive and fun, not about the graphics or "shinyness". It's the kind of game everybody in my school enjoys, beating each other's highscores, in a simple game that everybody fancies.

However, I must congratulate Levak, for a great game and all the others too, but mainly Deep Thought and Jim Bauwens. :D

327
The results (by the users) are out. Levak dominated, nice job Levak!

328
News / Re: Winning the game since 3 years
« on: August 25, 2011, 03:37:01 am »
* ephan lost.

This is great news, maybe when September comes the calculator zone will be more awake, because of school starting? I don't know, but it's what happened to me.

And 3 years of this website is great!

329
TI Z80 / Re: On-the-fly screenshots (homescreen only)
« on: August 19, 2011, 05:04:13 pm »

330
TI Z80 / Re: On-the-fly screenshots (homescreen only)
« on: August 19, 2011, 04:58:08 pm »
Maybe we should stop saying everything using screenshots, to keep this topic clean for giving suggestions and reporting bugs on Homer?

Pages: 1 ... 20 21 [22] 23 24 ... 424