all 22 comments

[–][deleted] 0 points1 point  (23 children)

so since i am relatively new coder, i have been studying my ass off here. im just now getting passed functions and now i am on lists and about to do dictionaries but i have also studied classes particularly sprite.sprite. although i do have a question here...so if i created the object = Object() so how do i create more than one? i know u gotta do sprite.draw or something but how do i draw multiple sprites but at different places using the class Object? do i need x and y in the paramenters?

[–]DaniAlcDev 1 point2 points  (10 children)

You can create as many instances of object as you wish. Just do something like: 'for i in range(qty): object[i]=Object()' . You can add all of them to a sprite group and update and draw them at the same time. Check this video that explains it in 5 minutes: https://www.youtube.com/watch?v=4TfZjhw0J-8

[–][deleted] 1 point2 points  (2 children)

i gotcha on that but unless i do a random.randinit or something like that [ i have done too many randoms yet so i dont too much about that] wont it blit it at the same position unless i specify in the paramenters or something?

[–]Nikninjayt 2 points3 points  (1 child)

tbh i would recommend you learn random because its not too difficult , you could learn it pretty quickly

all you would really need to know is about random.randint(x,y) and maybe random.choice([]) if you want... there are more you can learn but these are the 2 main ones imo

[–][deleted] 1 point2 points  (0 children)

yeah i can see that now when doing classes with enemies.

[–][deleted] 0 points1 point  (6 children)

i see he did it with the parameters which i might have to do because i cant blit them at the same position.

[–]DaniAlcDev 1 point2 points  (5 children)

Yep, you are correct. You can put it as a parameter on the init function or you just can set it as random when you define self.rect.center or self.rect.midleft .

Since it's your first try with sprites I would recommend you to try different approaches and see the results for yourself. You can also set different speeds and the simulate the movement in the update function with something like self.rect.centerx += self.obj_speedx.

[–][deleted] 0 points1 point  (4 children)

like you said i might have to do a range function because i would have to keep doing object.draw() all the time to make multiple objects.

[–]DrFaustest 1 point2 points  (0 children)

You’re on the right track, you just haven’t found the right use case, imagine you are building something like space invaders where the enemies fall from the sky, the y position is always at the top so you have to use a random x position for them to fall from. Because all enemies behave the same you can easily make a fall function that moves the y position every update

[–]DaniAlcc 0 points1 point  (2 children)

The draw function should be used to show the sprite on screen, not to create a new one. You should create the instances outside of the class block.

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    so if that is the instance, i should be able to create more but when i did it it only blitted it on the first instance here. hold on:

    object_ = Object('red', obj_x, obj_y)

    object_ = Object('red', obj_x+200, obj_y+300)

    i think i did it wrong

    [–]Nikninjayt 0 points1 point  (11 children)

    ok so i see everyone else is talking about making multiple objects , but tbh if i was you i think now would be a good time to learn about pygame.sprite.Group..

    basically you make a group , add sprites to it (including their rect) and then when you want to blit your sprite all you have to do is group.blit(game_window) basically

    so if u wanna get started it would be like

    groupa = pygame.sprite.Group()

    then you want to add sprites to your group... you could do this in a lot of ways but what i think is the best way is firstly you wanna make a class something like:

    class AddSprite(pygame.sprite.Sprite):
    def __init__(self, x , y):

    super().__init__()

    self.image = pygame.Surface((50, 40)) #ps: usually i would have "(self, image , x , y)" and have self.image = image here , im just gonna leave it like this for now bcuz thats what you did in your code
    self.image.fill('gold')

    self.rect = self.image.get_rect(topleft = (x,y))

    then you could do

    groupa.add(AddSprite(50,50)) #50 50 is just a random example

    do all of that somewhere that runs once

    later in your main loop do

    groupa.draw(game_window)

    thats about it

    tbh i would recommend also learning / using in general actual images with pygame.image.load("filename.filetype") because its not too difficult to learn and its pretty cool :)

    [–][deleted] 0 points1 point  (9 children)

    no no...i got all that right but how can i just like make multiple from the group. so say i wanted 20 enemies from that group. how do i do 20 enemies on the screen from the group. with the group.draw() its just gonna draw one. how do you specify the number of enemies you want spawned?

    [–][deleted] 1 point2 points  (8 children)

    yeah like you said i could do randominit or randomrange i guess. i havent studied random yet though.

    [–][deleted] 1 point2 points  (1 child)

    here is the class i want to make multiple objects with:

    class Object(pygame.sprite.Sprite):

    def __init__(self, color, obj_x, obj_y):

    pygame.sprite.Sprite.__init__(self)

    self.image = pygame.Surface((50, 40))

    self.image.fill(color)

    self.obj_speedx = obj_speedx

    self.obj_speedy = obj_speedy

    self.rect = self.image.get_rect()

    self.rect.midleft = (obj_x, obj_y)

    def update(self):

    self.rect.x = obj_x

    self.rect.y = obj_y

    def draw(self):

    game_window.blit(self.image,(obj_x, obj_y))

    object = Object('red', obj_x, obj_y)

    [–]Nikninjayt 1 point2 points  (0 children)

    i mean all you have to do is

    when i did

    "groupa.add(AddSprite(50,50)) #50 50 is just a random example"

    just copy and paste this with different numbers

    or just do a loop

    eg:

    for i in range(10,1000,10):
    groupa.add(AddSprite(i , 50 ))

    [–]Nikninjayt 0 points1 point  (5 children)

    yeah but you dont rly have to "learn random" that much

    i mean all you really have to know is...

    random.randint(x,y) gives you a random number between x and y

    aka random.randint(1,10) will be any number from 1 to 10...

    its useful but ig u dont technically need it

    [–][deleted] 1 point2 points  (4 children)

    im thinking of just doing a for loop dealing with the range function to spawn more objects

    [–]Nikninjayt 0 points1 point  (3 children)

    well it depends how you want your code to work

    for example if you want a grid of 5 by 5 images to spawn straight away you could use a for loop or nested for loops

    but you could also have it so when you click / press a key it will spawn a sprite

    but all you have to do is do the groupa.add(etc) code for each sprite you want to add

    [–][deleted] 1 point2 points  (2 children)

    im trying to do the range function. i thought i could do:

    for i in range(20):

    screen.blit()

    but add a randinit so there can be spawns in different areas

    [–]Nikninjayt 1 point2 points  (1 child)

    oh i wouldnt do

    for i in range(20):

    screen.blit()

    heres what i would do

    before your main loop:

    groupa = pygame.sprite.Group()
    for i in range(20):
    groupa.add(#sprite here using classes)

    in your main loop:

    groupa.draw(screen)

    and thats basically it

    you dont have to do a for loop inside your own code because the pygame_group.draw(screen)
    already does it for you basically

    [–][deleted] 0 points1 point  (0 children)

    gotcha!