Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Imports
- import pygame
- import time
- import random
- pygame.init()
- #Important Variables
- display_width = 900
- display_height = 550
- display_side = 550
- gameExit = False
- gameOver = False
- gap = display_side/15
- side = display_side/2 - 3*gap/2
- s_x = (display_width - display_side)/2
- s_y = (display_height - display_side)/2
- XnY = [[s_x + gap, s_y + gap], [s_x + gap + side + gap, s_y + gap], [s_x + gap, s_y + gap + side + gap], [s_x + gap + side + gap, s_y + gap + side + gap]]
- check_one = 0
- check_two = 0
- check_three = 0
- check_four = 0
- check_five = 0
- round_no = 1
- random_array = []
- user_array = []
- randDone = False
- choice = 0
- button_width = 110
- button_height = 50
- dialog_side = display_side*9/10
- smallfont = pygame.font.SysFont('comicsansms', 25)
- medfont = pygame.font.SysFont('comicsansms', 45)
- larfont = pygame.font.SysFont('comicsansms', 80)
- #Colours
- white = (255,255,255)
- black = (0,0,0)
- red = (150,20,20)
- br_red = (255,0,0)
- mid_red = (165, 35, 35)
- green = (78,164,40)
- br_green = (0,255,0)
- mid_green = (65, 190, 18)
- blue = (46,46,158)
- br_blue = (0,100,255)
- mid_blue = (20, 20, 182)
- gray = (200,200,200)
- dr_gray = (100,100,100)
- yellow = (150, 150, 43)
- br_yellow = (255,255,0)
- mid_yellow = (165, 165, 54)
- orange = (255, 168, 64)
- brown = (128, 64, 0)
- gameDisplay = pygame.display.set_mode((display_width,display_height))
- pygame.display.set_caption('Simon')
- clock = pygame.time.Clock()
- FPS = 15
- #Classes
- class Square:
- def __init__(self, side, colour, mid_colour, br_colour, x, y):
- self.side = side
- self.colour = colour
- self.midcolour = mid_colour
- self.brcolour = br_colour
- self.x = x
- self.y = y
- def draw(self):
- pygame.draw.rect(gameDisplay, self.colour, [self.x, self.y, self.side, self.side])
- def update(self):
- pygame.draw.rect(gameDisplay, self.brcolour, [self.x, self.y, self.side, self.side])
- def mid_update(self):
- pygame.draw.rect(gameDisplay, self.midcolour, [self.x, self.y, self.side, self.side])
- #Object Declarations
- #Class Square
- first = Square(side, red, mid_red, br_red, XnY[0][0], XnY[0][1])
- second = Square(side, blue, mid_blue, br_blue, XnY[1][0], XnY[1][1])
- third = Square(side, green, mid_green, br_green, XnY[2][0], XnY[2][1])
- fourth = Square(side, yellow, mid_yellow, br_yellow, XnY[3][0], XnY[3][1])
- #Functions
- def mouseGlow():
- #Global Declarations
- global check_one
- global check_two
- cur = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- check_one = 0
- while check_one < 4:
- if XnY[check_one][0] <= cur[0] <= XnY[check_one][0] + side and XnY[check_one][1] <= cur[1] <= XnY[check_one][1] + side:
- cur = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- if check_one == 0:
- first.mid_update()
- if click[0] == 1:
- first.update()
- check_two = True
- while click[0] == 0 and check_two == True:
- user_array.append(check_one + 1)
- check_two = False
- first.update()
- pygame.display.update()
- time.sleep(1)
- if check_one == 1:
- second.mid_update()
- if click[0] == 1:
- second.update()
- check_two = True
- while click[0] == 0 and check_two == True:
- user_array.append(check_one + 1)
- check_two = False
- second.update()
- pygame.display.update()
- time.sleep(1)
- if check_one == 2:
- third.mid_update()
- if click[0] == 1:
- third.update()
- check_two = True
- while click[0] == 0 and check_two == True:
- user_array.append(check_one + 1)
- check_two = False
- third.update()
- pygame.display.update()
- time.sleep(1)
- if check_one == 3:
- fourth.mid_update()
- if click[0] == 1:
- fourth.update()
- check_two = True
- while click[0] == 0 and check_two == True:
- user_array.append(check_one + 1)
- check_two = False
- fourth.update()
- pygame.display.update()
- time.sleep(1)
- check_one += 1
- def random_input():
- #Global Declaration
- global choice
- global random_array
- global randDone
- global round_no
- global user_array
- while not randDone:
- while len(random_array) < round_no:
- choice = round(random.randrange(1, 5))
- random_array.append(choice)
- if choice == 1:
- first.update()
- pygame.display.update()
- time.sleep(1.5)
- if choice == 2:
- second.update()
- pygame.display.update()
- time.sleep(1.5)
- if choice == 3:
- third.update()
- pygame.display.update()
- time.sleep(1.5)
- if choice == 4:
- fourth.update()
- pygame.display.update()
- time.sleep(1.5)
- first.draw()
- second.draw()
- third.draw()
- fourth.draw()
- pygame.display.update()
- time.sleep(0.5)
- randDone = True
- user_array = []
- def gameOver_func():
- #Global Declarations
- global gameOver
- global dialog_side
- first.draw()
- second.draw()
- third.draw()
- fourth.draw()
- pygame.display.update()
- while gameOver:
- pygame.display.update()
- pygame.draw.rect(gameDisplay, brown, [(s_x + display_side/2 - dialog_side/2), (s_y + display_side/2 - dialog_side/2), dialog_side, dialog_side])
- message_to_screen("GAME OVER", white, (s_x + display_side/2), (s_y + display_side/2 - 2*dialog_side/5 + 65), 'large')
- Button('Quit', (s_x + display_side/2 + 85), (s_y + display_side/2 + 165), yellow, orange, black, 'quit')
- Button('Play Again', (s_x + display_side/2 - 170), (s_y + display_side/2 + 165), yellow, orange, black, 'play', 40)
- #The events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- def start():
- start = True
- while start:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- gameDisplay.fill(mid_yellow)
- pygame.draw.rect(gameDisplay, mid_red, [0, 0, display_width/2, display_height/2])
- pygame.draw.rect(gameDisplay, mid_blue, [display_width/2, 0, display_width/2, display_height/2])
- pygame.draw.rect(gameDisplay, mid_green, [0, display_height/2, display_width/2, display_height/2])
- message_to_screen('SIMON', white, (display_width / 2), (display_height / 2), 'large')
- cur = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- Button("Play", display_width/2 - 3*button_width/2 - 150, 450, brown, orange, black, action = 'play')
- Button("Quit", display_width/2 + button_width/2 + 150, 450, brown, orange, black, action = 'quit')
- pygame.display.update()
- clock.tick(FPS)
- def text_objects(text, colour,size):
- if size == 'small':
- textSurface=smallfont.render(text,True, colour)
- if size == 'medium':
- textSurface=medfont.render(text,True, colour)
- if size == 'large':
- textSurface=larfont.render(text,True, colour)
- return textSurface, textSurface.get_rect()
- def message_to_screen(msg,colour,x_displace = 0, y_displace = 0, size ='small'):
- textSurf, textRect = text_objects(msg,colour, size)
- textRect.center = x_displace, y_displace
- gameDisplay.blit(textSurf,textRect)
- def text_to_button(msg, colour, topX, topY, size = 'small'):
- textSurf, textRect = text_objects(msg, colour, size)
- textRect.center = (topX + button_width/2), (topY + button_height/2)
- gameDisplay.blit(textSurf, textRect)
- def Button(msg, x, y, act_colour, inact_colour, textcolour, action, x_increase = 0):
- cur = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- if x + button_width >= cur[0] >= x and y + button_height >= cur[1] >= y:
- pygame.draw.rect(gameDisplay, act_colour, (x - x_increase/2, y, button_width + x_increase, button_height))
- if click[0] == 1:
- if action == 'rand_input':
- first.draw()
- second.draw()
- third.draw()
- fourth.draw()
- text_to_button(msg, textcolour, x, y)
- pygame.display.update()
- random_input()
- elif action == 'quit':
- pygame.quit()
- quit()
- elif action == 'controls':
- pass
- elif action == 'back':
- start()
- elif action == 'play':
- gameLoop()
- else:
- pygame.draw.rect(gameDisplay, inact_colour, (x - x_increase/2, y, button_width + x_increase, button_height))
- text_to_button(msg, textcolour, x, y)
- #The Game Loop
- def gameLoop():
- #Global Declarations
- global gameExit
- global gameOver
- global randDone
- global random_array
- global user_array
- global round_no
- global check_one
- global check_two
- global check_three
- global check_four
- global check_five
- global choice
- gameExit = False
- gameOver = False
- check_one = 0
- check_two = 0
- check_three = 0
- check_four = 0
- check_five = 0
- round_no = 1
- random_array = []
- user_array = []
- randDone = False
- choice = 0
- gameDisplay.fill(gray)
- while not gameExit:
- gameDisplay.fill(gray)
- pygame.draw.rect(gameDisplay, dr_gray, [s_x, s_y, display_side, display_side])
- message_to_screen("Round " + str(round_no), black, 9*display_width/10 + 5, display_height/2, 'medium')
- if not randDone:
- Button("Start", 30, 50, yellow, orange, black, 'rand_input')
- Button('Back', 30, display_height/2, yellow, orange, black, 'back')
- Button("Quit", 30, display_height - 40 - button_height, yellow, orange, black, 'quit')
- #The events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- #The main working body
- first.draw()
- second.draw()
- third.draw()
- fourth.draw()
- mouseGlow()
- if user_array == random_array and random_array != []:
- user_array = []
- random_array = []
- round_no += 1
- randDone = False
- if random_array != []:
- while check_three < len(user_array):
- if user_array[check_three] != random_array[check_three] and user_array[check_three] != 0:
- gameOver = True
- gameOver_func()
- check_three += 1
- check_three = 0
- pygame.display.update()
- clock.tick(FPS)
- start()
- pygame.quit()
- quit()
Add Comment
Please, Sign In to add comment