Jump to content

How Do Games Work?


Recommended Posts

I guess this would most closely relate to java so I will use java as an example. While I'm familiar with web programming, I am a novice at desktop languages. I am taking a java class and so far all of my programs have a start and an end. The only time I can repeat actions is through a loop but that halts all other scripts while it completes this loop. Years ago I made some games in macromedia flash (back when it was macromedia still) and I'm sure the way it worked was it would loop through all of the script as fast as the processor could handle. I'm not trying to make a game but I've been wondering how it's done in java. Lets say you make a plat-former and you character can move left-right, and jump. You can also shoot an arrow which has physics applied to it. Meanwhile you have ai character that are attacking you. How would the algorithm looks for this? Does it use a loop to constantly check conditions?

[num] <- 1while [num] = 1	 if left key down   /	   [playerPosX] <- [playerPosX] - 1   \	   // play walking animation	 end if	 if right key down   /	   . . .   \	   . . .	 end if	 // More movement stuff like walking and jumping	 // Move enemies	 // If enemies are in range, attack	 // If arrows are in the air, calculate it's next position based on speed, gravity, and angle	 // And a bunch of other stuffend while

And you loop through that the entire game. This seems very inefficient and primitive but I'm not sure how these games work because I'm only familiar with flash and I didn't pay attention to the mechanics of it. I just stole code snippets off the internet and applied them to movie clips (flash for objects).Also, that brought up another question. How do mmorpgs work? My theory is that when you make a request to the sever you are doing it at a speed of hundreds of times a second. Constantly checking what information is on the server. And depending on what information is present you turn it into what you see on the screen. So if you move your character forward, you send a request to the server to update your location. It makes the calculations and updates the database referring to you location. Then all the other players, including you, can see where you have moved after making a request to the database. But this boggles my mind when I think about how long it takes to get mysql information via php or ajax. Maybe my theory is incorrect, perhaps someone can shed some light on both subjects?

Link to post
Share on other sites

You can continuously loop, but a more efficient way is to use event handling so that actions are only triggered in response to player events. The "enemies" and other stuff can be controlled in separate threads so that calculations can be done concurrently with the player's actions. So, while the player is standing still, the player-movement thread can be sleeping, but the arrow-moving-through-air thread can still be updating the position of the arrow on-screen. Then, when the arrow hits the player, another thread may be spawned to determine the player's damage, etc.For games involving networking, such as MMORPGs, rather than the client constantly check with the server, the server can simply push out new information when it is available. HTTP is a pull-only protocol, that is, the client has to request data from the server before the server can send information, but other protocols allow for the server to send information unsolicited, as long as the client is listening for it.

Link to post
Share on other sites
  • 2 weeks later...

If you have an action that happens more than once, you will need to execute that block of code more than once. Note that at the Assembly level, there are no "looping structures" as such, just the ability to jump to certain instructions.At a hardware level, devices (such as your keyboard) can send interrupts to the OS, indicating that something has happened on that device. As the page says:

Hardware interrupts were introduced as a way to avoid wasting the processor's valuable time in polling loops, waiting for external events. They may be implemented in hardware as a distinct system with control lines, or they may be integrated into the memory subsystem.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...