Skip to content

Instantly share code, notes, and snippets.

@TheBonnec
Last active May 10, 2021 19:30
Show Gist options
  • Save TheBonnec/6d32898c1d85df2fc2ed01b347403b59 to your computer and use it in GitHub Desktop.
Save TheBonnec/6d32898c1d85df2fc2ed01b347403b59 to your computer and use it in GitHub Desktop.
Puissance 4
# Version 1.1
from math import *
from kandinsky import *
class GEBlock:
def __init__(self, ID, x,y,w,h, col):
self.ID = ID
self.x = x
self.y = y
self.width = w
self.height = h
self.color = col
self.oldPos = (0,0,0,0)
def move(self,x,y):
self.x += x
self.y += y
def changeFrame(self,w,h):
self.width = w
self.height = h
def changeColor(self,col):
self.color = col
def updateOldPos(self):
s = self
self.oldPos = (s.x,s.y,s.width,s.height)
class GEView:
def __init__(self, bg):
self.blocks = []
self.background = bg
def addBlock(self, block, pos = 0):
self.blocks.insert(pos,block)
def addBlocks(self, blocks, pos = 0):
for b in range(blocks):
self.blocks.insert(pos, blocks[-b])
def getBlock(self, ID):
for b in self.blocks:
if b.ID == ID:
return b
return False
def reload(self):
if (self.blocks, self.background) != self.OLDPOS:
if self.background == self.OLDPOS[0][1]:
self.smallReload()
else:
self.hardReload()
def hardReload(self):
fill_rect(0,0,320,222,self.background)
for b in self.blocks:
fill_rect(rInt(b.x), rInt(b.y), rInt(b.width), rInt(b.height), b.color)
def smallReload(self):
for b in self.blocks:
fill_rect(rInt(b.x), rInt(b.y), rInt(b.width), rInt(b.height), b.color)
zte = zte(b)
for z in zte:
fill_rect(rInt(z[0]), rInt(z[1]), rInt(z[2]), rInt(z[3]), self.background)
if zte != []:
b.updateOldPos()
def changeBackground(self,bg):
self.background = bg
def searchBlockByID(self,i):
for j in range(self.blocks):
if self.blocks[j].ID == i:
return j
def zte(self, b):
o = b.oldPos
dx, dy = rInt(b.x) - rInt(o[0]), rInt(b.y) - rInt(o[1])
if dx >= b.width or dy >= b.height:
return [(o[0], o[1], o[2], o[3])]
r = []
if dx != 0:
r.append((o[0]+o[2]/2-(dx/abs(dx) * (o[2]/2)), o[1], dx, o[3]))
if dy != 0:
r.append((o[0], o[1]+o[3]/2-(dy/abs(dy) * (o[3]/2)), o[2], dy))
return r
# Other functions
def rInt(i):
return int(round(i,0))
from math import *
global model
model = "n0110"
'''
Version 1.0c
@TBonnec
'''
from kandinsky import *
from math import *
from time import *
from ion import *
try:
from games_engine import *
except:
print("\n\nERREUR: Games Engine \nintrouvalble \n\nPour jouer a ce jeu, vous \ndevez installer Games Engine\n\n\n")
kd = keydown
def setSettings(mod="2"):
global bg1,bg2,bg3,wh,ye,re,bl,lg
global model
if model == "n0100":
bg1, bg2, bg3 = (46,46,40), (77,75,70), (112,112,102)
wh, ye, re, bl, lg = (255,255,230), (255,190,50), (255,115,55), (110,150,165), (210,185,140)
else:
bg1, bg2, bg3 = (40,40,40), (67,70,80), (95,100,120)
wh, ye, re, bl, lg = (255,255,255), (255,230,70), (245,120,90), (90,120,245), (190,190,190)
def mainTitle():
setSettings()
drawMainTitle()
while True:
m = selectionView(197,35,113,177,
[["Jouer",45,"OK",True],
["Sortir",70,"OK",True]],
[["v 1.0c",205,165,bg2],
["@TBonnec",205,185,bg2]])
if m[0] == 0:
return "game"
elif m[0] == 1:
return "exit"
def drawMainTitle():
fill_rect(0,0,320,222,bg1)
draw_string("PUISSANCE 4",105,10,wh,bg1)
grid()
def selectionView(x,y,w,h,menus,txts):
px, py, pw, ph = x+3, y+3, w-6, h-6
cursor = 0
colors = [ye]
for m in range(len(menus)):
colors.append(wh)
fill_rect(x,y,w,h,bg2)
def updateView():
fill_rect(px,py,pw,ph,bg3)
for m in range(len(menus)):
s = menus[m][0]
draw_string(s,x+8,menus[m][1],colors[m],bg3)
if cursor == m and menus[m][2] != None:
draw_string(menus[m][2], (x+w-len(menus[m][2])*10-8), menus[m][1],ye,bg3)
if txts != None:
for t in txts:
draw_string(t[0],t[1],t[2],t[3],bg3)
updateView()
while True:
if kd(KEY_DOWN):
colors[cursor] = wh
cursor = min(cursor+1, len(menus)-1)
colors[cursor] = ye
updateView()
lock(KEY_DOWN)
elif kd(KEY_UP):
colors[cursor] = wh
cursor = max(cursor-1, 0)
colors[cursor] = ye
updateView()
lock(KEY_UP)
elif kd(KEY_OK) or kd(KEY_EXE):
lock(KEY_OK)
lock(KEY_EXE)
if menus[cursor][-1]:
r = [cursor]
return r
def lock(key):
while kd(key):
sleep(0.01)
class Jeton(GEBlock):
def __init__(self,ID,caseX,caseY,col,player):
GEBlock.__init__(self,ID,caseX * 25 + 14, caseY * 25 + 64,19,19,col)
self.caseX = caseX
self.caseY = caseY
self.player = player
def moveDown(self):
self.caseY += 1
self.move(0,25)
class Board(GEView):
def __init__(self,blocks,bg,n):
GEView.__init__(self,blocks,bg,n)
self.fixedBlocks = []
def searchWinner(self, block):
lines = ((0,1), (1,0), (1,1), (-1,1))
joueur = block[2]
for l in lines:
compteur = []
for i in range(7):
x = l[0]*(i-3) + block[0]
y = l[1]*(i-3) + block[1]
for b in self.fixedBlocks:
if b[0] == x and b[1] == y:
if b[2] == joueur:
compteur.append((b[0]*25+14,b[1]*25+64))
else:
compteur = []
if len(compteur) >= 4 or len(self.fixedBlocks) >= 42:
for c in compteur:
fill_rect(c[0]+4, c[1]+4,11,11,ye)
draw_string("Appuyez sur 'OK' pour continuer",5,9,wh,bg1)
while True:
if kd(KEY_OK):
lock(KEY_OK)
break
return joueur
self.blocks = []
return -1
def addJeton(self,jeton):
p = True
for b in self.fixedBlocks:
if b[0] == jeton.caseX and b[1] == 0:
p = False
if p:
self.blocks.append(jeton)
self.__goesDown__()
return p
def __goesDown__(self):
nb = self.blocks[-1]
q = False
while nb.caseY < 5:
for b in self.fixedBlocks:
if b[0] == nb.caseX and b[1] == nb.caseY + 1:
q = True
self.smallReload()
if q:
break
else:
self.blocks[-1].moveDown()
self.smallReload()
sleep(0.05)
self.fixedBlocks.append((nb.caseX,nb.caseY,nb.player))
self.blocks = []
global view, sX, caseX, jNb, player
def game():
global view, sX, caseX, jNb, player
resetGame()
jCols = [re,bl]
pList = ["Rouge", "Bleu"]
exit = False
while True:
if kd(KEY_LEFT) or kd(KEY_RIGHT):
fill_rect(sX,42,10,10,bg1)
dx = kd(KEY_RIGHT) - kd(KEY_LEFT)
sX = int(min(max(sX + (dx/abs(dx)*25), 17), 167))
caseX = int(min(max(caseX+dx, 0), 6))
lock(KEY_LEFT)
lock(KEY_RIGHT)
elif kd(KEY_OK):
if view.addJeton(Jeton(str(jNb),caseX,0,jCols[player],player)):
jNb += 1
player = (player==0)+0
while kd(KEY_OK):
sleep(0.01)
fill_rect(sX,42,10,10,jCols[player])
view.reload()
if view.fixedBlocks != []:
s = view.searchWinner(view.fixedBlocks[-1])
if s != -1:
return "menu"
sleep(0.02)
def resetGame():
global view, sX, caseX, jNb, player
view = Board([],bg1,1)
sX = 17
caseX = 0
jNb = 0
player = 0
view.smallReload()
fill_rect(10,60,152,177,bg1)
draw_string("Jouer ",205,45,bg2,bg3)
draw_string("Sortir",205,70,bg2,bg3)
grid()
def grid():
fill_rect(10,60,177,152,bg1)
pos = [10,35,60,85,110,135,160,185]
for p in pos:
fill_rect(p,60,2,152,lg)
fill_rect(10,p+50,177,2,lg)
def start():
mode = "menu"
while True:
if mode == "menu":
mode = mainTitle()
elif mode == "game":
mode = game()
else:
break
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment