Esempio n. 1
0
    def draw(self):
        print ""
        board = self.board
        for line in range(len(board)):
            print board[len(board) - (line + 1)]
        #Draw to panel
        if self.paneldisplay:
            colors = self.COLORS
            if self.BOARDSIZE > 4:
                for x in range(len(board)):
                    for y in range(len(board[0])):
                        sp.set_pixel_color(sp.matrix((x + 1), 14 - (y + 1)),
                                           colors[board[x][y]])
            else:
                for x in range(len(board)):
                    for y in range(len(board[0])):

                        sp.set_pixel_color(
                            sp.matrix(((x + 1) * 2), (15 - (y + 1) * 2)),
                            colors[board[x][y]])
                        sp.set_pixel_color(
                            sp.matrix(((x + 1) * 2), (15 - (y + 1) * 2) - 1),
                            colors[board[x][y]])
                        sp.set_pixel_color(
                            sp.matrix(((x + 1) * 2 - 1), (15 - ((y + 1) * 2))),
                            colors[board[x][y]])
                        sp.set_pixel_color(
                            sp.matrix(((x + 1) * 2) - 1,
                                      (15 - ((y + 1) * 2) - 1)),
                            colors[board[x][y]])
Esempio n. 2
0
 def food_service(self):
     """food controlling"""
     f = self.food
     if f["active"] and not f["visible"] and f["start_time"] < time.time():
         # show food at a free pixel
         # free position finding
         # random position
         r = random.randint(0, self.max_row)
         c = random.randint(0, self.max_col)
         # first free position next to random position
         num_of_cells = (self.max_row + 1) * (self.max_col + 1)
         while [r, c] in [coord[:2] for coord in self.pixels] and \
                 num_of_cells:
             c += 1
             if c > self.max_col:
                 c = 0
                 r += 1
                 if r > self.max_row:
                     r = 0
             num_of_cells -= 1
         if num_of_cells:
             # free position found
             # show food at [r, c]
             sp.set_pixel_color(sp.matrix(r, c), f["color"])
             f["position"] = [r, c]
             f["visible"] = True
     elif f["active"] and f["visible"] and (f["start_time"] + \
             f["duration"]) > time.time():
         # a food is in progress
         # test if snake reached the food
         if self.food["position"] in [coord[:2] for coord in \
                 self.pixels]:
             self.add_pixel()
             f["active"] = False
             f["visible"] = False
         return
     elif f["active"] and not f["visible"] and f["start_time"] > time.time(
     ):
         # wait to show
         return
     else:
         # old food hiding
         if f["visible"]:
             pos = f["position"]
             sp.set_pixel_color(sp.matrix(pos[0], pos[1]), self.blank_color)
             f["visible"] = False
         # new food generation
         t1 = f["df_interval"][0] * 1000
         t2 = f["df_interval"][1] * 1000
         d1 = f["df_duration"][0] * 1000
         d2 = f["df_duration"][1] * 1000
         f["start_time"] = time.time() + random.randint(t1, t2) / 1000.
         f["duration"] = random.randint(d1, d2) / 1000.
         f["active"] = True
         f["visible"] = False
Esempio n. 3
0
 def move(self):
     """snake moving"""
     if len(self.pixels) > 0:
         last_pixel = self.pixels[-1][:]
         for index in range(len(self.pixels) - 1, 0, -1):
             self.pixels[index][0] = self.pixels[index - 1][0]
             self.pixels[index][1] = self.pixels[index - 1][1]
         self.pixels[0][0] = self.position[0]
         self.pixels[0][1] = self.position[1]
         # last pixel hiding
         sp.set_pixel_color(sp.matrix(last_pixel[0], last_pixel[1]), \
                 self.blank_color)
         # snake show (the whole snake or first two pixels
         # it depends on number of colors in the snake
         for pixel in self.pixels[:2]:
             sp.set_pixel_color(sp.matrix(pixel[0], pixel[1]), pixel[2])
Esempio n. 4
0
    def init(self, wi=None, panel=True):
        global board
        global oldboard
	global panel
        self.board = [[0 for _ in range(self.BOARDSIZE)] for _ in range(self.BOARDSIZE)]
        self.oldboard = copy.deepcopy(self.board)
	self.panel =  [[0 for _ in range(15)] for _ in range(9)]

        self.addtile()
       

        if panel:
            self.paneldisplay = True
        else:
            self.paneldisplay = False

        if self.paneldisplay:
            sp.panel_clear()
        
        self.draw()
        self.playing=True
        if self.BOARDSIZE > 4 and self.paneldisplay:
        	sp.rectangle(0, 15-(self.BOARDSIZE+2), self.BOARDSIZE+1, 14, "ffffff")

	else:
		sp.rectangle(0,15-((self.BOARDSIZE+2)*2-2), ((self.BOARDSIZE+1)*2)-1, 14,  "656565")

        if self.paneldisplay:
            for i in range(len(self.COLORS)-2):
                sp.set_pixel_color(sp.matrix(i,0), self.COLORS[i+1])
		#sp.set_pixel_color(sp.matrix(i,1), self.COLORS2[i+1])

		time.sleep(0.1)
		print i
        self.play(wi)
Esempio n. 5
0
 def play(self):
     while True:
         sp.panel_clear()
         x, y = infrapen.get_cords()
         infrapen.waituntilrelease()
         sp.set_pixel_color(sp.matrix(x, y), "906090")
         print x, y
    def draw(self):
        print ""
        board = self.board
        for line in range(len(board)):
            print board[len(board)-(line+1)]
        #Draw to panel
        if self.paneldisplay:
            colors = self.COLORS
            if self.BOARDSIZE > 4:
                for x in range(len(board)):
                    for y in range(len(board[0])):
                        sp.set_pixel_color(sp.matrix((x+1),14-(y+1)), colors[board[x][y]])
            else:
                for x in range(len(board)):
                    for y in range(len(board[0])):

                        sp.set_pixel_color(sp.matrix(((x+1)*2),(15-(y+1)*2)), colors[board[x][y]])
                        sp.set_pixel_color(sp.matrix(((x+1)*2),(15-(y+1)*2)-1), colors[board[x][y]])
                        sp.set_pixel_color(sp.matrix(((x+1)*2-1),(15-((y+1)*2))), colors[board[x][y]])
                        sp.set_pixel_color(sp.matrix(((x+1)*2)-1,(15-((y+1)*2)-1)), colors[board[x][y]])
Esempio n. 7
0
    def conf(self, wimote):
	global wi
 	wi  = wimote
	sp.panel_clear()
	sp.set_pixel_color(sp.matrix(0,0),"ffffff")	
	print "klikni vlevo dole"
	ld = self.getcord(wi)
	sp.panel_clear()
	print ld
	self.waitfornopen()
	time.sleep(1)

        
        sp.set_pixel_color(sp.matrix(0,14),"ffffff")
	print "klikni vpravo dole"
	pd = self.getcord(wi)
	sp.panel_clear()
	print pd
	self.waitfornopen()
	time.sleep(1)
        sp.set_pixel_color(sp.matrix(8,0),"ffffff")

	print "klikni vlevo nahore"
	lh = self.getcord(wi)
	sp.panel_clear()
	print lh
	self.waitfornopen()
	time.sleep(1)	

        sp.panel_clear()
        sp.set_pixel_color(sp.matrix(8,14),"ffffff")
	print "klikni pravo nahore"
	ph = self.getcord(wi)
	sp.panel_clear()
	print ph
	self.waitfornopen()
	
	self.pohl.setsrc(lh, ph, ld, pd)
	print "konfigurace dokoncena"
Esempio n. 8
0
 def wii_move(self, wi=None):
     """the snake controlling through wiimote
     wi is wiimote object returned from winit()
     """
     if wi is None:
         """ wiimote initializing """
         sp.wi = winit()
     old_position = [-1, -1]
     play = True
     while play:
         move_flag = False
         if len(self.pixels) > 0:
             self.position = [self.pixels[0][0], self.pixels[0][1]][:]
         if self.position != old_position:
             old_position = self.position[:]
             time.sleep(0.1)
         buttons = wi.state["buttons"]
         if buttons & 4:
             # trigger
             fire(self.position)
             old_position = [-1000, -1000]
             if len(self.pixels) > 0:
                 fp = self.pixels[0]
                 set_pixel_color(matrix(fp[0], fp[1]), fp[2])
         if buttons & 256:
             # left
             self.position[1] -= 1
             move_flag = True
         if buttons & 512:
             # right
             self.position[1] += 1
             move_flag = True
         if buttons & 2048:
             # up
             self.position[0] += 1
             move_flag = True
         if buttons & 1024:
             # down
             self.position[0] -= 1
             move_flag = True
         if buttons & 8:
             # A button
             # go to left bottom
             """
             the snake head goes to the [0, 0] position
             the rest of the snake leaves at the current position
             """
             # the old snake head hiding
             set_pixel_color(matrix(self.pixels[0][0], self.pixels[0][1]), \
                     self.blank_color)
             self.position[0] = 0
             self.position[1] = 0
             self.pixels[0][0] = self.pixels[0][1] = 0
             # show the new head (see moving)
             for pixel in self.pixels[:2]:
                 sp.set_pixel_color(sp.matrix(pixel[0], pixel[1]), pixel[2])
         if buttons & 128:
             # Home button
             # end of controlling loop
             play = False
         if buttons & cwiid.BTN_PLUS:
             """add pixel to snake"""
             self.add_pixel()
             old_position = [-1000, -1000]
             fp = self.pixels[0]
             sp.set_pixel_color(sp.matrix(fp[0], fp[1]), fp[2])
             # wait for button release
             while wi.state["buttons"] & cwiid.BTN_PLUS:
                 pass
         if buttons & cwiid.BTN_MINUS:
             """delete pixel at the end of the snake"""
             self.del_pixel()
             old_position = [-1000, -1000]
             # wait for button release
             while wi.state["buttons"] & cwiid.BTN_MINUS:
                 pass
         if move_flag:
             self.move()
         """testing if the snake doesn't have the head on his own body"""
         if len(self.pixels) > 1 and [self.pixels[0][0], self.pixels[0][1]] \
                 in [coord[:2] for coord in self.pixels[1:]]:
             # colision - end of game
             for blink in range(3):
                 sp.set_panel_color("ff0000")
                 time.sleep(0.1)
                 sp.set_panel_color("")
                 time.sleep(0.1)
             self.pixels = []
             self.position = [0, 0]
             self.add_pixel()
             fire(self.position)
             fp = self.pixels[0]
             set_pixel_color(matrix(fp[0], fp[1]), fp[2])
         # food controlling
         self.food_service()