New answers tagged python
2
The error is likely the typo in the line:
pygame.sprite.Sprite.__init(self)
The __init__ function in python is special, and should be called as such. That being said, your code would be cleaner and you could avoid these issues if you used new-style class inheritance:
class bullet(pygame.sprite.Sprite):
def __init__(self, bullet_vel):
...
1
The problem is that you are checking the index of the element, not the actual element itself. Substitute list[i] for i and it works, for:
list=[36, 36, 79, 96, 36, 91, 77, 33, 19, 3, 34, 70, 12, 12, 54, 98, 86, 11, 17, 17]
def find(list,x):
for i in range(len(list)):
if x == list[i]:
print ("Found",x,"at position",i)
find(list,12)
...
0
This belongs on stackoverflow.com.
This being said, you check if the current index (i) is equal to the desired number (x), so it will always return the same number as x.
Instead, you want to see if the item in the list at the current index is equal to x, like so: if list[i] == x:.
The index represent the current position that you're at, say first item, ...
0
The event VIDEORESIZE is triggered from windowing system, not from pygame.
In response to event, program should update display with calling pygame.display.set_mode(newsize, sameoptions)
0
You can test the distance between self and target to determine what sprite to use. If the distance is greater than a set MAX, then it looking to left/right otherwise it is just looking down. For example,
let self = (sx, sy) target = (tx, ty)
if (sy < ty)
distx = abs(sx - tx);
if (distx <= MAX_X OR sx == tx)
return "down"
else
...
1
Try using a common class/interface for each of your "states", this is a fairly common approach to state machines (and avoids the long chain of if/else).
You could use just a dictionary of functions, but more often than not you'll likely find you want some form of initialisation functions too, or notification of completion - so may as well go with a ...
2
Ensure your worker threads aren't sitting there spinning and forcing unnecessary time sharing across your CPU. This can be solved using manual resets and signals, rather than a spin lock.
Additionally, consider profiling major portions of your code to identify hot spots, my research brings up the fact that python itself includes a respectable profiler ...
3
Bresenham's algorithm is specifically built to draw circles with fixed-point mathematics; that is, to rasterize circles. For what you're doing you're almost certainly better off with a much more abstract representation of your circular motion — that is, you want to keep track of your character's angular velocity and to simply move it with constant ...
2
Look here: http://www.nongnu.org/pygsear/doc/api_html/private/pygame.sprite.Sprite-class.html
When you subclass Sprite, you must call this pygame.sprite.Sprite.__init__(self) before you add the sprite to any groups, or you will get an error.
That seems to be what is missing in order to make Paddle a proper subclass of Sprite.
0
For actually randomly generating a platform, you define the:
x = max horizontal distance away from last platform
y = max vertical distance away from last platform
Use these values along with a random number generator to figure out the new distance. And like I stated in the previous post, you want to change this as the game proceeds. For instance the max ...
0
I actually made a game like this in cocos2d. I chose something similar to a GDC talk about the making of Jetpack Joyride. Which is:
Randomly generate about 1000 pixels of platforms at a time. Let's call this a section.
When the players starts to get close to the top of this section, go generate another section.
This allows you to maybe kind of add some ...
2
You are probably looking for the absolute value!
You can implement it like this:
def abs(value):
if value < 0:
return -value
else:
return value
You can use it like this:
radius = 20
if abs(playerPositionX - enemyPositionX) < radius:
enemyCollidesWithPlayer()
However, if your game is in 2D, and you wish to find the ...
3
Your problem is the fact that you're only looking at KEYDOWN events.
What you need to do is toggle a boolean value when a key is pressed or released.
Something like this would work:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN: # check for key ...
1
I asked a question here that's very similar, and received the following answer:
The way to deal with this is to set a timer once the person taps the phone. The most user friendly scenario that you'd implement would look something like this:
When you detect a tap, set a timer (t = timeToRepeat)
On each frame, decrease the timer by dt
If the ...
2
I don't know python or pygame, but assuming you're using a game library there should be a way to poll the state of the key, such as if it's currently down or not instead of if it was pressed since last update. Use that for checking and updating movement.
The next problem you will run into is it will update movement as fast as your logic update interval is ...
3
I don't know python, but have you tried setting a bool to true when the key is pressed, and changing the speed in an if statement based on that bool. When the key gets released you just have to set the bool back to false.
0
I GOT IT WORKING!! I'm going to put all the info here that you need to know, as it's incredibly simple once you get it working.
First i'll explain my directory setup:
So i have: src\game\main.py. in source, i have a music, graphics, game and data folder. I have all my .py files in the game folder, and the others are self explanatory. Also inside the source ...
2
Well, consider the following:
The ratio between the max width of the bar and the real width of the bar is the same as the ratio between max HP and HP; so:
width / max_width = hp / max_hp =>
width = hp / max_hp * max_width
0
Have you thought about using an existing service to handle your online leaderboard ?
If you want to focus on your game this might be a better idea.
I think Scoreoid would be particularly suited for your usecase, it handles hosting, user authentication, https security...while still giving you the freedom to implement your user interface and features the way ...
Top 50 recent answers are included
