Skip to content

Instantly share code, notes, and snippets.

@TheBonnec
Last active May 10, 2021 18:36
Show Gist options
  • Save TheBonnec/ef1808b883cc36edfc49316d02ae257d to your computer and use it in GitHub Desktop.
Save TheBonnec/ef1808b883cc36edfc49316d02ae257d to your computer and use it in GitHub Desktop.
Pong
# Version 1.0.4
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(col):
self.color = col
def zte(self):
o = self.oldPos
dx, dy = rInt(self.x) - rInt(o[0]), rInt(self.y) - rInt(o[1])
if dx >= self.width or dy >= self.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
def updateOldPos(self):
s = self
self.oldPos = (s.x,s.y,s.width,s.height)
class GEView:
def __init__(self, blocks, bg, n):
self.blocks = blocks
self.background = bg
self.nOldPos = n
self.OLDPOS = [([], (255,255,255))]
def addBlock(self,block,pos):
self.blocks.insert(pos,block)
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):
self.__updateOldPos__()
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):
self.__updateOldPos__()
for b in self.blocks:
fill_rect(rInt(b.x), rInt(b.y), rInt(b.width), rInt(b.height), b.color)
zte = b.zte()
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 __updateOldPos__(self):
if len(self.OLDPOS) >= self.nOldPos:
self.OLDPOS.pop(0)
self.OLDPOS.append((self.blocks, self.background))
# Other functions
def rInt(i):
return int(round(i,0))
#Version Alpha 1.0
#Fonctionne avec Games Engine 1.0.3
from ion import *
from math import *
from time import *
from random import *
from kandinsky import *
from games_engine import *
global gameMode, bg1, bg2, bg3, bl, gr, wh, ye
bg1, bg2, bg3 = (46,46,40), (77,75,70), (112,112,102)
bl = (110,150,165)
gr = (110,205,130)
wh = (255,255,230)
ye = (255,230,70)
''' ----- Menu ----- '''
def menuView():
drawMainMenu()
s = selectionView(201,38,109,174,
[["Jouer",46,None,"OK",True],
["Sortir",71,None,"OK",True]],
[["v alpha 1",209,165,bg2],
["@TBonnec",209,186,bg2]])
if s[0] == 0:
return "game"
elif s[0] == 1:
return "quit"
def drawMainMenu():
fill_rect(0,0,320,222,bg1)
draw_string("PONG",141,10,wh,bg1)
fill_rect(10,70,10,70,bl)
fill_rect(181,110,10,70,gr)
fill_rect(95,120,10,10,wh)
''' ----- Tools ----- '''
#View to select a setting or a page
def selectionView(x,y,w,h,menus,txts):
px, py, pw, ph = x+3, y+3, w-6, h-6
#txts : [(txt, x,y, col)]
#menus : [(title: str, y: int, switch: [str,str,..., startID], slctr: str, okable: Bool)]
cursor = 0
colors = [ye]
switch = {} #switch : {menuID: switchPos}
for m in range(len(menus)):
colors.append(wh)
if menus[m][2] != None:
switch[m] = menus[m][2][-1]
fill_rect(x,y,w,h,bg2)
def updateView():
fill_rect(px,py,pw,ph,bg3)
# Settings
for m in range(len(menus)):
s = menus[m][0]
if menus[m][2] != None:
s += " " + menus[m][2][switch[m]]
draw_string(s,x+8,menus[m][1],colors[m],bg3)
if cursor == m and menus[m][3] != None:
draw_string(menus[m][3], (x+w-len(menus[m][3])*10-8), menus[m][1],ye,bg3)
# Texts
if txts != None:
for t in txts:
draw_string(t[0],t[1],t[2],t[3],bg3)
updateView()
while True:
if keydown(KEY_DOWN):
colors[cursor] = wh
cursor = min(cursor+1, len(menus)-1)
colors[cursor] = ye
updateView()
while keydown(KEY_DOWN):
sleep(0.01)
elif keydown(KEY_UP):
colors[cursor] = wh
cursor = max(cursor-1, 0)
colors[cursor] = ye
updateView()
while keydown(KEY_UP):
sleep(0.01)
elif keydown(KEY_LEFT):
if menus[cursor][2] != None:
switch[cursor] = max(switch[cursor]-1,0)
updateView()
while keydown(KEY_LEFT):
sleep(0.01)
elif keydown(KEY_RIGHT):
if menus[cursor][2] != None:
switch[cursor] = min(switch[cursor]+1,len(menus[cursor][2])-2)
updateView()
while keydown(KEY_RIGHT):
sleep(0.01)
elif keydown(KEY_OK) or keydown(KEY_EXE):
while keydown(KEY_OK) or keydown(KEY_EXE):
sleep(0.01)
if menus[cursor][-1]:
r = [cursor]
for i in switch.items():
r.append(i)
return r #return : (cursor: int, switchPos: (switchTitle, switchPos))
''' ----- Game ----- '''
global ballSpeed, ballXAngle, ballYAngle, points
def game():
global ballSpeed, ballXAngle, ballYAngle, points
resetGameValues()
view = GEView([GEBlock("p1",10,76,10,70,bl),
GEBlock("p2",300,76,10,70,gr),
GEBlock("ball",155,106,10,10,wh)],
bg1, 1)
view.hardReload()
while True:
if keydown(KEY_FOUR):
view.blocks[0].move(0,-4)
elif keydown(KEY_ZERO):
view.blocks[0].move(0,4)
if keydown(KEY_RIGHTPARENTHESIS):
view.blocks[1].move(0,-4)
elif keydown(KEY_MINUS):
view.blocks[1].move(0,4)
view.blocks[2].move(ballXAngle * ballSpeed, ballYAngle * ballSpeed)
hitTest(view)
view.reload()
draw_string(str(points[0])+" : "+str(points[1]),135,10,wh,bg1)
sleep(0.01)
def resetGameValues(bs=4, bxa=1, bya=0, p=[0,0]):
global ballSpeed, ballXAngle, ballYAngle, points
ballSpeed = bs
ballXAngle = bxa
ballYAngle = bya
points = p
def hitTest(view):
global ballSpeed, ballXAngle, ballYAngle, points
ball = view.blocks[2]
bar1 = view.blocks[0]
bar2 = view.blocks[1]
#Test borders
if ball.y <= 2 or ball.y >= 210 :
ballYAngle = -ballYAngle
if ball.x <= 22:
if ball.y >= bar1.y - 10 and ball.y <= bar1.y + bar1.height:
ballYAngle = uniform(-0.9,0.9)
ballXAngle = abs(sin(acos(ballYAngle)))
else:
ballXAngle = -ballXAngle
points[1] += 1
ballSpeed += 0.05
elif ball.x >= 288:
if ball.y >= bar2.y - 10 and ball.y <= bar2.y + bar2.height:
ballYAngle = uniform(-0.9,0.9)
ballXAngle = -abs(sin(acos(ballYAngle)))
else:
ballXAngle = -ballXAngle
points[0] += 1
ballSpeed = min(ballSpeed + 0.1, 10)
''' ----- Launch Game ----- '''
gameMode = "menu"
while True:
if gameMode == "menu":
gameMode = menuView()
elif gameMode == "game":
gameMode = game()
else:
break
#Version Alpha 1.0
#Fonctionne avec Games Engine 1.0.3
from ion import *
from math import *
from time import *
from kandinsky import *
from games_engine import *
global gameMode, bg1, bg2, bg3, bl, gr, wh, ye
bg1 = (40,40,40)
bg2 = (67,70,80)
bg3 = (95,100,120)
bl = (90,120,245)
gr = (90,245,120)
wh = (255,255,255)
ye = (255,230,70)
''' ----- Menu ----- '''
def menuView():
drawMainMenu()
s = selectionView(201,38,109,174,
[["Jouer",46,None,"OK",True],
["Sortir",71,None,"OK",True]],
[["v alpha 1",209,165,bg2],
["@TBonnec",209,186,bg2]])
if s[0] == 0:
return "game"
elif s[0] == 1:
return "quit"
def drawMainMenu():
fill_rect(0,0,320,222,bg1)
draw_string("PONG",141,10,wh,bg1)
fill_rect(10,70,10,70,bl)
fill_rect(181,110,10,70,gr)
fill_rect(95,120,10,10,wh)
''' ----- Tools ----- '''
#View to select a setting or a page
def selectionView(x,y,w,h,menus,txts):
px, py, pw, ph = x+3, y+3, w-6, h-6
#txts : [(txt, x,y, col)]
#menus : [(title: str, y: int, switch: [str,str,..., startID], slctr: str, okable: Bool)]
cursor = 0
colors = [ye]
switch = {} #switch : {menuID: switchPos}
for m in range(len(menus)):
colors.append(wh)
if menus[m][2] != None:
switch[m] = menus[m][2][-1]
fill_rect(x,y,w,h,bg2)
def updateView():
fill_rect(px,py,pw,ph,bg3)
# Settings
for m in range(len(menus)):
s = menus[m][0]
if menus[m][2] != None:
s += " " + menus[m][2][switch[m]]
draw_string(s,x+8,menus[m][1],colors[m],bg3)
if cursor == m and menus[m][3] != None:
draw_string(menus[m][3], (x+w-len(menus[m][3])*10-8), menus[m][1],ye,bg3)
# Texts
if txts != None:
for t in txts:
draw_string(t[0],t[1],t[2],t[3],bg3)
updateView()
while True:
if keydown(KEY_DOWN):
colors[cursor] = wh
cursor = min(cursor+1, len(menus)-1)
colors[cursor] = ye
updateView()
while keydown(KEY_DOWN):
sleep(0.01)
elif keydown(KEY_UP):
colors[cursor] = wh
cursor = max(cursor-1, 0)
colors[cursor] = ye
updateView()
while keydown(KEY_UP):
sleep(0.01)
elif keydown(KEY_LEFT):
if menus[cursor][2] != None:
switch[cursor] = max(switch[cursor]-1,0)
updateView()
while keydown(KEY_LEFT):
sleep(0.01)
elif keydown(KEY_RIGHT):
if menus[cursor][2] != None:
switch[cursor] = min(switch[cursor]+1,len(menus[cursor][2])-2)
updateView()
while keydown(KEY_RIGHT):
sleep(0.01)
elif keydown(KEY_OK) or keydown(KEY_EXE):
while keydown(KEY_OK) or keydown(KEY_EXE):
sleep(0.01)
if menus[cursor][-1]:
r = [cursor]
for i in switch.items():
r.append(i)
return r #return : (cursor: int, switchPos: (switchTitle, switchPos))
''' ----- Game ----- '''
def game():
ballXAngle = cos(pi/4)
ballYAngle = sin(pi/4)
ballSpeed = 3
points = [0,0]
view = GEView([GEBlock("p1",10,76,10,70,bl),
GEBlock("p2",300,76,10,70,gr),
GEBlock("ball",155,106,10,10,wh)],
bg1, 1)
view.hardReload()
while True:
if keydown(KEY_FOUR):
view.blocks[0].move(0,-4)
elif keydown(KEY_ZERO):
view.blocks[0].move(0,4)
elif keydown(KEY_RIGHTPARENTHESIS):
view.blocks[1].move(0,-4)
elif keydown(KEY_MINUS):
view.blocks[1].move(0,4)
ball = view.blocks[2]
bar1 = view.blocks[0]
bar2 = view.blocks[1]
#Test borders
if ball.y <= 2 or ball.y >= 210 :
ballYAngle = -ballYAngle
elif ball.x <= 22:
if ball.y >= bar1.y - 10 and ball.y <= bar1.y + bar1.height:
ballXAngle = -ballXAngle
elif ball.x >= 288:
if ball.y >= bar2.y - 10 and ball.y <= bar2.y + bar2.height:
ballXAngle = -ballXAngle
view.blocks[2].move(ballXAngle * ballSpeed, ballYAngle * ballSpeed)
view.reload()
sleep(0.01)
def hitTest(view):
ball = view.blocks[2]
bar1 = view.blocks[0]
bar2 = view.blocks[1]
#Test borders
if ball.y <= 0 or ball.y >= 212 :
ballYAngle = -ballYAngle
''' ----- Launch Game ----- '''
gameMode = "menu"
while True:
if gameMode == "menu":
gameMode = menuView()
elif gameMode == "game":
gameMode = game()
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment