User:Mirjam Dissel/association machine: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 18: Line 18:
#set colours
#set colours
WHITE = (255,255,255)
WHITE = (255,255,255)
BLACK = (0,0,0)


#set background colour
#set background colour
Line 25: Line 26:
imageList = os.listdir("resized/")
imageList = os.listdir("resized/")
displayList = [[],[],[],[]]
displayList = [[],[],[],[]]
surfaceList = [[],[],[],[]]


hold = [False, False, False, False]
hold = [False, False, False, False]
Line 36: Line 38:
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 48:
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)).convert())
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 60:


#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.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
#execute function
drawImages()
drawImages()


#draws window on the screen
#draws window on the screen
Line 81: Line 99:
if event.type == KEYUP:
if event.type == KEYUP:
if (event.key == K_RETURN):
if (event.key == K_RETURN):
shuffle()
#slide 4px until correct position, take new images and draw them again
drawImages()
for x in range (10):
for s in range(0,320,120):
drawImages(s)
pygame.time.delay(12)
shuffle()
drawImages()
# print "it works"
# print "it works"
if (event.key == K_a):
if (event.key == K_a):
Line 98: Line 121:
pygame.display.update()
pygame.display.update()
clock.tick(30)
clock.tick(30)
</source>
</source>

Latest revision as of 20:59, 9 May 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)
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)