|
It is currently Fri Sep 10, 2010 1:23 am
|
View unanswered posts | View active topics
| Author |
Message |
|
Joined: Fri Feb 05, 2010 4:30 am Posts: 60
|
 Sprite groups
Work on my project continues.. now I'm trying to implement bullets into my game. Two problems arise, though: First, the bullets are supposed to disappear when they collide with blocks, which are in the form of a list. I'm not really sure if they do, but they tend to disappear at weird places. Second, if I scroll the screen while the bullet is in the air, it appears to bend to where I'm scrolling. Any help would be appreciated, as always  Check out my project at http://code.google.com/p/strikecraft/downloads/list if you want to test it for yourself. Here's the code for my shoot class:
|
| Sun Mar 14, 2010 9:18 am |
|
 |
|
Joined: Sun Jan 31, 2010 9:39 am Posts: 95
|
 Re: Sprite groups
Looking briefly at the code, it seems like the bullets are processed before the blocks. This means the bullets move, hit the blocks, get deleted, and then the blocks move, so when you render the blocks will have moved from where they hit the blocks, which could cause the effect you're noticing.
|
| Sun Mar 14, 2010 8:48 pm |
|
 |
|
Joined: Fri Feb 05, 2010 4:30 am Posts: 60
|
 Re: Sprite groups
Nope, changing the order of the processing doesn't help. I wish there were a way to render multiple entities without using a spritegroup, though.
|
| Mon Mar 15, 2010 5:23 am |
|
 |
|
Joined: Thu Aug 27, 2009 11:10 am Posts: 65
|
 Re: Sprite groups
I've never actually used a sprite group before. You can just put all of your objects into a list and then loop through that calling update/render on each. That's what I usually do.
_________________ Check out my latest project: http://pyedpypers.org/index.php?page=project&projectid=44 - Cat And Mouse
Please let me know if you make any levels for it, I'd love to include them in the next release.
|
| Mon Mar 15, 2010 10:43 am |
|
 |
|
Joined: Fri Feb 05, 2010 4:30 am Posts: 60
|
 Re: Sprite groups
Yeah, I prefer lists too. I got it to shoot nicely, but it doesn't detect collision with blocks. Also, it lags massively after a few shots, on my 2.5gz 6gb RAM computer.
|
| Tue Mar 16, 2010 10:12 pm |
|
 |
|
Joined: Thu Aug 27, 2009 11:10 am Posts: 65
|
 Re: Sprite groups
The lag could be this loop: You're iterating through the bullets list twice. Try something like: Not sure what's causing the collisions to not be detected. Try the code above, and if it still doesn't work put some prints in after the if to see if something's wrong.
_________________ Check out my latest project: http://pyedpypers.org/index.php?page=project&projectid=44 - Cat And Mouse
Please let me know if you make any levels for it, I'd love to include them in the next release.
|
| Tue Mar 16, 2010 10:34 pm |
|
 |
|
Joined: Sun Jan 31, 2010 9:39 am Posts: 95
|
 Re: Sprite groups
Be careful about those loops where you remove an item from the list you're iterating on. You want instead to do something like which iterates over a copy of the list, then you can remove instances from the real list safely.
|
| Tue Mar 16, 2010 11:21 pm |
|
 |
|
Joined: Fri Feb 05, 2010 4:30 am Posts: 60
|
 Re: Sprite groups
Ah, thanks george, its a lot faster now(is still pretty slow with 8+ bullets on screen), and works when you slice the list. Apparently, though, calling your code won't work, since it doesn't recognize anything in "bullets" when "block" is called after it is. So I just had to call bullet again. Pretty strange and sacrifices some speed, but it works so I'm happy. Thanks  :
|
| Wed Mar 17, 2010 12:11 am |
|
 |
|
Joined: Thu Aug 27, 2009 11:10 am Posts: 65
|
 Re: Sprite groups
I'm going to go through some of my code because I've been just removing items from the list the lazy way... I've read on some mailing list that iterating through a list backwards also prevents this problem, without having to copy the entire list. Does anyone know if this is true? Edit: Here's a link to the post: http://www.python.org/search/hypermail/ ... /0615.htmlLooks like it is safe 
_________________ Check out my latest project: http://pyedpypers.org/index.php?page=project&projectid=44 - Cat And Mouse
Please let me know if you make any levels for it, I'd love to include them in the next release.
|
| Wed Mar 17, 2010 11:14 am |
|
 |
|
Joined: Thu Aug 27, 2009 11:10 am Posts: 65
|
 Re: Sprite groups
The reason it slows down is that 3rd loop. Let's say you have 10 bullets and 50 walls. You will go through the first loop 10 times, and then each time you loop you will go through 50 walls, 10*50 - so 500 iterations. If you then iterate through the bullets list again you're increasing it to 10*50*10 == 5000! So every update you are calling rect.colliderect() 5000 times. To increase the speed, you could do this: Try that and let me know if you have any problems.
_________________ Check out my latest project: http://pyedpypers.org/index.php?page=project&projectid=44 - Cat And Mouse
Please let me know if you make any levels for it, I'd love to include them in the next release.
|
| Wed Mar 17, 2010 11:52 am |
|
|
Who is online |
Users browsing this forum: Google [Bot] and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|