python - Pygame how do I create duplicates of a sprite class then put them in a group? -


hello i'm trying randomly spawn copies of pipes class display them screen. set random number generator has 1/3 chance 1. if gets 1 made random number generator choose number betweeen 0 , 600(the width of screen) x coordinate of new sprite. display sprite screen using random x coordinate , predefined y coordinate.

            class pipes(pygame.sprite.sprite):                     def __init__(self, x):                             pygame.sprite.sprite.__init__(self)                             self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert()                             self.imx = 0                             self.imgy = 375                             self.setdisplay = pygame.display.get_surface()                      def playerrect(self):                             self.x = self.imgx                             self.y = self.imgy                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 32, 32))                      def draw(self):                             self.setdisplay.blit(self.img)                     def update(self):                         self.spawn = random.randint(0, 3)                         if self.spawn == 1:                             self.spawnx = random.randint(0, 600)                             setdisplay.blit(self.img, [self.spawnx, 375])                             allpipes.add(new_instance(self.spawnx)) 

then made group of sprites called allpipes hold new instances of pipe , make constructor pipe.

            allpipes = pygame.sprite.group()             pipe = pipes(0) 

then in gameloop called pipes draw function randomly spawns new instances , draw , update allpipes group images sprite appear on screen dissapear when new 1 appears makes me believe not making new instance of pipes each time. can somone please help?

            def gameloop():                    imgx = 10                 imgy = 10                 lead_x_change = 0                 lead_y_change = 0                 move_variable = 100                 jumpcounter = 0                   while true:                          event in pygame.event.get():                         #print (event)                          if event.type == quit:                             pygame.quit()                             sys.exit()                           elif event.type == timer_event:                                 trail                                 allpipes = pygame.sprite.renderplain(())                                 player.playerrect()                                 #setdisplay.blit(background.img, [0, 0])                                 setdisplay.blit(background.img2, [0, 0])                                 setdisplay.blit(player.img, [player.imgx, player.imgy])                                 #setdisplay.blit(background.img, [0, 0])                                 setdisplay.blit(ground.img, [ground.imgx, ground.imgy])                                 pipe.update()                                 allpipes.draw(setdisplay)                                 sprite in allpipes:                                 sprite.update() 

this full code:

            import pygame             import sys             pygame.locals import *             import random             #creates clock count framerate             clock = pygame.time.clock()             #starts program             pygame.init()             isfalling = true             allpipes = pygame.sprite.group()              #creates window of 800x600             setdisplay = pygame.display.set_mode((800, 600))             pygame.display.set_caption('menu')             #loads image sprite             img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png')             allpipes = pygame.sprite.group()              #player class             class player(pygame.sprite.sprite):                     def __init__(self):                             pygame.sprite.sprite.__init__(self)                             self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert()                             self.imgx = 0                             self.imgy = 375                             self.setdisplay = pygame.display.get_surface()                      def playerrect(self):                             self.x = self.imgx                             self.y = self.imgy                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 32, 32))                      def draw(self):                             self.setdisplay.blit(self.img)                      def load(self, filename):                             self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert_alpha()                              class trail(pygame.sprite.sprite):                     def __init__(self):                             pygame.sprite.sprite.__init__(self)                             self.x = player.imgx - 1                             self.y = player.imgy - 1                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 10, 10))                              self.setdisplay = pygame.display.get_surface()                     def trailrect(self):                             self.x = player.imgx - 1                             self.y = player.imgy - 1                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 10, 10))                     def draw(self):                             self.setdisplay.blit(self.img)                             trail.trailrect()               class background(pygame.sprite.sprite):                     def __init__(self):                              pygame.sprite.sprite.__init__(self)                              self.img = pygame.image.load('c:\\users\\ben\\documents\\background.png').convert()                              self.img2 = pygame.image.load('c:\\users\\ben\\documents\\trees1.png').convert()                              self.treesx = 0                              self.treesy = 70                              self.treesrect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255),pygame.rect(self.treesx, self.treesy, 376, 100))                     def draw(self):                              self.setdisplay.blit(self.img)                              self.setdisplay.blit(self.img2)                      def load(self, filename):                              self.img = pygame.image.load('c:\\users\\ben\\documents\\background.png').convert_alpha()                              self.img2 = pygame.image.load('c:\\users\\ben\\documents\\trees1.png').convert_alpha()             class ground(pygame.sprite.sprite):                     def __init__(self):                             pygame.sprite.sprite.__init__(self)                             self.img = pygame.image.load('c:\\users\\ben\\documents\\ground.png').convert()                             self.imgx = 0                             self.imgy = 400                             self.setdisplay = pygame.display.get_surface()                             self.x = self.imgx                             self.y = self.imgy                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 800, 200))                      def draw(self):                             self.setdisplay.blit(self.img)                      def load(self, filename):                             self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert_alpha()                            class pipes(pygame.sprite.sprite):                     def __init__(self, x):                             pygame.sprite.sprite.__init__(self)                             self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert()                             self.imx = 0                             self.imgy = 375                             self.setdisplay = pygame.display.get_surface()                      def playerrect(self):                             self.x = self.imgx                             self.y = self.imgy                             self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 32, 32))                      def draw(self):                             self.setdisplay.blit(self.img)                     def update(self):                         self.spawn = random.randint(0, 3)                         if self.spawn == 1:                             self.spawnx = random.randint(0, 600)                             setdisplay.blit(self.img, [self.spawnx, 375])                             allpipes.add(new_instance(self.spawnx))              player = player()                           background = background()             ground = ground()             trail = trail()             timer_event = pygame.userevent + 1             pygame.time.set_timer(timer_event, 250)             trail.trailrect()             pipe = pipes(0)             def gameloop():                    imgx = 10                 imgy = 10                 lead_x_change = 0                 lead_y_change = 0                 move_variable = 100                 jumpcounter = 0                   while true:                          event in pygame.event.get():                         #print (event)                          if event.type == quit:                             pygame.quit()                             sys.exit()                           elif event.type == timer_event:                                 trail                                 allpipes = pygame.sprite.renderplain(())                                 player.playerrect()                                 #setdisplay.blit(background.img, [0, 0])                                 setdisplay.blit(background.img2, [0, 0])                                 setdisplay.blit(player.img, [player.imgx, player.imgy])                                 #setdisplay.blit(background.img, [0, 0])                                 setdisplay.blit(ground.img, [ground.imgx, ground.imgy])                                 pipe.update()                                 allpipes.draw(setdisplay)                                 sprite in allpipes:                                 sprite.update()                                       #rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(player.x, player.y, 32, 32))                                 player.imgy += 30                                   isfalling = false                                 groundcolliding = false                                 if isfalling == true:                                         player.imgy += 0                                  if player.rect.colliderect(ground.rect):                                                 print("collided")                                                 isfalling = false                                                 groundcolliding = true                                                 player.imgy = 370                                                 jumpcounter = 0                                            if player.rect.colliderect(background.treesrect):                                         print("c")                                    keys = pygame.key.get_pressed()                                 if keys[k_left]:                                     player.imgx -= 20                                 if keys[k_right]:                                     player.imgx += 20                                 if keys[k_up]:                                     if jumpcounter < 2:                                             player.imgy -= 50                                             isfalling = true                                             jumpcounter += 1                                         print(player.x, player.y)                                 print (isfalling, groundcolliding)                     pygame.display.flip()                     #pygame.display.update()                     clock.tick(70)              gameloop()             #start (0, 71)             #length (376, 71)             #width (0, 168) 

there lot of indentation error. try this:

import pygame import sys pygame.locals import * import random #creates clock count framerate clock = pygame.time.clock() #starts program pygame.init() isfalling = true allpipes = pygame.sprite.group()  #creates window of 800x600 setdisplay = pygame.display.set_mode((800, 600)) pygame.display.set_caption('menu') #loads image sprite img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png') allpipes = pygame.sprite.group()  #player class class player(pygame.sprite.sprite):         def __init__(self):                 pygame.sprite.sprite.__init__(self)                 self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert()                 self.imgx = 0                 self.imgy = 375                 self.setdisplay = pygame.display.get_surface()          def playerrect(self):                 self.x = self.imgx                 self.y = self.imgy                 self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 32, 32))          def draw(self):                 self.setdisplay.blit(self.img)          def load(self, filename):                 self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert_alpha()                  class trail(pygame.sprite.sprite):         def __init__(self):                 pygame.sprite.sprite.__init__(self)                 self.x = player.imgx - 1                 self.y = player.imgy - 1                 self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 10, 10))                  self.setdisplay = pygame.display.get_surface()         def trailrect(self):                 self.x = player.imgx - 1                 self.y = player.imgy - 1                 self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 10, 10))         def draw(self):                 self.setdisplay.blit(self.img)                 trail.trailrect()   class background(pygame.sprite.sprite):         def __init__(self):                  pygame.sprite.sprite.__init__(self)                  self.img = pygame.image.load('c:\\users\\ben\\documents\\background.png').convert()                  self.img2 = pygame.image.load('c:\\users\\ben\\documents\\trees1.png').convert()                  self.treesx = 0                  self.treesy = 70                  self.treesrect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255),pygame.rect(self.treesx, self.treesy, 376, 100))         def draw(self):                  self.setdisplay.blit(self.img)                  self.setdisplay.blit(self.img2)          def load(self, filename):                  self.img = pygame.image.load('c:\\users\\ben\\documents\\background.png').convert_alpha()                  self.img2 = pygame.image.load('c:\\users\\ben\\documents\\trees1.png').convert_alpha() class ground(pygame.sprite.sprite):         def __init__(self):                 pygame.sprite.sprite.__init__(self)                 self.img = pygame.image.load('c:\\users\\ben\\documents\\ground.png').convert()                 self.imgx = 0                 self.imgy = 400                 self.setdisplay = pygame.display.get_surface()                 self.x = self.imgx                 self.y = self.imgy                 self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 800, 200))          def draw(self):                 self.setdisplay.blit(self.img)          def load(self, filename):                 self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert_alpha()                class pipes(pygame.sprite.sprite):         def __init__(self, x):                 pygame.sprite.sprite.__init__(self)                 self.img = pygame.image.load('c:\\users\\ben\\documents\\sprite.png').convert()                 self.imx = 0                 self.imgy = 375                 self.setdisplay = pygame.display.get_surface()          def playerrect(self):                 self.x = self.imgx                 self.y = self.imgy                 self.rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(self.x, self.y, 32, 32))          def draw(self):                 self.setdisplay.blit(self.img)         def update(self):             self.spawn = random.randint(0, 3)             if self.spawn == 1:                 self.spawnx = random.randint(0, 600)                 setdisplay.blit(self.img, [self.spawnx, 375])                 allpipes.add(new_instance(self.spawnx))  player = player()               background = background() ground = ground() trail = trail() timer_event = pygame.userevent + 1 pygame.time.set_timer(timer_event, 250) trail.trailrect() pipe = pipes(0) def gameloop():        imgx = 10     imgy = 10     lead_x_change = 0     lead_y_change = 0     move_variable = 100     jumpcounter = 0       while true:              event in pygame.event.get():             #print (event)              if event.type == quit:                 pygame.quit()                 sys.exit()               elif event.type == timer_event:                     trail                     allpipes = pygame.sprite.renderplain(())                     player.playerrect()                     #setdisplay.blit(background.img, [0, 0])                     setdisplay.blit(background.img2, [0, 0])                     setdisplay.blit(player.img, [player.imgx, player.imgy])                     #setdisplay.blit(background.img, [0, 0])                     setdisplay.blit(ground.img, [ground.imgx, ground.imgy])                     pipe.update()                     allpipes.draw(setdisplay)                     sprite in allpipes:                         sprite.update()                           #rect = pygame.draw.rect(setdisplay, pygame.color(0, 0, 255), pygame.rect(player.x, player.y, 32, 32))                     player.imgy += 30                       isfalling = false                     groundcolliding = false                     if isfalling == true:                             player.imgy += 0                      if player.rect.colliderect(ground.rect):                                     print("collided")                                     isfalling = false                                     groundcolliding = true                                     player.imgy = 370                                     jumpcounter = 0                                if player.rect.colliderect(background.treesrect):                             print("c")                        keys = pygame.key.get_pressed()                     if keys[k_left]:                         player.imgx -= 20                     if keys[k_right]:                         player.imgx += 20                     if keys[k_up]:                         if jumpcounter < 2:                                 player.imgy -= 50                                 isfalling = true                                 jumpcounter += 1                             print(player.x, player.y)                     print (isfalling, groundcolliding)         pygame.display.flip()         #pygame.display.update()         clock.tick(70)  gameloop() #start (0, 71) #length (376, 71) #width (0, 168) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -