User:Mirjam Dissel/association machine

From XPUB & Lens-Based wiki

Associationmachine.png

helloimage.py

import pygame, sys, os, random
from pygame.locals import *

#setup pygame
pygame.init()

#setup window (what do the 0 and 32 do?)
windowSurface = pygame.display.set_mode((1300, 1000), 0, 32)
pygame.display.set_caption("SLOT MACHINE")

#initiate clock
clock  = pygame.time.Clock()

#set colours
WHITE = (255,255,255)
BLACK = (0,0,0)

#set background colour
windowSurface.fill(WHITE)

#make a list of images of resized directory
imageList = os.listdir("resized/")
displayList = [[],[],[],[]]
surfaceList = [[],[],[],[]]

hold = [False, False, False, False]

def shuffle():
	global displayList
#	print "it's shuffling"
	for i in range (4):
#		displayList = []
		if hold[i]:
			continue
		chosen = 0
		while (chosen == 0) or (len(displayList[i]) < 4):
			randomImage = random.choice(imageList)
			for displayColumn in displayList:
				if randomImage in displayColumn:
					print "already chosen"
					break
					
					
			else: 
				displayList[i].append(randomImage)
				surfaceList[i].append(pygame.image.load("resized/{0}".format(randomImage)).convert())
				if len(displayList[i]) == 5:
					displayList[i].pop(0)
				if len(surfaceList[i]) == 5:
					surfaceList[i].pop(0)
				chosen += 1

##print displayList
shuffle()


#drawing the images onto the window
def drawImages(slide=0):
	windowSurface.fill(WHITE)
	for x in range(4):
		for y in range(4):
			imgObject = surfaceList[x][y]
			imgRect = imgObject.get_rect()
			imgRect.left = 20 + (320*x)
			if hold[x]:
				imgRect.top = 20 + (320*(2-y))
			else:
				imgRect.top = 20 + (320*(2-y))+slide
			#actually draws the image on the windowsurface
			windowSurface.blit(imgObject, imgRect)
			pygame.draw.line(windowSurface, (255, 0, 0), (0, 500), (1300, 500))
	pygame.display.update()


#line = pygame.Rect((10, 20), (400, 30))
#line.fill(BLACK)




#execute function
drawImages()



#draws window on the screen
pygame.display.update()


# van de imgrect en imgobject kan ik een list van 4 lists maken. en dan een functie slideImage, bij imgrect tel ik bij de top telkens 1px op, dan schuiven de plaatjes naar beneden. (niet zeker of windowSurface ook geupdate moet worden.) pygame.display.update() wel. totdat ze 318 px zijn verschoven roep je drawImages weer aan. maak de list groter 4x4 plaatjes waarvan er maar 4x3 getekend worden.

# gameloop pygame
while True:
	for event in pygame.event.get():
		if event.type == KEYUP:
			if (event.key == K_RETURN):
				#slide 4px until correct position, take new images and draw them again
				for x in range (10):
					for s in range(0,320,120):
						drawImages(s)
						pygame.time.delay(12)
					shuffle()
					drawImages()
#				print "it works"
			if (event.key == K_a):
				hold[0] = not hold[0]
			if (event.key == K_s):
				hold[1] = not hold[1]
			if (event.key == K_d):
				hold[2] = not hold[2]
			if (event.key == K_f):
				hold[3] = not hold[3]
		if event.type == QUIT:
			keepGoing = False
			pygame.quit()
			sys.exit()
	pygame.display.update()
	clock.tick(30)