funtion draw loop???? pythin pygame

Viewed 13

I just started learning python and pygame yesterday. I have a specific question. I want to know if there's a way to set parameters to draw a square in like a def function and then call upon that function in an if statement to draw the square. I want to be able to call upon that function multiple times and it to create new squares over and over again without deleting the previous one created. Also, I want to know if there is a way to then assign a variable to the square that the function created. I am trying to create connect four and have one square that I can move around and drop in place, but I can't figure out where to go next without watching a youtube video, but that's cheating.

Here's my code although it doesn't really have anything to do with the question.

import pygame
import time
pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
background_main_color = (107,142,35)
background_corner_color = (0,100,0)
game_grid_start_x = 150
game_grid_end_x = 650
game_grid_end_y = 590
game_grid_x_size = 500
game_grid_y_size = 390
block_size_width = game_grid_x_size/7
block_size_height = game_grid_y_size/6
block_stop_y = game_grid_end_y + block_size_height
display_width = 800
display_height = 600


def square_function_draw ():
  game_display.fill(red, rect = [block_position_x,block_position_y,block_size_width,block_size_height])

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Connect 4")



game_exit = False

block_position_x = 400
block_position_y = 80
block_change_y = 0
clock = pygame.time.Clock()
FPS = 45
button_down = False
game = True



while not game_exit:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      game_exit = True

    if button_down == False:
      if event.type == pygame.MOUSEMOTION:
        pos_mouse_x, pos_mouse_y = pygame.mouse.get_pos()
        if pos_mouse_x > game_grid_start_x + block_size_width* 0.5 and pos_mouse_x < game_grid_end_x - block_size_width *  0.5:
          block_position_x = pos_mouse_x - block_size_width/2
        elif pos_mouse_x < game_grid_start_x:
          block_position_x = game_grid_start_x
        elif pos_mouse_x > game_grid_end_x - block_size_width:
          block_position_x = game_grid_end_x - block_size_width

    if event.type == pygame.MOUSEBUTTONDOWN:
      if pos_mouse_x > game_grid_start_x and pos_mouse_x < game_grid_start_x + block_size_width:
        button_down = True
        block_position_x = game_grid_start_x
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width and pos_mouse_x < game_grid_start_x + block_size_width*2:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width*2 and pos_mouse_x < game_grid_start_x + block_size_width*3:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width*2
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width*3 and pos_mouse_x < game_grid_start_x + block_size_width*4:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width*3
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width*4 and pos_mouse_x < game_grid_start_x + block_size_width*5:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width*4
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width*5 and pos_mouse_x < game_grid_start_x + block_size_width*6:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width*5
        block_change_y = 5
      elif pos_mouse_x > game_grid_start_x + block_size_width*6 and pos_mouse_x < game_grid_start_x + block_size_width*7:
        button_down = True
        block_position_x = game_grid_start_x + block_size_width*6
        block_change_y = 5
  #print(block_position_y)
  #print(block_size_height)
  #print(block_size_width)
  
  if block_position_y == 525:
    block_change_y = 0
    
    
    
  block_position_y += block_change_y

  game_display.fill(white)
  pygame.draw.rect(game_display, blue, rect = [game_grid_start_x,200,game_grid_x_size,game_grid_y_size])
  
  pygame.draw.rect(game_display, red, rect = [block_position_x,block_position_y,block_size_width,block_size_height])
  
  pygame.draw.rect(game_display,black, rect = [block_position_x,155,block_size_width,block_size_height/2])


  pygame.display.update()

  clock.tick(FPS)
  
      

pygame.quit()
quit()
0 Answers
Related