Bottom Page

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!
Quote
#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)].
dan789 likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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'>]
dan789 likes this post
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  More Efficent Way of Generating Permutations/ w Rep ClassicalSoul 2 207 Yesterday, 05:22 AM
Last Post: perfringo
  An Object of Objects? cebb 4 207 Jul-28-2019, 03:29 PM
Last Post: cebb
  rotating and resizing objects jenya56 3 165 Jul-26-2019, 12:06 AM
Last Post: scidam
  Does XLRD have a method to extract embedded objects (pdf) from an excel sheet? matthewme 1 198 Jun-13-2019, 10:00 PM
Last Post: Larz60+
  Creating Dynamic Objects MacFie 4 330 Jun-13-2019, 02:48 PM
Last Post: MacFie
  guessing the number game go127a 6 348 Apr-27-2019, 01:23 PM
Last Post: go127a
  Moving with objects simultaneously kom2 1 205 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  Why is this pointing to the objects hshivaraj 6 280 Apr-09-2019, 10:55 PM
Last Post: hshivaraj
  Generating a polynomial equation BinaryStar 3 385 Mar-18-2019, 12:53 AM
Last Post: scidam
  How to redefine object so that all user- objects have the necessary capabilities? AlekseyPython 5 359 Mar-03-2019, 04:25 AM
Last Post: AlekseyPython

Forum Jump:


Users browsing this thread: 1 Guest(s)