User:Mirjam Dissel/association machine: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 25: Line 25:
imageList = os.listdir("resized/")
imageList = os.listdir("resized/")
displayList = [[],[],[],[]]
displayList = [[],[],[],[]]
surfaceList = [[],[],[],[]]


hold = [False, False, False, False]
hold = [False, False, False, False]
Line 36: Line 37:
continue
continue
chosen = 0
chosen = 0
while (chosen == 0) or (len(displayList[i]) < 3):
while (chosen == 0) or (len(displayList[i]) < 4):
randomImage = random.choice(imageList)
randomImage = random.choice(imageList)
for displayColumn in displayList:
for displayColumn in displayList:
Line 46: Line 47:
else:  
else:  
displayList[i].append(randomImage)
displayList[i].append(randomImage)
if len(displayList[i]) == 4:
surfaceList[i].append(pygame.image.load("resized/{0}".format(randomImage)))
if len(displayList[i]) == 5:
displayList[i].pop(0)
displayList[i].pop(0)
if len(surfaceList[i]) == 5:
surfaceList[i].pop(0)
chosen += 1
chosen += 1


Line 55: Line 59:


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




Line 81: Line 91:
if event.type == KEYUP:
if event.type == KEYUP:
if (event.key == K_RETURN):
if (event.key == K_RETURN):
#slide 4px until correct position, take new images and draw them again
for s in range(0,320,4):
drawImages(s)
# pygame.time.delay(4)
shuffle()
shuffle()
drawImages()
drawImages()
Line 97: Line 111:
sys.exit()
sys.exit()
pygame.display.update()
pygame.display.update()
clock.tick(30)
clock.tick(3)


</source>
</source>

Revision as of 16:49, 23 April 2012

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 = [[],[],[],[]]
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)))
				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.display.update()




#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 s in range(0,320,4):
					drawImages(s)
#					pygame.time.delay(4)
				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(3)