Python Forum
Generating number of objects for a game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating number of objects for a game
#1
Hello!

I've started working on my game in Python, using tkinter (so not any pygame or other gaming libraries). I have already made some steps in my plan, what to create and what should the overall game logic look like, but I stuck on one point. I cannot understand, or I cannot imagine, how to create a generator of number of some, let's say enemies, or cars. What I want to do is to create some road and let a huge number of cars spawning and moving there, each car using the same logic given for cars generally. Please, can you give me any hint, how could I create such a generator of objects, using one and the same logic each? I know I should use some classes and parentship, but that's all I can imagine now.

I will appreciate any help. Thank you!
Reply
#2
You create instances of your class:

class Car(object):
    # class code goes here.

car1 = Car()
car2 = Car()
...
Or better yet, cars = [Car() for car in range(num_cars)].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here's a generator of objects
print([object for i in range(10)])
Output:
[<class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>, <class 'object'>]
Reply
#4
Thank you guys, this idea helped me a lot!

I'd like to know one other thing now, related to mouse events. This is a part of my code:

class ClassName:

    def __init__(self):
        self.canvas.bind("<ButtonPress>", self.click)
        ...

    ... # other methods

    def click(self, event):
        x, y = event.x, event.y
        if x > 150:
            self.canvas.bind('<Motion>', self.choose_car)
            move_car_cursor = True
    
        if move_car_cursor:
            move_car_cursor = False
            self.canvas.bind('<ButtonPress>', self.place_car)

    def choose_car(self, event):
        x, y = event.x, event.y
        try:
            self.canvas.delete(self.car_to_put)
        except AttributeError:
            pass
        self.car_to_put = self.canvas.create_image(x, y, image=self.car1_image)

    def place_car(self, event):
        ... # not implemented yet
So, as you can see, what I want to do is to choose a car (from some menu) and then put this car somewhere in canvas (on a road). But I have somehow lost in how bind functions work. After selecting a car from menu, it's picture should appear behind the cursor (while moving with cursor) - that works, but then, after second click somewhere on canvas, this image should be removed and displayed a new one - static on canvas (this is not completely implemented yet). I'm afraid, that the way how did I create these mouse event functions is not, in order to do this, correct. Can you help me?

Thanks.

EDIT: It's me kom2, I somehow lost my account password, but found it again. :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to count the number of tries in guessing game. Frankduc 7 1,847 Mar-20-2022, 08:16 PM
Last Post: menator01
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  guessing the number game go127a 6 4,775 Apr-27-2019, 01:23 PM
Last Post: go127a
  Number Guessing Game - Need Help joekbaum 8 5,264 Jan-04-2018, 02:16 AM
Last Post: ShadowWarrior17
  "Guess the Number" game CTT 14 13,234 Jul-26-2017, 11:41 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020