Thwack a square

Using the delay code that gives some time before the serve in the pong clone, you can switch between vertical positions per update. So you can make a “whack-a-mole” type game. Links: gif1 gif2

thwack

The squares can be instantiated with a delay, although this number is the same as their retreat (push-in) time – which makes sense: the harder “moles” present themselves for briefer moments. There is also a delay in the appearance and disappearance of the “thwack!” feedback text (visible in the second gif), programmed the same way.

if (shouldDisplayText) {
  g.setColor(Color.RED);
  g.drawText("thwack!", x, y);

  if (--delay < 0) {
    delay = defaultDelay; // reset
    textShouldDisplay = false;
  }
}

Overriding default state from external context

The program default gets superceded by the command line invocation, but only if the user includes an argument. The invoking script can be scheduled with defaults, but for debugging can be run with particular parameters.

@echo off

setlocal enabledelayedexpansion

set var1=yes

if not "%1"=="" (
  set var1=%1
)
echo VAR1 !var1!

endlocal
goto :eof

For debugging and testing, it’s nice to run a specific state with the same script that also works with the defaults.

Twenty-seven tokens

I finally had to use perl instead of batch scripts for a really long CSV-formatted file, because for /f only separates up to twenty-six tokens.

for /f "delims=, tokens=27" %%i in (csv-file.txt) do (
  echo nope
)

With perl,

my @fields = split(/,/, $line);
print $fields[26] . "\n";

Just like that.

Another means of web scraping

Using AutoIt, you can get a window handle on Internet Explorer. AutoIt will build the DOM tree. Awesome. From there, you can reference the form and its constituent elements. Here I salute the community, because of the User-Defined Functions (UDFs) in IE.au3.

One example use is transferring data from one source to another. Manual data entry is slow and error-prone; with one click, you can copy the data and fill in the fields, just like that. If you can save one person his time, multiply it tenfold or a hundredfold – that’s the real value, day in and day out.

Actually, I had to say, “I’m not sure we need more than one programmer.” I don’t know when I would need a second person. Sure, if we acted as independent consultants. There is an overhead to programming in a team, but the advantage is asynchronous productivity.

I haven’t looked at my work and thought, “Oh, if only there were a second person.” So far, everything seems tractable and viable. And if not that, at least learnable.

All just loops before we hear about for

Addition, multiplication and now summation – all as iterative operations:

4 + 7 is four plus seven ones, or four with ones stacked, for a sum of eleven.

5 * 12 is five added to itself eleven times, giving sixty.

Summation is interesting because it is the idea of adding increasing numbers together, like 1 + 2 + 3 + … + n. We could look at it as successive treatments of the number one:

((((1) + 1 + 1) + 1 + 1 + 1) + … + n) [1]

When n is 4, the summation from 1 to 4 is

1 + 2 + 3 + 4 = 10

Or,

  ((((1) + 1 + 1) + 1 + 1 + 1) + 1 + 1 + 1 + 1)
                                 ^__n terms__^
= ((((1) + 1 + 1) + 1 + 1 + 1) + n)

So I stop taking binary operations and symbols for granted, knowing we could be decomposing numbers into ones and iterating thereby. Does that make notation a form of abstraction?

Unit selection in an early RTS

Objects repeatedly drawn with empty update() methods remain static (of course): their velocity doesn’t change from zero, so their positions don’t change.

piece-rts

The pieces are composites of fillOval() and fillRect() methods. A mouse press event sends the cursor coordinates to the model, which compares each piece’s x- and y-position. The object is then referenced with a separate instance (outside the array) called selectedPiece. So during the draw update,

for (Piece p : pieces) {
  if (selectedPiece == p) {
    context.setColor(Color.GREEN);
    context.drawOval((int)selectedPiece.getX()- ...);
  }
  p.draw(context);  // so we draw all the pieces
}

I really do need to finish the Pong example. Parallax backgrounds, loading images, and AffineTransformed minimaps (Harbour) await.

https://aptavout.files.wordpress.com/2014/03/piece-rts.gif

The new province

The server was a splurging mess, throwing out session IDs with every request. It was a decoy, because the traffic trails showed something else: one stable cookie, but it couldn’t be caught. It was the Higgs cookie, the field at which authenticated traffic could achieve useful mass, but it couldn’t be contained.

The best I could do was copy it. I ran the simulation to spit out the same pattern and sent out the packets. Bingo, success: observations matched results. Each tacit approval was a step closer to total command-line control of a once-manual process. What remained after was to get acquainted with the neighborhood.

A part of me wants to convert all of it to an Access database, or a federated version of loosely-associated data contexts. Smaller, simpler components and such, even with databases.

“Are you ready to leave?” I asked. And his answer was, “They don’t pay me enough.”

drawString() versus FontMetrics

The second chapter of “Java Game Programming for Dummies” implements a Pong clone with state-driven design. I made an example of a start screen before the game begins:

start-game-menu

The red dot is the (x, y) position sent to drawString(); unlike other drawXXX() methods, the string is drawn with the origin in the lower-left corner. This might be related to the font’s “ascent” property. So there are two places where I consider this: the first is whether the player’s mouse click is within the “Start game” area.

if (clickedX >= chooseStart.getX() &&
    clickedX <= chooseStart.getX() + chooseStart.getWidth())
  if (clickedY <= chooseStart.getY() &&
      clickedY >= chooseStart.getY() - chooseStart.getHeight())
        gameState = WAIT;

Note the subtraction in the second conditional. The usual hit test would assume the upper left corner, but for drawn strings it is different. Also, getHeight() returns fontRenderHeight, a private integer of my SpecialText instance.

The green rectangle grows with the text size, a useful debugging visual. Here’s a link to a GIF of the menu.

The second place is retrieving the font metrics information. First, set the graphics context to the desired font and then get the FontMetrics object:

public void draw (Graphics g) { 
  g.setFont(new Font("Dialog", Font.PLAIN, 24);
  
  FontMetrics oFM = g.getFontMetrics();
  fontRenderHeight = oFM.getAscent();
}

Observing data on data

The menubar obscures the world bounds, so objects get drawn underneath it: bouncing spheres pass above and return, inscrutable. This was solved with

menuBar.setOpaque(false);

Trying to extend the learned examples as far as possible, I added horizontal, vertical and resultant vector components:

transparent-jmenubar-plus-resultant

The vector lines were multiplied by 10 because the dx and dy magnitudes were small enough to appear nearly as points. Interestingly, it didn’t matter whether dx or dy were positive or negative; the calculations were the same:

// preamble: all vectors start at the circle's center
hx = vx = rx = source.getX() + source.getWidth() / 2;
hy = vy = ry = source.getY() + source.getWidth() / 2;

if (source.getDX() < 0) {
  hx2 = hx + source.getDX() * 10;
  hy2 = hy;
} else {
    hx2 = hx + source.getDX() * 10;
    hy2 = hy;
}
// vertical vector coordinates
vy2 = vy + source.getDY() * 10;
vx2 = vx;

rx2 = hx2;  // resultant
ry2 = vy2;

Is it because the square of the distance is positive? So

rx2 = hx2 = hx + source.getDX() = (hx - source.getDX())^2 // ???

This “learning lab” fits in with the earlier topic on moving from concrete, sample data to metadata, and then to analysis and rules discovery – except instead of names we have visual evidence; instead of tables, illustrations.

Gamer ascendant

I tried a different thing while programming today: I sat down with the data and began two things: first, to write down the “metadata”; and second to note a rule.

For example, names parsed out of a workbook: the common ones are two words and some are four words long. Some names have a suffix “Jr.” and others’ last names have missing hyphens. My task was to parse this set of names for a one-off migration.

Where there were exceptions – not many – I was explicit in their priority; otherwise, it worked alright. It was a meditative exercise to write on paper the act of data gathering – you wouldn’t have caught me with this pensive attitude in biology lab. Yet I experienced flow.

Going from concrete data – examples, made-up inputs, imaginative brainstorms – to metadata like the length or number or other measurements, and then to the rule made me think of gamers. Experience, like knowing the rules and its exceptions, wrapped round them like a cloak, bringing them farther down the journey.

That’s when I rightly associated making games with playing them as one and the same, except creating them still taking a higher position because of execution of the implementation details.