I want to add sounds and a game start / game over screen but the actual mechanics are down,for the most part
Code:
import random, sys, time, pygame
from pygame.locals import *
fps = 30
height = 500
width = 500
buttonsize = 200
buttongap = 20
xmargin = int((width - (2*buttonsize) - buttongap) /2)
ymargin = int((height - (2*buttonsize) - buttongap) /2)
flashDelay = 200
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
BRIGHTRED = (255, 0, 0)
RED = (155, 0, 0)
BRIGHTGREEN = ( 0, 255, 0)
GREEN = ( 0, 155, 0)
BRIGHTBLUE = ( 0, 0, 255)
BLUE = ( 0, 0, 155)
BRIGHTYELLOW = (255, 255, 0)
YELLOW = (155, 155, 0)
DARKGRAY = ( 40, 40, 40)
bgColor = BLACK
# Rect objects for each of the four buttons
yellowrect = pygame.Rect(xmargin, ymargin, buttonsize, buttonsize)
bluerect = pygame.Rect(xmargin + buttonsize + buttongap, ymargin, buttonsize, buttonsize)
redrect = pygame.Rect(xmargin, ymargin + buttonsize + buttongap, buttonsize, buttonsize)
greenrect = pygame.Rect(xmargin + buttonsize + buttongap, ymargin + buttonsize +buttongap, buttonsize, buttonsize)
def main():
global clock, gameDisplay, basicfont
clock = pygame.time.Clock()
gameDisplay = pygame.display.set_mode((height, width))
pygame.display.set_caption('Simon Says')
crashed = False
pygame.init()
step = 0
waitingForInput = False
orderList = []
#main loop
while not crashed:
clicked = None
drawButtons()
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('God Fucking Dammit Leeroy')
crashed = True
print(event)
#for the plebs who use mice
if event.type == pygame.MOUSEBUTTONUP:
mousex, mousey = event.pos
clicked = getButtonClicked(mousex, mousey)
print('this works?')
#for them classy folks who like the keyboard
elif event.type == KEYDOWN:
if event.key == K_z:
clicked = YELLOW
print('yellow')
elif event.key == K_x:
clicked = BLUE
print('blue')
elif event.key == K_c:
clicked = RED
print('red')
elif event.key == K_v:
clicked = GREEN
print('green')
pygame.display.update()
clock.tick(fps)
if not waitingForInput:
orderList.append(random.choice((YELLOW,BLUE,RED,GREEN)))
for item in orderList:
clickedAnimation(item, 50)
pygame.time.wait(flashDelay)
waitingForInput = True
else:
print('got after else')
print("order length is: %d" % len(orderList))
#print('order 0 is: %s' % orderList[0])
if clicked and orderList[step] == clicked:
step += 1
waitingForInput = True
if step == len(orderList):
waitingForInput = False
step = 0
elif clicked and orderList[step] != step:
crashed = True
else:
waitingForInput = True
def getButtonClicked(x, y):
if yellowrect.collidepoint((x,y)):
print('yellow')
return YELLOW
elif bluerect.collidepoint((x,y)):
print('blue')
return BLUE
elif redrect.collidepoint((x,y)):
print('red')
return RED
elif greenrect.collidepoint((x,y)):
print('green')
return GREEN
return None
def drawButtons():
pygame.draw.rect(gameDisplay, YELLOW, yellowrect)
pygame.draw.rect(gameDisplay, RED, redrect)
pygame.draw.rect(gameDisplay, BLUE, bluerect)
pygame.draw.rect(gameDisplay, GREEN, greenrect)
def clickedAnimation(color, speed=50):
if color == YELLOW:
flash = BRIGHTYELLOW
rectangle = yellowrect
elif color == BLUE:
flash = BRIGHTBLUE
rectangle = bluerect
elif color == RED:
flash = BRIGHTRED
rectangle = redrect
elif color == GREEN:
flash = BRIGHTGREEN
rectangle = greenrect
originalSurface = gameDisplay.copy()
flashSurface = pygame.Surface((buttonsize, buttonsize))
flashSurface = flashSurface.convert_alpha()
r, g, b = flash
for start, end, step in ((0, 255, 1), (255, 0, -1)): # animation loop
for alpha in range(start, end, speed * step):
gameDisplay.blit(originalSurface, (0, 0))
flashSurface.fill((r, g, b, alpha))
gameDisplay.blit(flashSurface, rectangle.topleft)
pygame.display.update()
clock.tick(fps)
gameDisplay.blit(originalSurface, (0, 0))
if __name__=='__main__':
main()