We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

11

Finding an algorithm is usually best done with a data structure that makes the algorithm easy. In this case, your territory. The territory should be an unordered (O(1) hash) set of borders and elements. Whenever you add an element to the territory, you iterate over adjacent tiles and see if they should be a border tile; in this case, they are a border ...


9

If you need to find edges of holes in the middle of your territory too, then your linear in the area of the territory bound is the best we can do. Any tile on the interior could potentially be a hole that we need to count, so we need to look at every tile in the area bounded by the territory's outline at least once to be sure we've found all the holes. But ...


7

Notice: Whether or not a tile is on the boundary only depends on it and its neighbors. Because of that: It is easy to run this query lazily. For instance: You do not need to search for the boundary on the whole map, only on what is visible. It is easy to run this query in parallel. In fact, I could image some shader code that does this. And if you need it ...


3

Looking in the code, the library provides noise.randomize() which states: Randomize the permutation table used by the noise functions. This makes them generate a different noise pattern for the same inputs. Thus, randomizing the function through the API provided is the most direct way to solve the problem. Here's an alternative solution based ...


2

Move your area up one tile, then up-right, then down right, etc.. Afterwards remove the original area. Merging all six sets should be O(n), sorting O(n.log(n)), set difference O(n). If the original tiles are stored in some sorted format, the merged set can be sorted in O(n) too. I don't think there is an algorithm with less than O(n), since you need to ...


2

Try setting the random seed. A common way to do this is by using the current time as the seed value. import random from datetime import datetime random.seed(datetime.now())


2

A quick recap of how Perlin noise works: It starts by finding the cell your sample point lands in within an integer lattice. From that it determines the integer points that define the corners of that cell. For each corner, it generates a pseudo-random gradient vector, and then computes the projection of your sample point's offset from the corner along this ...


2

Take a look at the pygame.transform.rotate method. It allows you to transform the drawing surface temporarily (don't forget to transform it by the inverse rotation afterwards) so that anything you draw will be rotated. Now, as far as calculating the angle to transform by, the pygame.transform.rotate method, unlike most rotate methods in libraries like this, ...


1

So you have a "one-button" gameplay and it's not engaging enough? Have you played NFSU? It had a drag race mode, which is basically pressing one button in perfect timing to outrace other competitors. Why not try to replace turns with cooldowns. Whenever someone's cooldown reaches zero - they can have their turn and try to press the button in perfect time. ...


1

worldNormal = modelMatrix * vertexNormal; x = Dot(camera.right, worldNormal); y = Dot(camera.up, worldNormal); angleaRadians = Atan2(-x, y);


1

First, instead of storing each platform as a member in class, do it like this: self.platforms=[Platform(200, 550), Platform(600, 550), Platform(1000, 550)] It's the list of all platforms in the game. By storing them in the list, you'll be able to easily add or remove them. Do the same for each object that's going to be more than one in the game (coins, ...


1

As you've pointed out, the other question you linked to is more about how to render a nice graph and numerical change, and not how to decide exactly how or why the number should change. It just states... You start at time T1. You're going towards time T2. Price at time T1 is P1. P2 is end price. You generate a random number (probably based on some events, ...


1

When you use something like self.foo.bar = self from the class bar, you're telling foo which bar it is linked to. Basically, self.scene = scene self.scene.manager = self is used to set reference in both ways, so that from the SceneManager, you can know which scene is managed, and from the scene, you can get access to the scene manager through the ...


1

The code formatting for your answer has mostly broken down, it would be a lot easier to help you if you fixed it. What does it mean for turtles to collide? It means that they occupy the same space, so the easiest thing to do is: if player.position() == player2.position(): print("GAME OVER") quit() however, this doesn't quite work because turtle ...


1

I just wrote a blog post about how to do this. This uses the first method that @DMGregory mentioned starting with an edge cell and marching around the perimeter. It's in C# instead of Python but should be pretty easy to adapt. https://dillonshook.com/hex-city-borders/


1

I'm not sure if it would work well, as I'm not familiar with pygame, but you could stick the value that you want in your Dog object as a float, and do calculations on this value, then use this value converted to int to update the rect.y variable. Dog.computation_y += 0.5 Dog.rect.y = math.floor(Dog.computation_y) You'll be able to control a bit more the ...


1

pygame.sprite.groupcollide returns a dictionary that always includes the sprites in the first group. Therefore, the if test you have written will always return true. You should re-write the function to check whether or not there are sprites in the list for every sprite in the dog group.


1

If your grass tile's position in the tilemap is <Gx,Gy> then its pixel position in the game world will be: tile_worldX = Gx * Tile_Size tile_worldY = Gy * Tile_Size Then you subtract the position of the screen's top-left corner from these coordinates to get their on-screen pixel position: tile_screenX = tile_worldX - view_x tile_screenY = ...


1

You will need to fix your update method and add some variables to your Bullet class: # Bullet Class class Bullet(pygame.sprite.Sprite): def __init__(self, vx, vy): super().__init__() self.image = pygame.Surface([4,10]) self.image.fill(red) self.rect = self.image.get_rect() self.velocity_x = vx self.velocity_y = ...


1

You need to write while difficulty < 1 and difficulty > 3. A comparision needs two elements, and the second statement is currently missing the left hand side operand. Note that the statement does not ever evaluate to true, since an integer cannot be smaller than one and larger than three at the same time. You probably meant to use or here.


1

You should do if-elif-elif with e.type. First import the event constants you need to check from pygame.constants For example from pygame.constants import QUIT, MOUSEBUTTONDOWN while true: for e in pygame.event.get(): if e.type == QUIT: #close the game elif e.type == MOUSEBUTTONDOWN: #mouse click The dir() ...


1

There's no way to do this automatically, so if you want some sort of automation for the surface rescaling, you'd need to write your own wrapper class around pygame.Rect, that owns a rectangle, and takes in a surface, and resizes the surface when a method of the wrapper is called. The resizing function you are looking for is pygame.transform.scale. Pass it a ...


1

I have never worked with Pygame, but are you not defining on init self.onGround = false? self.falling = True self.onGround = False self.v = 0 That would mean that if jump is called, nothing happens. def jump(self): if self.onGround == False: return self.velocity = 8 self.onGround = False Maybe there is something else you ...


1

This often happens also if you do not specify the context before compiling the shaders. For example, for QT Widgets (pytqt) you should have your shaders compiled when the gl context is enabled, for example in the initializeGL function. So in the case of using QT you can do something like: class MyQtWidget(QOpenGLWidget, QWidget): def __init__(self, ...


1

So there's a couple steps to this: You need a list to handle the log messages. You need a function to print events to the list. How you do this is up to you, but it needs to be a function you can call whenever these events happen. Something as simple as a custom print function works just fine. Here's a quick idea of how to make the log itself: list = [] ...


1

Please use the function pygame.key.set_repeat(delay, interval) once before starting the infinite while loop. I use as delay 1 and an interval of 25. From the docs: pygame.key module The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. ...


1

I believe that this is what you were looking for: keys = pygame.key.get_pressed() # checking pressed keys if keys[pygame.K_ESCAPE]: # If 'esc' - QUIT GAME game_exit = True if keys[pygame.K_RIGHT]: # If 'right' - MOVE RIGHT character.move('right') The longer you hold the key down, the more it would move


Only top voted, non community-wiki answers of a minimum length are eligible