I've made a simple connect 4 game using PyGame. I'm new to python and haven't completely perfected this. I know it's inefficient but I can't tell exactly HOW to fix it? Could someone help me out? Would be greatly appreciated.
import pygame
from colors import *
import random
pygame.init()
size = (320, 280)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Connect 4 but Worse")
done = False
clock = pygame.time.Clock()
turn = 0
class Space():
def __init__(self):
self.color = WHITE
self.height = 40
self.width = 40
self.x = 5
self.y = 5
def Draw(self, screen):
pygame.draw.ellipse(screen, self.color, [self.x, self.y, 40, 40])
x_list = [5, 50, 95, 140, 185, 230, 275]
y_list = [5, 50, 95, 140, 185, 230]
spaces_list = []
x_counter = 0
y_counter = 0
for i in range(42):
slot = Space()
slot.color = WHITE
slot.height = 40
slot.width = 40
slot.x = x_list[x_counter]
slot.y = y_list[y_counter]
spaces_list.append(slot)
x_counter += 1
if x_counter == 7:
y_counter +=1
x_counter = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
(posx,posy) = pygame.mouse.get_pos()
if posx > 5 and posx < 45:
if spaces_list[35].color == WHITE:
if turn == 0:
spaces_list[35].color = RED
turn = 1
elif turn == 1:
spaces_list[35].color = YELLOW
turn = 0
elif spaces_list[28].color == WHITE:
if turn == 0:
spaces_list[28].color = RED
turn = 1
elif turn == 1:
spaces_list[28].color = YELLOW
turn = 0
elif spaces_list[21].color == WHITE:
if turn == 0:
spaces_list[21].color = RED
turn = 1
elif turn == 1:
spaces_list[21].color = YELLOW
turn = 0
elif spaces_list[14].color == WHITE:
if turn == 0:
spaces_list[14].color = RED
turn = 1
elif turn == 1:
spaces_list[14].color = YELLOW
turn = 0
elif spaces_list[7].color == WHITE:
if turn == 0:
spaces_list[7].color = RED
turn = 1
elif turn == 1:
spaces_list[7].color = YELLOW
turn = 0
elif spaces_list[0].color == WHITE:
if turn == 0:
spaces_list[0].color = RED
turn = 1
elif turn == 1:
spaces_list[0].color = YELLOW
turn = 0
if posx > 50 and posx < 90:
if spaces_list[36].color == WHITE:
if turn == 0:
spaces_list[36].color = RED
turn = 1
elif turn == 1:
spaces_list[36].color = YELLOW
turn = 0
elif spaces_list[29].color == WHITE:
if turn == 0:
spaces_list[29].color = RED
turn = 1
elif turn == 1:
spaces_list[29].color = YELLOW
turn = 0
elif spaces_list[22].color == WHITE:
if turn == 0:
spaces_list[22].color = RED
turn = 1
elif turn == 1:
spaces_list[22].color = YELLOW
turn = 0
elif spaces_list[15].color == WHITE:
if turn == 0:
spaces_list[15].color = RED
turn = 1
elif turn == 1:
spaces_list[15].color = YELLOW
turn = 0
elif spaces_list[8].color == WHITE:
if turn == 0:
spaces_list[8].color = RED
turn = 1
elif turn == 1:
spaces_list[8].color = YELLOW
turn = 0
elif spaces_list[1].color == WHITE:
if turn == 0:
spaces_list[1].color = RED
turn = 1
elif turn == 1:
spaces_list[1].color = YELLOW
turn = 0
if posx > 95 and posx < 135:
if spaces_list[37].color == WHITE:
if turn == 0:
spaces_list[37].color = RED
turn = 1
elif turn == 1:
spaces_list[37].color = YELLOW
turn = 0
elif spaces_list[30].color == WHITE:
if turn == 0:
spaces_list[30].color = RED
turn = 1
elif turn == 1:
spaces_list[30].color = YELLOW
turn = 0
elif spaces_list[23].color == WHITE:
if turn == 0:
spaces_list[23].color = RED
turn = 1
elif turn == 1:
spaces_list[23].color = YELLOW
turn = 0
elif spaces_list[16].color == WHITE:
if turn == 0:
spaces_list[16].color = RED
turn = 1
elif turn == 1:
spaces_list[16].color = YELLOW
turn = 0
elif spaces_list[9].color == WHITE:
if turn == 0:
spaces_list[9].color = RED
turn = 1
elif turn == 1:
spaces_list[9].color = YELLOW
turn = 0
elif spaces_list[2].color == WHITE:
if turn == 0:
spaces_list[2].color = RED
turn = 1
elif turn == 1:
spaces_list[2].color = YELLOW
turn = 0
if posx > 140 and posx < 180:
if spaces_list[38].color == WHITE:
if turn == 0:
spaces_list[38].color = RED
turn = 1
elif turn == 1:
spaces_list[38].color = YELLOW
turn = 0
elif spaces_list[31].color == WHITE:
if turn == 0:
spaces_list[31].color = RED
turn = 1
elif turn == 1:
spaces_list[31].color = YELLOW
turn = 0
elif spaces_list[24].color == WHITE:
if turn == 0:
spaces_list[24].color = RED
turn = 1
elif turn == 1:
spaces_list[24].color = YELLOW
turn = 0
elif spaces_list[17].color == WHITE:
if turn == 0:
spaces_list[17].color = RED
turn = 1
elif turn == 1:
spaces_list[17].color = YELLOW
turn = 0
elif spaces_list[10].color == WHITE:
if turn == 0:
spaces_list[10].color = RED
turn = 1
elif turn == 1:
spaces_list[10].color = YELLOW
turn = 0
elif spaces_list[3].color == WHITE:
if turn == 0:
spaces_list[3].color = RED
turn = 1
elif turn == 1:
spaces_list[3].color = YELLOW
turn = 0
if posx > 185 and posx < 225:
if spaces_list[39].color == WHITE:
if turn == 0:
spaces_list[39].color = RED
turn = 1
elif turn == 1:
spaces_list[39].color = YELLOW
turn = 0
elif spaces_list[32].color == WHITE:
if turn == 0:
spaces_list[32].color = RED
turn = 1
elif turn == 1:
spaces_list[32].color = YELLOW
turn = 0
elif spaces_list[25].color == WHITE:
if turn == 0:
spaces_list[25].color = RED
turn = 1
elif turn == 1:
spaces_list[25].color = YELLOW
turn = 0
elif spaces_list[18].color == WHITE:
if turn == 0:
spaces_list[18].color = RED
turn = 1
elif turn == 1:
spaces_list[18].color = YELLOW
turn = 0
elif spaces_list[11].color == WHITE:
if turn == 0:
spaces_list[11].color = RED
turn = 1
elif turn == 1:
spaces_list[11].color = YELLOW
turn = 0
elif spaces_list[4].color == WHITE:
if turn == 0:
spaces_list[4].color = RED
turn = 1
elif turn == 1:
spaces_list[4].color = YELLOW
turn = 0
if posx > 230 and posx < 270:
if spaces_list[40].color == WHITE:
if turn == 0:
spaces_list[40].color = RED
turn = 1
elif turn == 1:
spaces_list[40].color = YELLOW
turn = 0
elif spaces_list[33].color == WHITE:
if turn == 0:
spaces_list[33].color = RED
turn = 1
elif turn == 1:
spaces_list[33].color = YELLOW
turn = 0
elif spaces_list[26].color == WHITE:
if turn == 0:
spaces_list[26].color = RED
turn = 1
elif turn == 1:
spaces_list[26].color = YELLOW
turn = 0
elif spaces_list[19].color == WHITE:
if turn == 0:
spaces_list[19].color = RED
turn = 1
elif turn == 1:
spaces_list[19].color = YELLOW
turn = 0
elif spaces_list[12].color == WHITE:
if turn == 0:
spaces_list[12].color = RED
turn = 1
elif turn == 1:
spaces_list[12].color = YELLOW
turn = 0
elif spaces_list[5].color == WHITE:
if turn == 0:
spaces_list[5].color = RED
turn = 1
elif turn == 1:
spaces_list[5].color = YELLOW
turn = 0
if posx > 275:
if spaces_list[41].color == WHITE:
if turn == 0:
spaces_list[41].color = RED
turn = 1
elif turn == 1:
spaces_list[41].color = YELLOW
turn = 0
elif spaces_list[34].color == WHITE:
if turn == 0:
spaces_list[34].color = RED
turn = 1
elif turn == 1:
spaces_list[34].color = YELLOW
turn = 0
elif spaces_list[27].color == WHITE:
if turn == 0:
spaces_list[27].color = RED
turn = 1
elif turn == 1:
spaces_list[27].color = YELLOW
turn = 0
elif spaces_list[20].color == WHITE:
if turn == 0:
spaces_list[20].color = RED
turn = 1
elif turn == 1:
spaces_list[20].color = YELLOW
turn = 0
elif spaces_list[13].color == WHITE:
if turn == 0:
spaces_list[13].color = RED
turn = 1
elif turn == 1:
spaces_list[13].color = YELLOW
turn = 0
elif spaces_list[6].color == WHITE:
if turn == 0:
spaces_list[6].color = RED
turn = 1
elif turn == 1:
spaces_list[6].color = YELLOW
turn = 0
screen.fill(BLUE)
counter = 0
for items in spaces_list:
spaces_list[counter].Draw(screen)
counter += 1
pygame.display.flip()
clock.tick(60)
pygame.quit()
if then elif
cascade. Try to explain how this works, either to a human, or to a rubber duck. You could also have a look at other solutions to the same task. \$\endgroup\$ – Roland Illig Dec 18 '19 at 20:31