Python Forum

Full Version: I want to add a bullet function to my game. how would i go about it?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm relatively new to python and I'm currently following a tutorial for a fighting game in pygame, but i want to add my own changes and additions. I'm trying to add a shooting function, but i have no idea how to go about it. I've seen the list method but don't know how to implement it because my game uses a player class and a main function, and i want to be able to assign the function to a button press.[attachment=2695][attachment=2694]
I would write Bullet as a subclass of Sprite. When firing a bullet I would add a bullet object to a Group. The bullet group will keep track of all the fired bullets, calling their update methods and removing bullets that collide with another sprite (like a player).
is Sprite the Player class? also, would i make a list and append the bullet object to it? is a Group a list? sorry for asking so many questions at once, i just want to be sure of what I'm doing.
Sprites and Groups may be the most important concepts in pygame. If they were not mentioned in your tutorial, I don't think the tutorial was very good.
(Jan-02-2024, 05:23 PM)Iyjja Wrote: [ -> ]I'm relatively new to Python and I'm currently following a tutorial for a fighting game in Pygame, but I want to add my own changes and additions. I'm trying to add a shooting function, but I have no idea how to go about it. I've seen the list method but don't know how to implement it because my game uses a player class and a main function, and I want to be able to assign the function to a button press.

If you want to add shooting in your pygame-based game you need to modify the player class to include a bullets list and a shoot method and create a Bullet class for bullet attributes and methods in the main function, called player. shoot() on a key press to spawn bullets.
(Jan-04-2024, 06:11 PM)deanhystad Wrote: [ -> ]Sprites and Groups may be the most important concepts in pygame. If they were not mentioned in your tutorial, I don't think the tutorial was very good.

i found a video that explains how to use sprites and groups. i think this is what i needed, thanks!