Questions tagged [game-loop]

The central component of any game, from a programming standpoint, is the game loop. The game loop allows the game to run smoothly regardless of a user's input or lack thereof.

The game loop is the central component of any game and it is generally split into three different pieces: initialization, update, and draw (or render).

During the initialization phase, the game is set up and the environment is prepared for the update and draw routines.

During the update phase, all of the objects and components of the game are updated based on the game's logic, user's input and the time since the last update routine.

During the draw or render phase, all of the objects and components of the game are drawn or rendered to the viewport based on the most recent update.

Once the current frame is rendered, the game loop repeats the execution of the update statements based on the new delta time and then renders again.

485 questions
0
votes
3 answers

How to remove an object from an array inside a game loop (setInterval)

I have a click event which checks if the object in the array should be removed(isSquashed) and when it's true we remove the object from the list of array but when that condition comes I either need to break out of the loop or decrement the i value,…
0
votes
2 answers

Doing position calculation in onDraw it's faster than in game loop thread... why?

I'm developing a simple game which uses normal android views, not openGL or other apis, simply uses views and moves them on the scren. I have a game loop which calls to AsteroidManager.updateAsteroidsPositions() which iterates in all the screen…
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
0
votes
1 answer

Why is sprite character update position causing a trail?

When hitting inputs to re render and move sprite, this causes the sprite to sort of trail/smear on the screen. I have tried to clearRect() but this doesn't seem to make any difference. I have also used this as inspiration…
tmkiernan
  • 375
  • 1
  • 16
0
votes
1 answer

Enemies stop when I press a key in tkinter game

For practice programming, I made a game in Python with turtle graphics and tkinter. I have a problem: if I press a key, the game loop stops and the enemies don't move. Is there any solution for this problem? import random import time import…
Laszlo
  • 1
0
votes
0 answers

How to properly implement handleInput() in a traditional JavaFX game loop?

I'm trying to implement a traditional game loop using JavaFX's AnimationTimer: @Override public void handle(long currentNanoTime) { // calculate time since last update. double elapsedTime = (currentNanoTime - lastNanoTime) / 1e9f; …
AlwaysNeedingHelp
  • 1,851
  • 3
  • 21
  • 29
0
votes
0 answers

Swift: What is a better way to implement a game loop for dice roll game (without using wait delay)

I am building a simple dice roll game. The game cycle is like this: 1) Roll dice and move piece 2) Check if there is extra bonus moves, if Yes then move the piece 3) Check if piece has reached its destination, if Yes then Game ends 4) End turn I…
0
votes
1 answer

How to detect collision only once in Game-loop instead of continuously?

I'm coding a small space Game in JavaScript, how do I detect collisions? I am looping the Game with requestAnimationFrame() and my question is: How do I detect a collision between two bodies, for the counting (death) Variable being incremented only…
0
votes
2 answers

Best way to run code once within while loops?

I'm writing a text-based RPG for a class, and am stuck on a code predicament... from tkinter import * ... runOnce = False nextRun = False """Main Loop""" while True: #New Game initialize if you.rName == None and runOnce == False: …
jon compton
  • 21
  • 1
  • 7
0
votes
2 answers

PHP Game of stones returns boolean instead of winner

The following code is for a game. It creates an array of N consecutive stones, an the players Alice and Bob will alternatively pick two consecutive stones (subtracts the value and leaves a 'gap') each time until there is no consecutive stones. That…
Underfunk
  • 57
  • 2
  • 9
0
votes
1 answer

python Scopage of a function output within gameloop not evaluating

My problem is, after calling the function check_win in the gameloop, even though the function evaluates false(I've checked so this is the case), I assign this false value to gamePlaying inside the loop, however the game loop gamePlaying conditional…
0
votes
1 answer

I'm having some problems using threads in java

I'm new to using threads in general can someone explain why I sometimes get this exception: Exception in thread "Thread-3" java.lang.NullPointerException at Game.tick(Game.java:96) at Game.run(Game.java:73) at…
Mike TT
  • 35
  • 4
0
votes
1 answer

How to refactor my code to a game loop?

Now I have a simple code without a main game loop. I'm rendering the sprites on the canvas using the Loader constructor and its image.onload function (because without image.onload I won't see any sprites) Now I want to animate on of my sprites and…
Limpuls
  • 856
  • 1
  • 19
  • 37
0
votes
2 answers

Understanding game loop

I am trying to follow a tutorial on creating a game in Java but I am having trouble understanding the game loop. I do not understand the purpose of this delta variable. Any help is appreciated. public void run() { long lastTime =…
J.Ramsey
  • 13
  • 3
0
votes
0 answers

How would I run a game loop in a Web Worker and draw to a canvas?

I'm writing a CHIP-8 emulator with JavaScript, and I have determined that it is best to run the CPU in a Web Worker (I'm still not completely sure about that, but I've decided to go with it). I've read that all canvas drawing should be done inside…
robbie
  • 1,219
  • 1
  • 11
  • 25
0
votes
1 answer

Powerbar decrease script, pygame

I'm trying to create a "powerbar." In the following code I can press left & right key to increase the bar, and the prg terminates when the bar_start_width > 400 px. I would like the "powerbar" to decrease while bar_start_width > 0. So, if you stop…