TIL

Web Dev Bootcamp TIL Day-6(Game Project Start)

frannyk 2022. 4. 26. 09:07

Breakout Game clone project

 

Using OOP for organizing gaming is convenient

  • OOP allows us to not cluster the main function as we can simply call methods
  • We can use a class for each component(character, stage, weapon, etc...)
  • create instance variables for each component
    • ex) for a ball, we assign its x and y coordinates, its radius, its speed, etc...
  • create methods for each component
    • ex) def draw_ball()

 

Use a nested for loop for creating grids

  • create a master list that will contains all blocks and their positional information that will be assigned to the grid
  • create a nested for loop ---> for loop that iterates through columns within a for loop that iterates through rows:
  • for each row iteration, we want to return back to master list a collection of blocks that have been assigned respective positions based on column iteration
def create_wall(self):
    #list for collection of final blocks
    self.blocks = []
    #define an empty list for an individual block
    block_individual = []
    for row in range(rows):
        #reset the block row list for each row iteration
        block_row = []
        for col in range(cols):
            #generate x and y positions for each block and create a rectangle from that
            block_x = col * self.width #x_pos is updated for each col iteration
            block_y = row * self.height #y_pos remains constant as it is dependent on row value
            rect = pygame.Rect(block_x, block_y, self.width, self.height) #create rect value based on x,y coordinates and width and heigh value
            #assign block strength value based on row position
            #the closer the block is to the paddle, the more durable
            if row < 2:
                strength = 3
            elif row < 4:
                strength = 2
            elif row < 6:
                strength = 1
            #create a list to store the rect and color data
            block_individual = [rect, strength]

            #append that individual block to the block row
            block_row.append(block_individual)

        #append the row to the full list of blocks
        self.blocks.append(block_row)