From 00f6e2c1849b5cb7c3ae97473be25ab57e4d2f5b Mon Sep 17 00:00:00 2001 From: Wassek AL CHAHID Date: Thu, 2 Mar 2017 21:22:28 +0100 Subject: [PATCH] Some python games we made for a school project (DAWIN Bachelor Degree's), using the Arbalet Led Table SDK --- arbalet/apps/bomberman/__init__.py | 0 arbalet/apps/bomberman/__main__.py | 2 + arbalet/apps/bomberman/bomberman.py | 368 ++++++++++++++++++++++++++ arbalet/apps/cfever/__init__.py | 0 arbalet/apps/cfever/__main__.py | 2 + arbalet/apps/cfever/cfever.py | 186 +++++++++++++ arbalet/apps/menu/__init__.py | 0 arbalet/apps/menu/__main__.py | 2 + arbalet/apps/menu/menu.py | 329 +++++++++++++++++++++++ arbalet/apps/pong/__init__.py | 0 arbalet/apps/pong/__main__.py | 2 + arbalet/apps/pong/pong.py | 330 +++++++++++++++++++++++ arbalet/apps/puissance4/__init__.py | 0 arbalet/apps/puissance4/__main__.py | 2 + arbalet/apps/puissance4/puissance4.py | 249 +++++++++++++++++ 15 files changed, 1472 insertions(+) create mode 100644 arbalet/apps/bomberman/__init__.py create mode 100644 arbalet/apps/bomberman/__main__.py create mode 100644 arbalet/apps/bomberman/bomberman.py create mode 100644 arbalet/apps/cfever/__init__.py create mode 100644 arbalet/apps/cfever/__main__.py create mode 100644 arbalet/apps/cfever/cfever.py create mode 100644 arbalet/apps/menu/__init__.py create mode 100644 arbalet/apps/menu/__main__.py create mode 100644 arbalet/apps/menu/menu.py create mode 100644 arbalet/apps/pong/__init__.py create mode 100644 arbalet/apps/pong/__main__.py create mode 100644 arbalet/apps/pong/pong.py create mode 100644 arbalet/apps/puissance4/__init__.py create mode 100644 arbalet/apps/puissance4/__main__.py create mode 100644 arbalet/apps/puissance4/puissance4.py diff --git a/arbalet/apps/bomberman/__init__.py b/arbalet/apps/bomberman/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arbalet/apps/bomberman/__main__.py b/arbalet/apps/bomberman/__main__.py new file mode 100644 index 0000000..73d84cc --- /dev/null +++ b/arbalet/apps/bomberman/__main__.py @@ -0,0 +1,2 @@ +from .bomberman import Bomberman +Bomberman().start() \ No newline at end of file diff --git a/arbalet/apps/bomberman/bomberman.py b/arbalet/apps/bomberman/bomberman.py new file mode 100644 index 0000000..66fcb25 --- /dev/null +++ b/arbalet/apps/bomberman/bomberman.py @@ -0,0 +1,368 @@ +import random +from arbalet.core import Application, Rate +import pygame +import time +import thread +from arbalet.colors import mul, hsv_to_rgb + +LEFT=(0,-1) +RIGHT=(0, 1) +DOWN=(1, 0) +UP=(-1, 0) +STOP=(0,0) + +# Set up the joystick +pygame.joystick.init() + +nbjoysticks = pygame.joystick.get_count() +joysticks = [] +for i in range(nbjoysticks): + joystick = pygame.joystick.Joystick(i) + joystick.init() + joysticks.append(joystick) + +class Bomb(): + def __init__(self, cord, player): + self.active = False + self.timer = 0 + self.player = player + self.cord = cord + +class Player(): + def __init__(self, pos, number, color): + self.alive = True + self.direction = STOP + self.pos = pos + self.number = number + self.color = color + + +class Bomberman(Application): + BG_COLOR = 'black' + WALL = 'grey' + COLORBOMB = 'orange' + DETONATEBOMB = 'orangered' + + + def __init__(self, touch_mode='quadridirectional'): + Application.__init__(self, touch_mode=touch_mode) + self.pierres = [] + + self.bomb1= Bomb((0,0),1) + self.bomb2= Bomb((0,0),2) + self.bomb3= Bomb((0,0),3) + self.bomb4= Bomb((0,0),4) + self.bomb5= Bomb((0,0),1) + self.bomb6= Bomb((0,0),2) + self.bomb7= Bomb((0,0),3) + self.bomb8= Bomb((0,0),4) + self.bomb9= Bomb((0,0),1) + self.bomb10= Bomb((0,0),2) + self.bomb11= Bomb((0,0),3) + self.bomb12= Bomb((0,0),4) + + self.slotbombs = [self.bomb1,self.bomb2,self.bomb3,self.bomb4,self.bomb5,self.bomb6,self.bomb7,self.bomb8,self.bomb9,self.bomb10,self.bomb11,self.bomb12] + self.bombs = [] + for i in range (1,self.height,2): + for j in range(self.width): + if(j==1 or j==3 or j==6 or j==8): + self.pierres.append((i,j)) + + + self.p1 = Player((14,5),1,'cyan') + self.p2 = Player((0,4),2,'darkblue') + self.p3 = Player((6,0),3,'green') + self.p4 = Player((8,9),4,'purple') + + def process_events(self): + new_dir1=None + new_dir2=None + new_dir3=None + new_dir4=None + for event in self.arbalet.events.get(): + key = pygame.key.get_pressed() + + if(len(joysticks)>=1): + if(self.p1.alive): + # Joystick control + if event.type==pygame.JOYAXISMOTION: + if joysticks[0].get_axis(1) < 0 and not(joysticks[0].get_axis(0) < 0 or joysticks[0].get_axis(0) > 0) and self.p1.pos[0]!=0: + new_dir1 = UP + if joysticks[0].get_axis(1) > 0 and not(joysticks[0].get_axis(0) < 0 or joysticks[0].get_axis(0) > 0) and self.p1.pos[0]!=14: + new_dir1 = DOWN + if joysticks[0].get_axis(0) > 0 and self.p1.pos[1]!=9: + new_dir1 = RIGHT + if joysticks[0].get_axis(0) < 0 and self.p1.pos[1]!=0: + new_dir1 = LEFT + if joysticks[0].get_button(0) or joysticks[0].get_button(1): + self.poserBombe(self.p1) + + if(len(joysticks)>=2): + if(self.p2.alive): + if event.type==pygame.JOYAXISMOTION: + if joysticks[1].get_axis(1) > 0 and self.p2.pos[0]!=0: + new_dir2 = UP + if joysticks[1].get_axis(1) < 0 and self.p2.pos[0]!=14: + new_dir2 = DOWN + if joysticks[1].get_axis(0) < 0 and not(joysticks[1].get_axis(1) < 0 or joysticks[1].get_axis(1) > 0) and self.p2.pos[1]!=9: + new_dir2 = RIGHT + if joysticks[1].get_axis(0) > 0 and not(joysticks[1].get_axis(1) < 0 or joysticks[1].get_axis(1) > 0) and self.p2.pos[1]!=0: + new_dir2 = LEFT + if joysticks[1].get_button(0) or joysticks[1].get_button(1): + self.poserBombe(self.p2) + + if(len(joysticks)>=3): + if(self.p3.alive): + # Joystick control + if event.type==pygame.JOYAXISMOTION: + if joysticks[2].get_axis(0) < 0 and self.p3.pos[0]!=0: + new_dir3 = UP + if joysticks[2].get_axis(0) > 0 and self.p3.pos[0]!=14: + new_dir3 = DOWN + if joysticks[2].get_axis(1) < 0 and not(joysticks[2].get_axis(0) < 0 or joysticks[2].get_axis(0) > 0) and self.p3.pos[1]!=9: + new_dir3 = RIGHT + if joysticks[2].get_axis(1) > 0 and not(joysticks[2].get_axis(0) < 0 or joysticks[2].get_axis(0) > 0) and self.p3.pos[1]!=0: + new_dir3 = LEFT + if joysticks[2].get_button(0) or joysticks[2].get_button(1): + self.poserBombe(self.p3) + + if(len(joysticks)>=4): + if(self.p4.alive): + # Joystick control + if event.type==pygame.JOYAXISMOTION: + if joysticks[3].get_axis(0) > 0 and not(joysticks[3].get_axis(1) < 0 or joysticks[3].get_axis(1) > 0) and self.p4.pos[0]!=0: + new_dir4 = UP + if joysticks[3].get_axis(0) < 0 and not(joysticks[3].get_axis(1) < 0 or joysticks[3].get_axis(1) > 0) and self.p4.pos[0]!=14: + new_dir4 = DOWN + if joysticks[3].get_axis(1) > 0 and self.p4.pos[1]!=9: + new_dir4 = RIGHT + if joysticks[3].get_axis(1) < 0 and self.p4.pos[1]!=0: + new_dir4 = LEFT + if joysticks[3].get_button(0) or joysticks[3].get_button(1): + self.poserBombe(self.p4) + + + # # Keyboard control + if event.type in [pygame.KEYDOWN, pygame.KEYUP]: + if(self.p1.alive): + if key[pygame.K_UP] and self.p1.pos[0]!=0: + new_dir1=UP + elif key[pygame.K_DOWN] and self.p1.pos[0]!=14: + new_dir1=DOWN + elif key[pygame.K_RIGHT] and self.p1.pos[1]!=9: + new_dir1 = RIGHT + elif key[pygame.K_LEFT] and self.p1.pos[1]!=0: + new_dir1 = LEFT + if key[pygame.K_p]: + self.poserBombe(self.p1) + + if(self.p2.alive): + if key[pygame.K_z] and self.p2.pos[0]!=0: + new_dir2=UP + elif key[pygame.K_s] and self.p2.pos[0]!=14: + new_dir2=DOWN + elif key[pygame.K_d] and self.p2.pos[1]!=9: + new_dir2 = RIGHT + elif key[pygame.K_q] and self.p2.pos[1]!=0: + new_dir2 = LEFT + if key[pygame.K_a]: + self.poserBombe(self.p2) + + #If you are using the cross instead of the joystick with your pad + # if event.type==pygame.JOYHATMOTION: + # if event.value[1]==1 and self.p3.pos[0]!=0: + # new_dir3 = UP + # elif event.value[1]==-1 and self.p3.pos[0]!=14: + # new_dir3 = DOWN + # elif event.value[0]==1 and self.p3.pos[1]!=9: + # new_dir3 = RIGHT + # elif event.value[0]==-1 and self.p3.pos[1]!=0: + # new_dir3 = LEFT + # if event.type == pygame.JOYBUTTONDOWN: + # self.poserBombe(self.p3) + + if new_dir1 is not None: + new_p1=((self.p1.pos[0]+new_dir1[0])%self.height, (self.p1.pos[1]+new_dir1[1])%self.width) + if new_p1 not in self.pierres and new_p1 not in self.bombs and new_p1 != self.p2.pos and new_p1 != self.p3.pos and new_p1 != self.p4.pos: + self.p1.direction=new_dir1 + else: + self.p1.direction=STOP + else: + self.p1.direction=STOP + + if new_dir2 is not None: + new_p2=((self.p2.pos[0]+new_dir2[0])%self.height, (self.p2.pos[1]+new_dir2[1])%self.width) + if new_p2 not in self.pierres and new_p2 not in self.bombs and new_p2 != self.p1.pos and new_p2 != self.p3.pos and new_p2 != self.p4.pos: + self.p2.direction=new_dir2 + else: + self.p2.direction=STOP + else: + self.p2.direction=STOP + + + if new_dir3 is not None: + new_p3=((self.p3.pos[0]+new_dir3[0])%self.height, (self.p3.pos[1]+new_dir3[1])%self.width) + if new_p3 not in self.pierres and new_p3 not in self.bombs and new_p3 != self.p1.pos and new_p3 != self.p2.pos and new_p3 != self.p4.pos: + self.p3.direction=new_dir3 + else: + self.p3.direction=STOP + else: + self.p3.direction=STOP + + if new_dir4 is not None: + new_p4=((self.p4.pos[0]+new_dir4[0])%self.height, (self.p4.pos[1]+new_dir4[1])%self.width) + if new_p4 not in self.pierres and new_p4 not in self.bombs and new_p4 != self.p1.pos and new_p4 != self.p2.pos and new_p4 != self.p3.pos: + self.p4.direction=new_dir4 + else: + self.p4.direction=STOP + else: + self.p4.direction=STOP + + def poserBombe(self, player): + newbombs = [] + for bomb in self.slotbombs: + if bomb.player == player.number: + newbombs.append(bomb) + for bomb in newbombs: + if not bomb.active: + if(self.bombAlreadySet(player)): + bomb.active = True + self.bombs.append((player.pos[0],player.pos[1])) + bomb.cord = (player.pos[0],player.pos[1]) + break + + def bombAlreadySet(self, player): + if player.pos in self.bombs: + return False + else: + return True + + def animateBombe(self,threadName): + ColorsBomb =['darkred','red','orange','yellow','white'] + while True: + for b in self.slotbombs: + if b.active: + if b.timer < 1.5: + self.model.set_pixel(b.cord[0],b.cord[1],self.DETONATEBOMB) + time.sleep(0.2) + self.model.set_pixel(b.cord[0],b.cord[1],self.COLORBOMB) + b.timer+=0.2*len(self.bombs) + else: + okH = True + okB = True + okG = True + okD = True + suprBomb = [b.cord] + for i in range(1,6): + #Haut + if okH and b.cord[0]-i >= 0 and (b.cord[0]-i,b.cord[1]) not in self.pierres: + self.model.set_pixel(b.cord[0]-i,b.cord[1],ColorsBomb[i-1]) + suprBomb.append((b.cord[0]-i,b.cord[1])) + else: + okH = False + #Bas + if okB and b.cord[0]+i <= 14 and (b.cord[0]+i,b.cord[1]) not in self.pierres: + self.model.set_pixel(b.cord[0]+i,b.cord[1],ColorsBomb[i-1]) + suprBomb.append((b.cord[0]+i,b.cord[1])) + else: + okB = False + #Gauche + if okG and b.cord[1]-i >= 0 and (b.cord[0],b.cord[1]-i) not in self.pierres: + self.model.set_pixel(b.cord[0],b.cord[1]-i,ColorsBomb[i-1]) + suprBomb.append((b.cord[0],b.cord[1]-i)) + else: + okG = False + #Droite + if okD and b.cord[1]+i <= 9 and (b.cord[0],b.cord[1]+i) not in self.pierres: + self.model.set_pixel(b.cord[0],b.cord[1]+i,ColorsBomb[i-1]) + suprBomb.append((b.cord[0],b.cord[1]+i)) + else: + okD = False + time.sleep(0.1) + if self.p1.pos in suprBomb: + self.p1.alive = False + self.p1.pos=None + if self.p2.pos in suprBomb: + self.p2.alive = False + self.p2.pos=None + if self.p3.pos in suprBomb: + self.p3.alive = False + self.p3.pos=None + if self.p4.pos in suprBomb: + self.p4.alive = False + self.p4.pos=None + time.sleep(0.2) + for supr in suprBomb: + self.model.set_pixel(supr[0],supr[1],self.BG_COLOR) + self.bombs.pop(0) + b.timer = 0 + b.active = False + + def updatePlayer(self, player): + new_p=((player.pos[0]+player.direction[0])%self.height, (player.pos[1]+player.direction[1])%self.width) + if(player.direction!=STOP and player.pos not in self.bombs): + self.model.set_pixel(player.pos[0], player.pos[1], self.BG_COLOR) + player.pos=new_p + self.model.set_pixel(new_p[0],new_p[1],player.color) + if player.pos in self.bombs: + self.model.set_pixel(new_p[0],new_p[1],self.COLORBOMB) + + def isOver(self, players): + alive = 0 + for player in players: + if player.alive: + alive +=1 + possibleWinner = player + if alive == 0: + return True + elif alive == 1: + return possibleWinner + elif alive >= 2: + return False + + def run(self): + self.model.set_pixel(self.p1.pos[0],self.p1.pos[1],self.p1.color) + self.model.set_pixel(self.p2.pos[0],self.p2.pos[1],self.p2.color) + self.model.set_pixel(self.p3.pos[0],self.p3.pos[1],self.p3.color) + self.model.set_pixel(self.p4.pos[0],self.p4.pos[1],self.p4.color) + + t1 = thread.start_new_thread(self.animateBombe,("animateBombe",)) + + rate = Rate(2) + + for i in range (1,self.height,2): + for j in range(self.width): + if(j==1 or j==3 or j==6 or j==8): + self.model.set_pixel(i,j,self.WALL) + + while True: + self.process_events() + + if(self.p1.alive): + self.updatePlayer(self.p1) + + if(self.p2.alive): + self.updatePlayer(self.p2) + + if(self.p3.alive): + self.updatePlayer(self.p3) + + if(self.p4.alive): + self.updatePlayer(self.p4) + + isOver = self.isOver([self.p1,self.p2,self.p3,self.p4]) + if(isOver is not False): + self.game_over(isOver) + break + + exit() + + + def game_over(self, winner): + print("Game OVER") + self.model.flash() + if(winner is True): + self.model.write("DRAW", 'white') + else: + self.model.write("P" + str(winner.number) + " WINS", winner.color) + diff --git a/arbalet/apps/cfever/__init__.py b/arbalet/apps/cfever/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arbalet/apps/cfever/__main__.py b/arbalet/apps/cfever/__main__.py new file mode 100644 index 0000000..27a199b --- /dev/null +++ b/arbalet/apps/cfever/__main__.py @@ -0,0 +1,2 @@ +from .cfever import Cfever +Cfever().start() \ No newline at end of file diff --git a/arbalet/apps/cfever/cfever.py b/arbalet/apps/cfever/cfever.py new file mode 100644 index 0000000..1c40a0e --- /dev/null +++ b/arbalet/apps/cfever/cfever.py @@ -0,0 +1,186 @@ +import random +from arbalet.core import Application, Rate +import pygame + +LEFT=(0,-1) +RIGHT=(0, 1) +DOWN=(1, 0) +UP=(-1, 0) + +K_LEFT2 = 113 +K_RIGHT2 = 100 +K_DOWN2 = 115 +K_UP2 = 122 + +# Set up the joystick +pygame.joystick.init() + +nbjoysticks = pygame.joystick.get_count() +joysticks = [] +for i in range(nbjoysticks): + joystick = pygame.joystick.Joystick(i) + joystick.init() + joysticks.append(joystick) + + +class Cfever(Application): + BG_COLOR = 'black' + P1COLOR = 'red' + P2COLOR = 'blue' + FOOD_COLOR='green' + + def __init__(self, touch_mode='quadridirectional'): + Application.__init__(self, touch_mode=touch_mode) + self.rate=2 + + self.DIRECTION1=DOWN + self.HEAD1=(1,1) + self.queue1=[self.HEAD1] + + self.DIRECTION2=UP + self.HEAD2=(13,8) + self.queue2=[self.HEAD2] + + self.FOOD_POSITIONS={} + self.rate_increase=0.15 + self.start_food=3 + + + + def process_events(self): + new_dir=None + new_dir2=None + for event in self.arbalet.events.get(): + if event.type==pygame.JOYAXISMOTION: + if joysticks[0].get_axis(1) < 0 and (self.DIRECTION1!=DOWN or len(self.queue1)<=1): + print("up") + new_dir=UP + if joysticks[0].get_axis(1) > 0 and (self.DIRECTION1!=UP or len(self.queue1)<=1): + print("down") + new_dir=DOWN + if joysticks[0].get_axis(0) > 0 and not(joysticks[0].get_axis(1) < 0 or joysticks[0].get_axis(1)) and (self.DIRECTION1!=LEFT or len(self.queue1)<=1): + print("right") + new_dir = RIGHT + if joysticks[0].get_axis(0) < 0 and not(joysticks[0].get_axis(1) < 0 or joysticks[0].get_axis(1)) and (self.DIRECTION1!=RIGHT or len(self.queue1)<=1): + new_dir = LEFT + + + if joysticks[1].get_axis(1) < 0 and (self.DIRECTION2!=DOWN or len(self.queue2)<=1): + print("up") + new_dir2=UP + if joysticks[1].get_axis(1) > 0 and (self.DIRECTION2!=UP or len(self.queue2)<=1): + print("down") + new_dir2=DOWN + if joysticks[1].get_axis(0) > 0 and not(joysticks[1].get_axis(1) < 0 or joysticks[0].get_axis(1)) and (self.DIRECTION2!=LEFT or len(self.queue2)<=1): + print("right") + new_dir2 = RIGHT + if joysticks[1].get_axis(0) < 0 and not(joysticks[1].get_axis(1) < 0 or joysticks[0].get_axis(1)) and (self.DIRECTION2!=RIGHT or len(self.queue2)<=1): + new_dir2 = LEFT + # Keyboard control + if event.type in [pygame.KEYDOWN, pygame.KEYUP]: + if event.key==pygame.K_UP and (self.DIRECTION1!=DOWN or len(self.queue1)<=1): + new_dir=UP + elif event.key==pygame.K_DOWN and (self.DIRECTION1!=UP or len(self.queue1)<=1): + new_dir=DOWN + elif event.key==pygame.K_RIGHT and (self.DIRECTION1!=LEFT or len(self.queue1)<=1): + new_dir = RIGHT + elif event.key==pygame.K_LEFT and (self.DIRECTION1!=RIGHT or len(self.queue1)<=1): + new_dir = LEFT + + elif event.key==K_UP2 and (self.DIRECTION2!=DOWN or len(self.queue2)<=1): + new_dir2=UP + elif event.key==K_DOWN2 and (self.DIRECTION2!=UP or len(self.queue2)<=1): + new_dir2=DOWN + elif event.key==K_RIGHT2 and (self.DIRECTION2!=LEFT or len(self.queue2)<=1): + new_dir2 = RIGHT + elif event.key==K_LEFT2 and (self.DIRECTION2!=RIGHT or len(self.queue2)<=1): + new_dir2 = LEFT + + if new_dir is not None: + self.DIRECTION1=new_dir + + if new_dir2 is not None: + self.DIRECTION2=new_dir2 + + + def process_extras(self, x=None, y=None): + pass + + def spawn_food(self, quantity=4): + for _ in range(0,quantity): + while True: + f=(random.randint(0,self.height-1),random.randint(0,self.width-1)) + if f not in self.queue1 and f not in self.queue2 and f not in self.FOOD_POSITIONS: + break + self.FOOD_POSITIONS[f]=True + + self.model.set_pixel(f[0], f[1], self.FOOD_COLOR) + + def run(self): + rate = Rate(self.rate) + self.model.set_all(self.BG_COLOR) + self.model.set_pixel(self.HEAD1[0],self.HEAD1[1],self.P1COLOR) + self.model.set_pixel(self.HEAD2[0],self.HEAD2[1],self.P2COLOR) + + self.spawn_food(self.start_food) + for x,y in self.FOOD_POSITIONS: + self.model.set_pixel(x, y, self.FOOD_COLOR) + + while True: + rate.sleep_dur=1.0/self.rate + with self.model: + self.process_events() + new_pos=((self.HEAD1[0]+self.DIRECTION1[0])%self.height, (self.HEAD1[1]+self.DIRECTION1[1])%self.width) + new_pos2=((self.HEAD2[0]+self.DIRECTION2[0])%self.height, (self.HEAD2[1]+self.DIRECTION2[1])%self.width) + + #check + if new_pos in self.queue1 or new_pos in self.queue2: + gagnant="J2" + break + + if new_pos2 in self.queue2 or new_pos2 in self.queue1: + gagnant="J1" + break + + self.HEAD1=new_pos + self.HEAD2=new_pos2 + + self.model.set_pixel(new_pos[0],new_pos[1],self.P1COLOR) + self.model.set_pixel(new_pos2[0],new_pos2[1],self.P2COLOR) + + self.queue1.append(new_pos) + self.queue2.append(new_pos2) + + if new_pos not in self.FOOD_POSITIONS: + x, y=self.queue1.pop(0) + self.model.set_pixel(x, y, self.BG_COLOR) + self.process_extras(x, y) + else: + del self.FOOD_POSITIONS[new_pos] + self.spawn_food(1) + self.rate+=self.rate_increase + self.process_extras() + + if new_pos2 not in self.FOOD_POSITIONS: + x, y=self.queue2.pop(0) + self.model.set_pixel(x, y, self.BG_COLOR) + self.process_extras(x, y) + else: + del self.FOOD_POSITIONS[new_pos2] + self.spawn_food(1) + self.rate+=self.rate_increase + self.process_extras() + rate.sleep() + self.game_over(gagnant) + exit() + + + def game_over(self, gagnant): + print("Game OVER") + self.model.flash() + if(gagnant=="J1"): + self.model.write("PLAYER 1 WINS", self.P1COLOR) + else: + self.model.write("PLAYER 2 WINS", self.P2COLOR) + + diff --git a/arbalet/apps/menu/__init__.py b/arbalet/apps/menu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arbalet/apps/menu/__main__.py b/arbalet/apps/menu/__main__.py new file mode 100644 index 0000000..e8bd716 --- /dev/null +++ b/arbalet/apps/menu/__main__.py @@ -0,0 +1,2 @@ +from .menu import Menu +Menu().start() diff --git a/arbalet/apps/menu/menu.py b/arbalet/apps/menu/menu.py new file mode 100644 index 0000000..e5dca40 --- /dev/null +++ b/arbalet/apps/menu/menu.py @@ -0,0 +1,329 @@ +import random +from arbalet.core import Application, Rate +import pygame +from pygame.locals import * +import time +import thread +import threading +from threading import Thread +from subprocess import call + +LEFT = -1 +RIGHT = 1 +LIMRIGHT = 10 +LIMLEFT = -1 + +K_LEFT1 = 113 +K_RIGHT1 = 100 + +# Set up the joystick +pygame.joystick.init() + +nbjoysticks = pygame.joystick.get_count() +joysticks = [] +for i in range(nbjoysticks): + joystick = pygame.joystick.Joystick(i) + joystick.init() + joysticks.append(joystick) + +class Menu(Application): + + BG_COLOR = 'black' + BTN_COLOR = 'green' + + def __init__(self): + Application.__init__(self) + pygame.init() + self.fin = False + self.games = ['pong','puissance4','tetris'] + self.indiceGame = 0 + self.choix = self.games[self.indiceGame] + self.PLATEAU_COLOR = 'blue' + self.launch = False + + def run(self): + self.init_grille() + + t2 = thread.start_new_thread(self.preview_game,("preview_game", )) + t2 = thread.start_new_thread(self.process_events,("process_events", )) + + while 1: + pass + + arbalet.close() + + def preview_game(self,threadName): + while self.fin == False: + self.clear_preview() + if self.choix == "pong": + ligne1 = 3 + ligne2 = 9 + dirX = 1 + dirY = 1 + BALLE = [ligne2-3,5] + retour = True; + while self.choix == "pong": + if retour or BALLE[0] == ligne2-1 or BALLE[0] == ligne1+1: + self.arbalet.user_model.set_pixel(ligne1, 7, self.BG_COLOR) + self.arbalet.user_model.set_pixel(ligne1, 8, self.BG_COLOR) + self.arbalet.user_model.set_pixel(ligne2, 1, self.BG_COLOR) + self.arbalet.user_model.set_pixel(ligne2, 2, self.BG_COLOR) + + for i in range(3,7): + self.arbalet.user_model.set_pixel(ligne1, i, 'red') + self.arbalet.user_model.set_pixel(ligne2, i, 'blue') + + retour = False + self.arbalet.user_model.set_pixel(BALLE[0], BALLE[1], self.BG_COLOR) + if (BALLE[0] == ligne1+1) or (BALLE[0] == ligne2-1): + retour = False + dirY = -dirY + dirX = -dirX + + if (BALLE[0] == ligne1+2): + self.arbalet.user_model.set_pixel(ligne1, 4, self.BG_COLOR) + self.arbalet.user_model.set_pixel(ligne1, 3, self.BG_COLOR) + for i in range(5,9): + self.arbalet.user_model.set_pixel(ligne1, i, 'red') + + if (BALLE[0] == ligne2-2): + self.arbalet.user_model.set_pixel(ligne2, 5, self.BG_COLOR) + self.arbalet.user_model.set_pixel(ligne2, 6, self.BG_COLOR) + for i in range(1,5): + self.arbalet.user_model.set_pixel(ligne2, i, 'blue') + + BALLE[0] += dirY + BALLE[1] -= dirX + + self.arbalet.user_model.set_pixel(BALLE[0], BALLE[1], 'white') + time.sleep(0.5) + + self.clear_preview() + + while self.choix == "puissance4": + colorp1 = 'yellow' + colorp2 = 'red' + col = 4 + ligne = 10 + for lig in range(4,10): + for colo in range(2,8): + self.arbalet.user_model.set_pixel(lig, colo, self.PLATEAU_COLOR) + + while ligne > 3 or self.choix == "puissance4": + self.model.set_pixel(3,col,colorp1) + time.sleep(0.16) + self.model.set_pixel(3,col,self.BG_COLOR) + for lig in range(4,ligne): + self.model.set_pixel(lig,col,colorp1) + if lig != 4: + self.model.set_pixel(lig-1,col,'blue') + time.sleep(0.16) + if ligne == 7 or self.choix != "puissance4": + break + self.model.set_pixel(3,col+1,colorp2) + time.sleep(0.16) + self.model.set_pixel(3,col+1,self.BG_COLOR) + for lig in range(4,ligne): + self.model.set_pixel(lig,col+1,colorp2) + if lig != 4: + self.model.set_pixel(lig-1,col+1,'blue') + time.sleep(0.16) + ligne -= 1 + if self.choix == "puissance4": + for i in range(3): + for i in range(0,4): + self.model.set_pixel(lig+i,col,'lightyellow') + time.sleep(0.16) + for i in range(0,4): + self.model.set_pixel(lig+i,col,colorp1) + time.sleep(0.16) + print self.choix + print self.indiceGame + if self.choix == "snake": + HEAD=(5,5) + queue=[self.HEAD] + pass + + while self.choix == "tetris": + self.clear_preview() + + for i in range (0,10): + if self.choix != "tetris": + break + self.model.set_pixel(i+1,4,'deeppink') + self.model.set_pixel(i,5,'deeppink') + self.model.set_pixel(i+1,5,'deeppink') + self.model.set_pixel(i+1,6,'deeppink') + if i> 0: + self.model.set_pixel(i-1,5,self.BG_COLOR) + self.model.set_pixel(i,4,self.BG_COLOR) + self.model.set_pixel(i,6,self.BG_COLOR) + time.sleep(0.16) + + for i in range(0,7): + if self.choix != "tetris": + break + self.model.set_pixel(i,2,'green') + self.model.set_pixel(i,3,'green') + self.model.set_pixel(i+1,3,'green') + self.model.set_pixel(i+1,4,'green') + if i> 0: + self.model.set_pixel(i-1,2,self.BG_COLOR) + self.model.set_pixel(i-1,3,self.BG_COLOR) + self.model.set_pixel(i,4,self.BG_COLOR) + + time.sleep(0.16) + + + self.model.set_pixel(6,2,self.BG_COLOR) + self.model.set_pixel(6,3,self.BG_COLOR) + + for i in range(7,9): + if self.choix != "tetris": + break + self.model.set_pixel(i,4,'green') + self.model.set_pixel(i+1,3,'green') + self.model.set_pixel(i+1,4,'green') + self.model.set_pixel(i+2,3,'green') + if i> 0: + self.model.set_pixel(i-1,4,self.BG_COLOR) + self.model.set_pixel(i,3,self.BG_COLOR) + + time.sleep(0.16) + + + + for i in range (0,10): + if self.choix != "tetris": + break + self.model.set_pixel(i+1,0,'orangered') + self.model.set_pixel(i+1,1,'orangered') + self.model.set_pixel(i+1,2,'orangered') + self.model.set_pixel(i,2,'orangered') + + if i> 0: + self.model.set_pixel(i-1,2,self.BG_COLOR) + + self.model.set_pixel(i,0,self.BG_COLOR) + self.model.set_pixel(i,1,self.BG_COLOR) + + time.sleep(0.16) + + + for i in range (0,8): + if self.choix != "tetris": + break + self.model.set_pixel(i,9,'yellow') + self.model.set_pixel(i+1,9,'yellow') + self.model.set_pixel(i+2,9,'yellow') + self.model.set_pixel(i+3,9,'yellow') + + if i> 0: + self.model.set_pixel(i-1,9,self.BG_COLOR) + + time.sleep(0.16) + + + for i in range (0,10): + if self.choix != "tetris": + break + self.model.set_pixel(i,7,'cyan') + self.model.set_pixel(i,8,'cyan') + self.model.set_pixel(i+1,7,'cyan') + self.model.set_pixel(i+1,8,'cyan') + + if i> 0: + self.model.set_pixel(i-1,7,self.BG_COLOR) + self.model.set_pixel(i-1,8,self.BG_COLOR) + + time.sleep(0.16) + + + + def clear_preview(self): + for lig in range(0,12): + for col in range(0,10): + self.arbalet.user_model.set_pixel(lig, col, self.BG_COLOR) + + + def process_events(self, threadName): + new_dir=None + while self.fin == False: + for event in self.arbalet.events.get(): + key = pygame.key.get_pressed(); + if event.type == pygame.QUIT: + self.fin = 'QUIT' + break + if event.type==pygame.JOYAXISMOTION: + if joysticks[0].get_axis(0) > 0: + new_dir = RIGHT + print "droite" + break + if joysticks[0].get_axis(0) < 0: + print "gauche" + new_dir = LEFT + break + + if event.type in [pygame.KEYDOWN, pygame.KEYUP]: + if event.key==pygame.K_RIGHT: + new_dir = RIGHT + print "droite" + break + + elif event.key==pygame.K_LEFT: + new_dir = LEFT + print "gauche" + break + + elif event.type==pygame.JOYHATMOTION: + if event.value[0]==1: + new_dir = RIGHT + print "droite" + break + elif event.value[0]==-1: + new_dir = LEFT + print "gauche" + break + if key[pygame.K_p]: + self.launch = True + + + if new_dir is not None : + if self.indiceGame == len(self.games)-1 and new_dir == RIGHT: + self.indiceGame = 0 + elif self.indiceGame == 0 and new_dir == LEFT: + self.indiceGame = len(self.games)-1 + else: + self.indiceGame += new_dir + new_dir=None + self.choix = self.games[self.indiceGame] + + if self.launch: + self.arbalet.close() + self.launchGame() + + time.sleep(1.2) + + def launchGame(self): + call(["python","-m","arbalet.apps."+self.choix]) + + + + def init_grille(self): + self.arbalet.user_model.set_all(self.BG_COLOR) + + self.arbalet.user_model.set_pixel(14, 4, 'yellow') + self.arbalet.user_model.set_pixel(14, 5, 'yellow') + self.arbalet.user_model.set_pixel(13, 4, 'yellow') + self.arbalet.user_model.set_pixel(13, 5, 'yellow') + + self.arbalet.user_model.set_pixel(14, 0, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(14, 1, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(13, 0, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(13, 1, self.BTN_COLOR) + + self.arbalet.user_model.set_pixel(14, 9, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(14, 8, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(13, 8, self.BTN_COLOR) + self.arbalet.user_model.set_pixel(13, 9, self.BTN_COLOR) + \ No newline at end of file diff --git a/arbalet/apps/pong/__init__.py b/arbalet/apps/pong/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arbalet/apps/pong/__main__.py b/arbalet/apps/pong/__main__.py new file mode 100644 index 0000000..a132b0a --- /dev/null +++ b/arbalet/apps/pong/__main__.py @@ -0,0 +1,2 @@ +from .pong import Pong +Pong().start() diff --git a/arbalet/apps/pong/pong.py b/arbalet/apps/pong/pong.py new file mode 100644 index 0000000..0cd1c75 --- /dev/null +++ b/arbalet/apps/pong/pong.py @@ -0,0 +1,330 @@ +import random +from arbalet.core import Application, Rate +import pygame +from pygame.locals import * +import time +import thread +import threading +from threading import Thread +from subprocess import call + +LEFT = -1 +RIGHT = 1 +LIMRIGHT = 10 +LIMLEFT = -1 + +K_LEFT1 = 113 +K_RIGHT1 = 100 + +# Set up the joystick +pygame.joystick.init() + +nbjoysticks = pygame.joystick.get_count() +joysticks = [] +for i in range(nbjoysticks): + joystick = pygame.joystick.Joystick(i) + joystick.init() + joysticks.append(joystick) + +print joysticks + +class Pong(Application): + + BG_COLOR = 'black' + BALLE_COLOR = 'white' + P1_COLOR = 'blue' + P2_COLOR = 'red' + + def __init__(self): + Application.__init__(self) + pygame.init() + self.BALLE = [7,random.randrange(0,9)] + self.P1_RACKET = range(3,6) + self.P2_RACKET = range(3,6) + self.P1_DIRECTION = [] + self.P2_DIRECTION = [] + self.P1_SCORE = 0 + self.P2_SCORE = 0 + self.SCORE_WIN = 5 + self.TABJEU = [[0] * 10 for _ in range(14)] + self.fin = False + self.ballspeed_increase = 0.01 + self.ballspeed = 0.5 + self.limspeed = 0.11 + self.RATE_SPEED = 2 + self.lenP1_RACKET = len(self.P1_RACKET) + self.lenP2_RACKET = len(self.P2_RACKET) + + def run(self): + rate = Rate(self.RATE_SPEED) + if self.P1_SCORE < self.SCORE_WIN or self.P2_SCORE < self.SCORE_WIN: + self.init_grille() + t1 = thread.start_new_thread(self.wait_for_timeout_or_event1,("wait_for_timeout_or_event1", )) + #t2 = thread.start_new_thread(self.wait_for_timeout_or_event2,("wait_for_timeout_or_event2", )) + t3 = thread.start_new_thread(self.animationBalle,("animationBalle", )) + while 1: + pass + time.sleep(2) + + def process_events1(self): + new_dir=None + while self.fin == False: + for event in self.arbalet.events.get(): + if event.type == pygame.QUIT: + self.fin = 'QUIT' + + # if joysticks[0].get_hat(0) == (1,0) or joysticks[0].get_hat(0) == (1,1) or joysticks[0].get_hat(0) == (1,-1): + # new_dir = RIGHT + # if (self.P1_RACKET[self.lenP1_RACKET-1]+new_dir) < LIMRIGHT: + # self.model.set_pixel(0,self.P1_RACKET[0], self.BG_COLOR) + + # for i in range(self.lenP1_RACKET): + # self.P1_RACKET[i] += new_dir + + # self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.P1_COLOR) + # print self.P1_RACKET + # break + # if joysticks[0].get_hat(0) == (-1,0) or joysticks[0].get_hat(0) == (-1,1) or joysticks[0].get_hat(0) == (-1,-1): + # new_dir = LEFT + # if (self.P1_RACKET[0]+new_dir) > LIMLEFT: + # self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.BG_COLOR) + + # for i in range(self.lenP1_RACKET): + # self.P1_RACKET[i] += new_dir + + # self.model.set_pixel(0,self.P1_RACKET[0], self.P1_COLOR) + # print self.P1_RACKET + # break + + if event.type in [pygame.KEYDOWN, pygame.KEYUP]: + if event.key==pygame.K_RIGHT: + new_dir = RIGHT + if (self.P1_RACKET[self.lenP1_RACKET-1]+new_dir) < LIMRIGHT: + self.model.set_pixel(0,self.P1_RACKET[0], self.BG_COLOR) + + for i in range(self.lenP1_RACKET): + self.P1_RACKET[i] += new_dir + + self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.P1_COLOR) + + elif event.key==pygame.K_LEFT: + new_dir = LEFT + if (self.P1_RACKET[0]+new_dir) > LIMLEFT: + self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.BG_COLOR) + + for i in range(self.lenP1_RACKET): + self.P1_RACKET[i] += new_dir + + self.model.set_pixel(0,self.P1_RACKET[0], self.P1_COLOR) + + if event.type==pygame.JOYAXISMOTION: + if joysticks[0].get_axis(0) < 0: + new_dir = RIGHT + if (self.P1_RACKET[self.lenP1_RACKET-1]+new_dir) < LIMRIGHT: + self.model.set_pixel(0,self.P1_RACKET[0], self.BG_COLOR) + + for i in range(self.lenP1_RACKET): + self.P1_RACKET[i] += new_dir + + self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.P1_COLOR) + + if joysticks[0].get_axis(0) > 0: + new_dir = LEFT + if (self.P1_RACKET[0]+new_dir) > LIMLEFT: + self.model.set_pixel(0,self.P1_RACKET[self.lenP1_RACKET-1], self.BG_COLOR) + + for i in range(self.lenP1_RACKET): + self.P1_RACKET[i] += new_dir + + self.model.set_pixel(0,self.P1_RACKET[0], self.P1_COLOR) + + if joysticks[1].get_axis(0) > 0: + new_dir = RIGHT + if (self.P2_RACKET[self.lenP2_RACKET-1]+new_dir) < LIMRIGHT: + self.model.set_pixel(14,self.P2_RACKET[0], self.BG_COLOR) + + for i in range(self.lenP2_RACKET): + self.P2_RACKET[i] += new_dir + + self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.P2_COLOR) + + + if joysticks[1].get_axis(0) < 0: + new_dir = LEFT + if (self.P2_RACKET[0]+new_dir) > LIMLEFT: + self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.BG_COLOR) + + for i in range(self.lenP2_RACKET): + self.P2_RACKET[i] += new_dir + + self.model.set_pixel(14,self.P2_RACKET[0],self.P2_COLOR) + + + def wait_for_timeout_or_event1(self, threadName): + t0 = time.time() + while time.time()-t0 < 1./self.RATE_SPEED: + time.sleep(0.07) + if self.process_events1(): + return + + # def wait_for_timeout_or_event2(self, threadName): + # t0 = time.time() + # while time.time()-t0 < 1./self.RATE_SPEED: + # time.sleep(0.07) + # if self.process_events2(): + # return + + # def process_events2(self): + # new_dir=None + # while self.fin == False: + # for event in self.arbalet.events.get(): + # # if joysticks[1].get_hat(0) == (1,0): + # # new_dir = RIGHT + # # if (self.P2_RACKET[self.lenP2_RACKET-1]+new_dir) < LIMRIGHT: + # # self.model.set_pixel(14,self.P2_RACKET[0], self.BG_COLOR) + + # # for i in range(self.lenP2_RACKET): + # # self.P2_RACKET[i] += new_dir + + # # self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.P2_COLOR) + # # print self.P2_RACKET + # # break + # if joysticks[1].get_hat(0) == (-1,0): + # new_dir = LEFT + # if (self.P2_RACKET[0]+new_dir) > LIMLEFT: + # self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.BG_COLOR) + + # for i in range(self.lenP2_RACKET): + # self.P2_RACKET[i] += new_dir + + # self.model.set_pixel(14,self.P2_RACKET[0],self.P2_COLOR) + # print self.P2_RACKET + # break + + + + # # elif event.type in [pygame.KEYDOWN, pygame.KEYUP]: + # # if event.key == K_RIGHT1: + # # new_dir = RIGHT + # # if (self.P2_RACKET[self.lenP2_RACKET-1]+new_dir) < LIMRIGHT: + # # self.model.set_pixel(14,self.P2_RACKET[0], self.BG_COLOR) + + # # for i in range(self.lenP2_RACKET): + # # self.P2_RACKET[i] += new_dir + + # # self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.P2_COLOR) + # # print self.P2_RACKET + # # break + # # elif event.key==K_LEFT1: + # # new_dir = LEFT + # # if (self.P2_RACKET[0]+new_dir) > LIMLEFT: + # # self.model.set_pixel(14,self.P2_RACKET[self.lenP2_RACKET-1], self.BG_COLOR) + + # # for i in range(self.lenP2_RACKET): + # # self.P2_RACKET[i] += new_dir + + # # self.model.set_pixel(14,self.P2_RACKET[0],self.P2_COLOR) + # # print self.P2_RACKET + # # break + + def init_grille(self): + self.arbalet.user_model.set_all(self.BG_COLOR) + for col in self.P1_RACKET: + self.arbalet.user_model.set_pixel(0, col, self.P1_COLOR) + self.arbalet.user_model.set_pixel(14, col, self.P2_COLOR) + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BALLE_COLOR) + + def nextRound(self): + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BG_COLOR) + self.ballspeed = 0.5 + self.BALLE = [7,random.randrange(0,9)] + print " SCORE P1 : " + str(self.P1_SCORE) + " SCORE P2 : " + str(self.P2_SCORE) + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BALLE_COLOR) + time.sleep(1.5) + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BG_COLOR) + + + + def animationBalle(self, threadName): + while self.fin == False: + directionY = random.randrange(-1, 1, 2) + directionX = random.randrange(-1, 1, 2) + + while 1: + # CONDITIONS DE VICTOIRES + if self.BALLE[0] == 0: + self.P2_SCORE += 1 + self.nextRound() + if self.P2_SCORE == self.SCORE_WIN: + self.fin = "P2" + break + continue + if self.BALLE[0] == 14: + self.P1_SCORE += 1 + self.nextRound() + if self.P1_SCORE == self.SCORE_WIN: + self.fin = "P1" + break + continue + + olddireX = directionX + + # CONTACT ENTRE RAQUETTE ET BALLE + if self.BALLE[0] == 13: + if (self.BALLE[1] in self.P2_RACKET or self.BALLE[1] == self.P2_RACKET[self.lenP2_RACKET-1]+1 or self.BALLE[1] == self.P2_RACKET[0]-1): + if self.BALLE[1] == LIMLEFT+1 or self.BALLE[1] == LIMRIGHT-1: + balleApres = -directionX + else: + balleApres = directionX + + directionY = -directionY + + if (self.BALLE[1]+balleApres not in self.P2_RACKET): + self.P1_SCORE += 1 + self.nextRound() + if self.P1_SCORE == self.SCORE_WIN: + self.fin = "P1" + break + continue + + elif self.BALLE[0] == 1: + if (self.BALLE[1] in self.P1_RACKET or self.BALLE[1] == self.P1_RACKET[self.lenP1_RACKET-1]+1 or self.BALLE[1] == self.P1_RACKET[0]-1): + if self.BALLE[1] == LIMLEFT+1 or self.BALLE[1] == LIMRIGHT-1: + balleApres = -directionX + else: + balleApres = directionX + + directionY = -directionY + + if (self.BALLE[1]+balleApres not in self.P1_RACKET): + self.P2_SCORE += 1 + self.nextRound() + if self.P2_SCORE == self.SCORE_WIN: + self.fin = "P2" + break + continue + + # CONTACT ENTRE BALLE ET COIN + if self.BALLE[1] == LIMLEFT+1 or self.BALLE[1] == LIMRIGHT-1 or (self.BALLE[0] == 1 and self.BALLE[1] == self.P2_RACKET[0]-1 and self.BALLE[1]+olddireX in self.P2_RACKET) or (self.BALLE[0] == 1 and self.BALLE[1] == self.P1_RACKET[0]-1 and self.BALLE[1]+olddireX in self.P1_RACKET) or (self.BALLE[0] == 13 and self.BALLE[1] == self.P1_RACKET[self.lenP1_RACKET-1]+1 and self.BALLE[1]+olddireX in self.P1_RACKET) or (self.BALLE[0] == 13 and self.BALLE[1] == self.P2_RACKET[self.lenP2_RACKET-1]+1 and self.BALLE[1] in self.P2_RACKET): + directionX = -directionX + + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BG_COLOR) + # MAJ POS + self.BALLE[0] += directionY + self.BALLE[1] += directionX + + # AFFICHAGE NEW POS + self.arbalet.user_model.set_pixel(self.BALLE[0], self.BALLE[1], self.BALLE_COLOR) + + time.sleep(self.ballspeed) + + if self.ballspeed >= self.limspeed: + self.ballspeed -= self.ballspeed_increase + + if self.fin == 'P1': + self.model.write("PLAYER 1 WINS!", self.P1_COLOR) + if self.fin == 'P2': + self.model.write("PLAYER 2 WINS!", self.P2_COLOR); + #call(["python","-m","arbalet.apps.menu"]) + self.arbalet.close() + exit() \ No newline at end of file diff --git a/arbalet/apps/puissance4/__init__.py b/arbalet/apps/puissance4/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arbalet/apps/puissance4/__main__.py b/arbalet/apps/puissance4/__main__.py new file mode 100644 index 0000000..5346095 --- /dev/null +++ b/arbalet/apps/puissance4/__main__.py @@ -0,0 +1,2 @@ +from .puissance4 import Puissance4 +Puissance4().start() diff --git a/arbalet/apps/puissance4/puissance4.py b/arbalet/apps/puissance4/puissance4.py new file mode 100644 index 0000000..f3c399e --- /dev/null +++ b/arbalet/apps/puissance4/puissance4.py @@ -0,0 +1,249 @@ +import random +from arbalet.core import Application, Rate +import pygame +import time +from subprocess import call + +LEFT=(0,-1) +RIGHT=(0, 1) +LIMRIGHT = 8 +LIMLEFT = 0 + +# Set up the joystick +pygame.joystick.init() + +nbjoysticks = pygame.joystick.get_count() +joysticks = [] +for i in range(nbjoysticks): + joystick = pygame.joystick.Joystick(i) + joystick.init() + joysticks.append(joystick) + +class Puissance4(Application): + BG_COLOR = 'black' + PLATEAU_COLOR = 'blue' + P1_COLOR = 'yellow' + P1_FLASHCOLOR = 'lightyellow' + P2_FLASHCOLOR = 'crimson' + P2_COLOR = 'red' + P_COLOR = P1_COLOR + RATE_SPEED = 1.5 + COLPLEINES = [] + CASESGAGNANTES = [] + + def __init__(self): + Application.__init__(self) + self.PIECE = (8,4) + self.TABJEU = [[0] * 7 for _ in range(6)] + self.DIRECTION = (0,0) + self.fin = False + self.rate = Rate(self.RATE_SPEED) + self.player_actu = True + + def run(self): + self.init_grille() + self.deroulementJeu() + if self.fin == 'draw': + self.model.flash() + self.model.write("DRAW!", 'deeppink') + elif self.fin == 'J1': + self.animationWin(self.fin) + self.model.write("YELLOW WINS!", self.P1_COLOR) + elif self.fin == 'J2': + self.animationWin(self.fin) + self.model.write("RED WINS!", self.P2_COLOR) + + call(["python","-m","arbalet.apps.menu"]) + self.arbalet.close() + exit() + + def process_events(self): + new_dir=None + while 1: + for event in self.arbalet.events.get(): + key = pygame.key.get_pressed() + if(len(joysticks)>=1): + # Joystick control + if event.type==pygame.JOYAXISMOTION: + if joysticks[0].get_axis(0) > 0: + new_dir = RIGHT + if joysticks[0].get_axis(0) < 0: + new_dir = LEFT + if joysticks[0].get_button(0) or joysticks[0].get_button(1): + new_dir = (0,0) + self.DIRECTION=new_dir + break + if event.type in [pygame.KEYDOWN, pygame.KEYUP]: + if key[pygame.K_q]: + self.fin = 'QUIT' + break + if event.key==pygame.K_RIGHT: + new_dir = RIGHT + elif event.key==pygame.K_LEFT: + new_dir = LEFT + elif event.key==pygame.K_DOWN: + new_dir = (0,0) + self.DIRECTION=new_dir + break + if event.type==pygame.JOYHATMOTION: + if event.value[1]==-1: + new_dir = (0,0) + self.DIRECTION=new_dir + break + if event.value[0]==1: + new_dir = RIGHT + elif event.value[0]==-1: + new_dir = LEFT + + if new_dir is not None: + if (self.PIECE[1]+new_dir[1]) == LIMRIGHT or (self.PIECE[1]+new_dir[1]) == LIMLEFT: + new_dir = None + else: + self.DIRECTION=new_dir + break + + def wait_for_timeout_or_event(self, allow_events=True): + t0 = time.time() + while time.time()-t0 < 1./self.RATE_SPEED: + time.sleep(0.07) + if allow_events and self.process_events(): + return + + def init_grille(self): + self.arbalet.user_model.set_all(self.BG_COLOR) + for lig in range(9,15): + for col in range(1,8): + self.arbalet.user_model.set_pixel(lig, col, self.PLATEAU_COLOR) + self.arbalet.user_model.set_pixel(self.PIECE[0], self.PIECE[1], self.P_COLOR) + + def deroulementJeu(self): + while 1: + while 1: + self.wait_for_timeout_or_event() + if (self.DIRECTION[0] == 0 and self.DIRECTION[1] == 0): + break + else: + self.model.set_pixel(self.PIECE[0],self.PIECE[1],self.BG_COLOR) + + new_pos=(self.PIECE[0]+self.DIRECTION[0], self.PIECE[1]+self.DIRECTION[1]) + self.PIECE=new_pos + self.model.set_pixel(self.PIECE[0],self.PIECE[1],self.P_COLOR) + + self.set_piece() + + testFin = self.game_over() + if testFin is not False: + self.fin = testFin + break + elif self.fin == 'draw': + break + + + def set_piece(self): + coltabjeu = self.PIECE[1]-1 + if coltabjeu not in self.COLPLEINES: + lignetabjeu = 5 + while self.TABJEU[lignetabjeu][coltabjeu] != 0: + if lignetabjeu == 0: + self.COLPLEINES.append(coltabjeu) + break + lignetabjeu -= 1 + + if lignetabjeu >= 0: + col = self.PIECE[1] + ligne = lignetabjeu+9 + + if lignetabjeu == 0: + self.COLPLEINES.append(coltabjeu) + if len(self.COLPLEINES) == 7: + self.fin = 'draw' + + self.model.set_pixel(self.PIECE[0],col,self.BG_COLOR) + self.model.set_pixel(self.PIECE[0]+1,col,self.P_COLOR) + time.sleep(0.1) + + for i in range(9,ligne): + self.model.set_pixel(i,col,self.PLATEAU_COLOR) + self.model.set_pixel(i+1,col,self.P_COLOR) + time.sleep(0.1) + + self.player_actu = not self.player_actu + + if(self.player_actu): + self.P_COLOR = self.P1_COLOR + self.TABJEU[lignetabjeu][coltabjeu] = 'J2' + else: + self.P_COLOR = self.P2_COLOR + self.TABJEU[lignetabjeu][coltabjeu] = 'J1' + + self.model.set_pixel(self.PIECE[0],col,self.P_COLOR) + + def animationWin(self,joueur): + if joueur == 'J1': + coulJ = self.P1_COLOR + coulAlt = self.P1_FLASHCOLOR + elif joueur == 'J2': + coulJ = self.P2_COLOR + coulAlt = self.P2_FLASHCOLOR + + for i in range(6): + for case in self.CASESGAGNANTES: + self.arbalet.user_model.set_pixel(case[0]+9, case[1]+1, coulAlt) + self.arbalet.user_model.set_pixel(case[0]+9, case[1]+1, coulAlt) + time.sleep(0.3) + for case in self.CASESGAGNANTES: + self.arbalet.user_model.set_pixel(case[0]+9, case[1]+1, coulJ) + self.arbalet.user_model.set_pixel(case[0]+9, case[1]+1, coulJ) + time.sleep(0.3) + + + + def game_over(self): + for ligne in range(6): + for col in range(7): + if self.TABJEU[ligne][col] == 'J1': + if col < 4: + #Parcours droite + if self.TABJEU[ligne][col+1] == 'J1' and self.TABJEU[ligne][col+2] == 'J1' and self.TABJEU[ligne][col+3] == 'J1': + self.CASESGAGNANTES = [(ligne,col),(ligne,col+1),(ligne,col+2),(ligne,col+3)] + return 'J1' + if ligne < 3 and col < 4: + #Parcours diag bas droite + if self.TABJEU[ligne+1][col+1] == 'J1' and self.TABJEU[ligne+2][col+2] == 'J1' and self.TABJEU[ligne+3][col+3] == 'J1': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col+1),(ligne+2,col+2),(ligne+3,col+3)] + return 'J1' + if ligne < 3: + #Parcours bas + if self.TABJEU[ligne+1][col] == 'J1' and self.TABJEU[ligne+2][col] == 'J1' and self.TABJEU[ligne+3][col] == 'J1': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col),(ligne+2,col),(ligne+3,col)] + return 'J1' + if col > 2 and ligne < 3 : + #parcours diag bas gauche + if self.TABJEU[ligne+1][col-1] == 'J1' and self.TABJEU[ligne+2][col-2] == 'J1' and self.TABJEU[ligne+3][col-3] == 'J1': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col-1),(ligne+2,col-2),(ligne+3,col-3)] + return 'J1' + if self.TABJEU[ligne][col] == 'J2': + if col < 4: + #Parcours droite + if self.TABJEU[ligne][col+1] == 'J2' and self.TABJEU[ligne][col+2] == 'J2' and self.TABJEU[ligne][col+3] == 'J2': + self.CASESGAGNANTES = [(ligne,col),(ligne,col+1),(ligne,col+2),(ligne,col+3)] + return 'J2' + if ligne < 3 and col < 4: + #Parcours diag bas droite + if self.TABJEU[ligne+1][col+1] == 'J2' and self.TABJEU[ligne+2][col+2] == 'J2' and self.TABJEU[ligne+3][col+3] == 'J2': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col+1),(ligne+2,col+2),(ligne+3,col+3)] + return 'J2' + if ligne < 3: + #Parcours bas + if self.TABJEU[ligne+1][col] == 'J2' and self.TABJEU[ligne+2][col] == 'J2' and self.TABJEU[ligne+3][col] == 'J2': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col),(ligne+2,col),(ligne+3,col)] + return 'J2' + if col > 2 and ligne < 3 : + #parcours diag bas gauche + if self.TABJEU[ligne+1][col-1] == 'J2' and self.TABJEU[ligne+2][col-2] == 'J2' and self.TABJEU[ligne+3][col-3] == 'J2': + self.CASESGAGNANTES = [(ligne,col),(ligne+1,col-1),(ligne+2,col-2),(ligne+3,col-3)] + return 'J2' + return False + + +