Classes and for loops - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Classes and for loops (/thread-5990.html) |
Classes and for loops - LessInformation - Nov-01-2017 this program makes boxes move, but i need to make them move randomly and indepensent of each other, by calling the method "rect.move()"but idk how to do that. ex. of how it should not look like: https://youtu.be/D7rkcA0-BR0 import pygame import random # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) class Rect(): def __init__(self): self.rectXPos = 0 self.rectYPos = 0 self.height = 0 self.width = 0 self.changeX = 0 self.changeY = 0 self.x = 0 self.y = 0 def move(self): self.x += self.changeX self.y += self.changeY def draw(self,screen): pygame.draw.rect(screen,RED,[self.x + self.rectXPos, self.y + self.rectYPos, self.height,self.width]) pygame.init() # Set the width and height of the screen [width, height] size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("My Game") # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() myList = for i in range(10): rect = Rect() rect.rectXPos = random.randrange(0,700) rect.rectYPos = random.randrange(0,500) rect.height = random.randrange(20,70) rect.width = random.randrange(20,70) rect.changeX = random.randrange(-3,3) rect.changeY = random.randrange(-3,3) myList.append([rect.rectXPos , rect.rectYPos, rect.height, rect.width, rect.changeX, rect.changeY]) # -------- Main Program Loop ----------- while not done: # --- Main event loop for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # --- Game logic should go here # --- Screen-clearing code goes here # Here, we clear the screen to white. Don't put other drawing commands # above this, or they will be erased with this command. # If you want a background image, replace this clear with blit'ing the # background image. screen.fill(WHITE) for i in range(10): rect.rectXPos = myList[0] rect.rectYPos = myList[i][1] rect.height = myList[i][2] rect.width = myList[i][3] rect.changeX = myList[i][4] rect.changeY= myList[i][5] rect.draw(screen) rect.move() # --- Drawing code should go here # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(60) # Close the window and quit. pygame.quit()[/i][/i][/i][/i][/i] RE: Classes and for loops - heiner55 - Nov-01-2017 Now more random, you need 10 Rect() (not only one): import pygame import random # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) class Rect(): def __init__(self): self.rectXPos = 0 self.rectYPos = 0 self.height = 0 self.width = 0 self.changeX = 0 self.changeY = 0 self.x = 0 self.y = 0 def move(self): self.x += self.changeX self.y += self.changeY def draw(self,screen): pygame.draw.rect(screen,RED,[self.x + self.rectXPos, self.y + self.rectYPos, self.height,self.width]) # main pygame.init() size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("My Game") myList = [ ] for i in range(10): rect = Rect() rect.rectXPos = random.randrange(0,700) rect.rectYPos = random.randrange(0,500) rect.height = random.randrange(20,70) rect.width = random.randrange(20,70) rect.changeX = random.randrange(-3,3) rect.changeY = random.randrange(-3,3) myList.append(rect) clock = pygame.time.Clock() done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(WHITE) for rect in myList: rect.draw(screen) rect.move() pygame.display.flip() clock.tick(60) # Close the window and quit. pygame.quit() |