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)

#set background colour
windowSurface.fill(WHITE)

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

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]) < 3):
			randomImage = random.choice(imageList)
			for displayColumn in displayList:
				if randomImage in displayColumn:
					print "already chosen"
					break
					
					
			else: 
				displayList[i].append(randomImage)
				if len(displayList[i]) == 4:
					displayList[i].pop(0)
				chosen += 1

##print displayList
shuffle()


#drawing the images onto the window
def drawImages():
	for x in range(4):
		for y in range(3):
			imgName = displayList[x][y]
			imgObject = pygame.image.load("resized/{0}".format(imgName))
			imgRect = imgObject.get_rect()
			imgRect.left = 20 + (320*x)
			imgRect.top = 20 + (320*(2-y))
			#actually draws the image on the windowsurface
			windowSurface.blit(imgObject, imgRect)


#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):
				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)