Join us on Discord!
You can help CodeWalrus stay online by donating here.
Menu

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.

Show posts Menu

Messages - kotu

#856
Quote from: p2 on October 31, 2016, 10:32:35 PM
right, maybe we should move these posts to a topic like "simple key stuff in java" ^^

done!
#857
from what i understood there were about 250 actual instructions, but they only really counted as about 150
#858
This topic was suggested by @p2 and focusses on getting keypress events in java.
It also features some tile rendering code which I made previously.
It features a little walrii, who you can walk around a map.
Also pressing space will change the terrain.

Here is the code.... hopefully it will explain itself.... if not then just ask what you want explaining!

Many thanks

Here is the source code....... the first file contains all the keypress stuff, the second two files are the tile rendering stuff. Feel free to use both if you want

TileTestApp.java
package tiletest;

import javax.swing.JFrame;
import tiletest.TileArea;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class TileTestApp implements KeyListener
{
private int m_walriix = 280, m_walriiy = 180;
private boolean m_keyDown = false;
private boolean m_keyUp = false;
private boolean m_keyLeft = false;
private boolean m_keyRight = false;
private int m_bgTile = 0;

public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
m_bgTile++;
if (m_bgTile > 7) {
m_bgTile = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) m_keyLeft = true;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) m_keyRight = true;
else if (e.getKeyCode() == KeyEvent.VK_UP) m_keyUp = true;
else if (e.getKeyCode() == KeyEvent.VK_DOWN) m_keyDown = true;
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT) m_keyLeft = false;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) m_keyRight = false;
else if (e.getKeyCode() == KeyEvent.VK_UP) m_keyUp = false;
else if (e.getKeyCode() == KeyEvent.VK_DOWN) m_keyDown = false;
}
public void keyTyped(KeyEvent e)
{
}

TileTestApp()
{
JFrame frame = new JFrame("kotu java demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add TileArea to the JFrame here
TileArea tileArea = new TileArea(600, 408);
frame.add("Center", tileArea);
frame.pack();
    frame.setVisible(true);
    frame.addKeyListener(this);

// load tileset
TileSource tiles = new TileSource("all-3.png", 24);

//----
for (;;)
{
if (m_keyRight) m_walriix += 4;
if (m_keyLeft) m_walriix -= 4;
if (m_keyUp) m_walriiy -= 4;
if (m_keyDown) m_walriiy += 4;

for (int x = 0; x < 25; x++) {
for (int y = 0; y < 17; y++) {
tileArea.drawTile(tiles, m_bgTile, 3, tiles.getTileSize()*x, tiles.getTileSize()*y);
}
}

tileArea.drawTile(tiles,  2,  1, m_walriix, m_walriiy);

frame.repaint();

try { // wait 10ms to avoid any flicker
    Thread.sleep(10);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
}

}

public static void main(String[] args)
{
new TileTestApp();
}
}


TileArea.java
package tiletest;

import java.awt.*;
import java.awt.image.*;

public class TileArea extends Component
{
private static final long serialVersionUID = 1L;
private BufferedImage m_image;
private int m_width, m_height;

public TileArea(int width, int height)
{
m_width = width;
m_height = height;
m_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}

public Dimension getPreferredSize()
{
        return new Dimension(m_width, m_height);
    }

public void paint(Graphics g)
{
g.drawImage(m_image, 0, 0, null);
}

public void drawTile(TileSource tileSource, int tilex, int tiley, int x, int y)
{
Graphics2D g = m_image.createGraphics();
    g.drawImage(tileSource.getTile(tilex, tiley), x, y, null);
}
}


TileSource.java
package tiletest;

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;

public class TileSource extends Component
{
private static final long serialVersionUID = 1L;
private BufferedImage m_tiles;
private int m_tileSize;

public TileSource(String filename, int tileSize)
{
m_tileSize = tileSize;
try {
m_tiles = ImageIO.read(new File(filename));
}
catch (IOException e) { // no need to do anything here
} // m_tiles will be null anyway

if (m_tiles == null) {
JOptionPane.showMessageDialog(null, "p2, no! could not load tileset.");
return;
}
}

public int getTileSize()
{
return m_tileSize;
}

public BufferedImage getTile(int tilex, int tiley)
{
if (m_tiles == null) {
return null;
}
return m_tiles.getSubimage(tilex*(m_tileSize+1)+1, tiley*(m_tileSize+1)+1, m_tileSize, m_tileSize);
}
}


You will also need to include this image in your project (from p2's game)


#859
And to properly learn it you have to learn like over 250 instructions.... i wanted to learn z80 but I couldn't really be bothered.... don't see much to be gained.

However you are right I am very used to C++/STL.
#860
also, i wouldn't really recommend ASM to a beginner, as it will make it very slow to write proper programs or big programs.
#861
I think I read that the definitions of these functions are implementation-specific, so could be
#863
General Music Talk / Re: Yay my decks arrived!!!
November 01, 2016, 05:48:53 AM
Quote from: Juju on November 01, 2016, 04:51:26 AM
Oooh, nice DJ deck. Looking to do some dank mixes?

looking to try and mix

Quote from: DJ Omnimaga on November 01, 2016, 04:53:13 AM
Those look great. :D How much did they cost?

a lot
#864
Other / Re: Halloween
November 01, 2016, 05:47:54 AM
what's unusual about those images dj?
#865
learn TI-BASIC or C

do not learn ASM yet if you are a beginner


*edit*

@xMarminq_ try this

http://tibasicdev.wikidot.com/sk:first-program
#866
Media Talk / Re: Funny/awesome Youtube videos
October 31, 2016, 11:36:33 PM
#867
General Music Talk / Re: HARDWIRED... TO SELF-DESTRUCT
October 31, 2016, 11:05:46 PM
Quote from: aeTIos on October 31, 2016, 09:49:07 PM
Quote from: kotu on October 31, 2016, 09:38:13 PM
why does he stab his drums at the end it doesn't seem very professional. i would say he doesn't know what he's doing
(not sure if /s)
It's Lars, such things are to be expected. I think the very tight drumming during the song serves as a testimony for his drumming.
i think he just got lucky, and at the end he was trying his hardest to play the drums correctly
#868
Site Discussion & Bug Reports / Re: CodeWalrus Ads
October 31, 2016, 11:02:47 PM
it looks racist
#869
General Music Talk / Yay my decks arrived!!!
October 31, 2016, 10:58:36 PM


(XDJ-700s)
#870
Other / Re: Halloween
October 31, 2016, 10:11:55 PM
Quote from: DJ Omnimaga on October 31, 2016, 10:07:48 PM
So did anyone have anything planned for Halloween? I'm years past my trick or treating years, but I went to an Halloween party last night with a Jason Vorhees costume. I am tempted to watch an horror movie with ghosts later but I'm still unsure (it needs to be scary).

Some stuff also changed on the forums for a few hours :P

the eye is quite good its a japanese film i think. not very scary though
Powered by EzPortal