def __init__(self): self.name = "Rpg" self.map = Map("Rpg") self.players = [Player("Marc")] self.window = Window(self.name) self.scenes = [] self.map.generate()
def __init__(self): self.name="Test" self.map_size=[60,40] self.map=Map(self.map_size) self.window_size=[1400,800] self.fullscreen=False self.window=Window(self.name,self.window_size,self.fullscreen,text_color=RED) self.player=Player(borders=[0,0]+self.map.size)
def __init__(self): self.name = "Test" self.map_size = [100, 20] self.map = Map(self.map_size) self.window_size = [700, 600] self.fullscreen = False self.window = Window(self.name, self.window_size, self.fullscreen) self.player = Player()
def __init__(self, plane=Plane(), window=None, **kwargs): if window: self.window = window else: window = Window(**kwargs) self.plane = plane self.window = window self.window.text_size = 30
def __init__(self, name="Asteroid", window=None): self.name = name self.window = window if self.window == None: self.window = Window(self.name, build=False) self.window.build() self.plan = Plan(lines=False) self.levels = [Level("First level")] self.round = 0 self.over = False
def __init__(self, window=None, plane=None): """Create a draw object using the window and optional plane.""" if window: self.window = window else: self.window = Window() if plane: self.plane = plane else: self.plane = Plane(offset=[window.w / 2, window.h / 2], zoom=min(window.size) / 10)
def __init__(self, tictactoe, players, window=Window(name="TicTacToe", size=(500, 500)), restart_at_end=False, telling=True): """Create a game of TicTacToe using the TicTacToe object.""" self.tictactoe = tictactoe self.players = players self.window = window self.default_size = window.size self.restart_at_end = restart_at_end # =>Easier to manipulate than having nasty infinities to deal with self.choice = None self.on = False self.telling = telling self.start_duration = 1 self.end_duration = 1 self.start_time = None self.end_time = None self.game_number = 0
for entity in self.entities: entity.update(self.cursor, self.click, self.keys) def input(self): """Wait for user to insert input.""" change = False while not change: cursor = self.window.point() if cursor is not self.cursor: self.cursor = cursor change = True click = self.window.click() if click is not self.click: self.click = click change = True keys = self.window.press() if keys is not self.keys: self.keys = keys change = True def log(self, message): """Print message with game mention.""" output = str(type(self)) print(output) if __name__ == "__main__": game = Game("test", Window()) game() game.log("salut")
from mywindow import Window from mysurface import Surface from mydraw import Draw from math import sqrt, atan, pi, cos, sin, pi from cmath import polar from mycolors import * from myabstract import Point, Form, Vector import random import time if __name__ == "__main__": real_window = Window(size=[1440, 900], fullscreen=True) draw = Draw(window=real_window) window = Surface( draw) #Create not a real window but a surface to display on screen. #window=Window(fullscreen=True) p1 = Point(1, -6) p2 = Point(-2, 4) p3 = Point(8, 5) p4 = Point(4, 4, color=(0, 255, 0)) points = [p1, p3, p2, p4] f = Form([ Point(random.randint(-10, 10), random.randint(-10, 10)) for i in range(10) ]) #f.show(window) f2 = f.getSparse() p1.show(window)
return rect def getRectFromCorners(corners): """Return the rect (top_left_corner,size) using the corners (top_left_corner,bottom_right_corner).""" """[mx,my,Mx,My] -> [mx,my,sx,sy]""" mx, my, Mx, My = corners sx, sy = Mx - mx, My - my rect = [mx, my, sx, sy] return rect def getCornersFromRect(rect): """Return the (top_left_corner,bottom_right_corner) using the corners rect (top_left_corner,size).""" """[mx,my,Mx,My] -> [mx,my,sx,sy]""" mx, my, sx, sy = rect Mx, My = mx + sx, my + sy corners = [mx, my, Mx, My] return corners if __name__ == "__main__": window = Window(fullscreen=False) theme = { "background": (0, 0, 0), "show scale": True, "show origin": True, "grid color": (200, 200, 200) } plane = Plane(theme) #plane.units=[1/10**300,1/10**300] plane(window)
def __call__(self): """Boucle 'for' principale du simulateur.""" for i in range(self.nombre_parties): if self.fenetre: jeu = Othello(self.joueurs, self.fenetre) else: jeu = Othello(self.joueurs) jeu() if not jeu.fenetre.open: break self.gagnants.append(jeu.gagnant) if self.display: print(self) def __repr__(self): """Renvoie une représentation des victoires de chaque joueur avec l'historice des victoires du simulateur.""" message = "Resultats de " + str(len(self.gagnants)) + " parties:\n" for numeror in range(len(self.joueurs)): message += "- Joueur " + str(numero) + " a gagne " + str( self.gagnants.count(numero)) + " fois.\n" return message if __name__ == "__main__": fenetre = Window(taille=[800, 800], fullscreen=False) joueurs = [IA(), BruteForce(3)] nombre_parties = 50 affichage = True simulation = Simulateur(fenetre, joueurs, nombre_parties) simulation() print(simulation)
def __init__(self,plane=Plane(),window=Window()): self.plane=plane self.window=window
def __init__(self, name="Lab"): """Creates lab using name.""" self.name = name self.entities = [] self.window = Window(self.name, set=False)
from marclib.polynomial import Polynomial from mygrapher import Grapher from mywindow import Window import functools import math prod = lambda l: functools.reduce(lambda a, b: a * b, l) #p=Polynomial([2,1]) sample = lambda f, xmin, xmax, n: [ f(xmin + (xmax - xmin) * i / n) for i in range(n) ] interpolator = lambda x, s: sum([ s[j] * prod([(len(s) * x - i) / (j - i) for i in range(len(s)) if i != j]) for j in range(len(s)) ]) s = sample(math.cos, -10, 10, 100) print(s) cosinus_interpolation = lambda x: interpolator(x, s) print(cosinus_interpolation(0)) #print(sample(math.cos,-10,10,100)) window = Window(size=[1000, 800], fullscreen=False) fs = [math.cos, cosinus_interpolation] grapher = Grapher(fs) grapher(window)
def __init__(self, name="Lab", entities=[]): """Creates lab using name.""" self.name = name self.entities = entities self.loop = [] self.window = Window(self.name)
pass self.grid = [[Component() for x in range(sx)] for y in range(sy)] def fill(self, position, value): #This method is totally useless """Fill the position with the given value.""" x, y = position self.grid[y][x] = value def showPlane(self, window): """Show the map on the window.""" self.showGrid(window) self.showCases(window) self.showBorders(window) def showCases(self, window): """Show all the components on screen.""" sx, sy = self.size nx = sx // 2 ny = sy // 2 for y in range(sy): for x in range(sx): position = self.getToScreen([x - nx, y - ny + 1], window) self.grid[y][x].show(window, position, self.units) if __name__ == "__main__": window = Window("Map") map = Map() print(map.grid) map(window)
def __call__(self): """Return all coordonnates of the circle.""" return [self.x,self.y,self.radius] def crossCircle(self,other): """Determine if two circles are crossing.""" vector=Vector.createFromTwoPoints(self.center,other.center) return vector.norm<self.radius+other.radius if __name__=="__main__": from mysurface import Surface from myzone import Zone from mywindow import Window surface=Surface(plane=Zone(),window=Window()) c1=Circle(4,2,2,color=mycolors.BLUE) c2=Circle(1,2,1,color=mycolors.BLUE) corners=[-1,-1,1,1] cs=[Circle.random(corners,color=mycolors.random()) for i in range(10)] fill=False c1_border_color=mycolors.random() c2_border_color=mycolors.random() c1_area_color=mycolors.random() c2_area_color=mycolors.random() while surface.open: surface.check() surface.control()
def norm(self): """Return the angle of a vector with the [1,0] direction in cartesian coordonnates.""" return Vector.polar([self.x, self.y])[0] def __xor__(self, other): """Return the angle between two vectors.""" return self.angle() - other.angle() def __invert__(self): """Return the unit vector.""" n, a = self.polar() if __name__ == "__main__": window = Window("Vector") p1 = Point(50, 100) p2 = Point(500, 400) p3 = Point(300, 200) v1 = Vector(p1, p2) v2 = 0.8 * v1 p4 = v2(p1) v2.color = (255, 0, 0) p4.color = (0, 255, 0) v1.show(p1, window) v2 %= 0.3 v2.show(p1, window) p2.show(window) p4.show(window) window()
Pour marc :9 Indiquer dans la fenetre à qui c'est le tour La fonction board.conquerir doit renvoyer le nombre de pion retourne La deco inutile à faire : Mini animation pour indiquer le dernier pion posé : colorer ses rebord de la couleur NEW_COLOR_PIECES, à ajouter dans les constantes Ajouter les 4 points noirs sur le plateau. Ajouter un menu très basique=>nouvelle class est nouveau fichier .py Pour dossier : expliquer demarche dans cahier de bord Faire mini schema des heritage de classe """ if __name__=="__main__": #Ceci est exécuté uniquement si le fichier est exécuté directement et non depuis un autre fichier. fenetre=Window(taille=[800,800],set=False,fullscreen=False) #Crée une fenêtre. developpeur1=Developpeur() developpeur2=Developpeur() humain=Humain() #Crée un humain. machine1=ia.IA() #Crée une intelligence artificielle. machine2=ia2.IA() machine3=ia2.IA() bruteforce=BruteForce(level=3) #Crée une machine utilisant la force de calcul de la machine, cela est utile pour les tests de niveau des nouvelles intelligences artificielles. jeu=Othello(joueurs=[humain,bruteforce],fenetre=fenetre) #Crée un jeu. jeu() #Lance le jeu.
self.over = False def __call__(self): while self.window.open and not self.over: self.window.check() self.update() self.show() def update(self): level = self.levels[self.round] if level.over: self.round += 1 else: level() entities = self.levels[self.level].entities player = level.control() for entity in entities: entity.update(entities) def show(self): self.window.clear() for entity in self.levels[self.round].entities: entity.show(self.window, self.plan) self.window.flip() if __name__ == "__main__": window = Window(build=False) game = Game(window=window) game()
from mywindow import Window from mycolors import WHITE, BLACK from Othello import Othello window = Window(size=[800, 800], set=False) window.open = True window.text_color = BLACK window.background_color = BLACK window.text_size = 30 #window.size=[900,20] results = [] i = 0 number = 10000 display = False while i < number and window.open: i += 1 game = Othello(window, display) game() winner_side = (game.state + 1) % 2 results.append(winner_side) message = "Results for " + str( i) + " tests:\n" + "White victories: " + str( results.count(0)) + "\n" + "Black victories: " + str( results.count(1)) + "\n" window.print(str(message), size=[50, 20]) window.flip() print(message)
form = (form + 1) % 4 print(form) if keys[K_RETURN]: self.clear() self.flip() def oldTrace(self, position, color=WHITE, radius=5): """Trace a point on the screen using position, size, color.""" pygame.draw.circle(self.screen, color, position, radius, 0) #print("position: ",position) def trace(self, position, size, color=WHITE, radius=5, form=0, width=0): """Trace a point on the screen using position, size, color.""" for tool in self.hand: tool.size = size tool.position = position tool.points.append(position) tool.color = color tool.radius = radius tool.connect = False tool.form = form tool.width = width tool.draw(self.screen) #print("position: ",position) if __name__ == "__main__": window = Window("Paint", build=False) paint = Paint() paint(window)
from mypoint import Point from myform import Form from myvector import Vector from mywindow import Window from random import randint window=Window() wsx,wsy=window.size X=wsx//4 Y=wsy//4 posAlea=lambda :[randint(X,3*X),randint(Y,3*Y)] f1=Form([Point(*posAlea()) for i in range(5)]) f1=f1.getSparse() f2=Form([Point(*posAlea()) for i in range(5)]) f2=f2.getSparse() #f1.points[0].color=(255,0,0) #f1.points[1].color=(255,0,0) while window.open: window.check() window.clear() f1.rotate(0.01,f1[0]) #f1.rotate(-0.01) f2.rotate(0.02,f1[1]) f2.rotate(-0.03) f1.show(window)
x, y, sx, sy = self.center(window, self.default_size) self.position = (x, y) self.size = (sx, sy) def center(self, window): """Center object by default.""" wsx, wsy = window.size wcx, wcy, wcsx, wcsy = window.coordonnates hx, hy = (wcsx - wcx, wcsy - wcy) return (hx, hy, wsx, wsy) def createButtons(self, names): """Create button entities for pannel.""" for i in range(len(names)): self.buttons.append(Button(names[i])) def checkButtons(self): """Return buttons states.""" states = [] for button in self.buttons: states.append(button()) return states if __name__ == "__main__": w = Window("Testing pannel") p = Panel(w) p.show(w, [100, 100, 400, 400], BLUE, GREEN) w.flip() w.pause()
"""Create a new form according screen coordonnates using a form and the window.""" points = [ Point(self.getToScreen(point, window)) for point in plane_form ] screen_form = deepcopy(plane_form) screen_form.points = points return screen_form def event(self, window): """Handle window events specifics to the maker.""" keys = window.press() click = window.click() cursor = window.point() if click: if not self.selection: self.startForm(cursor, window) else: self.changeForm(cursor, window) if keys[K_SPACE]: self.endForm() def select(self, window): """Still not clear what it does. (it means its useless)""" pass if __name__ == "__main__": window = Window(fullscreen=True) maker = Maker() maker(window)
button_key=self.checkButtons() if button_key is not None: self.tree[self.key] def checkButtons(self): """Check if the buttons are being clicked.""" for i in range(len(self.page.buttons)): if self.page.buttons[i].clicked: return i key_action=self.tree[self.key] action=self.actions[key_action] action() def getPage(self): """Return the current page.""" return self.pages[self.key] page=property(getPage) if __name__=="__main__": from mywindow import Window window=Window("Page") buttons=[Button(t) for t in ["oui","non","peut etre","pourquoi pas"]] page=Page(window.size,buttons) page(window)
def lines(self,color,positions,connected=True,width=1): new_positions=self.plane.getAllToScreen(positions,self.window) self.window.draw.lines(screen,color,connected,positions,width) def show(self): self.plane.showGrid(self.window) self.window.flip() def clear(self): self.plane.clear(self.window) def check(self): self.window.check() def control(self): self.plane.control(self.window) if __name__=="__main__": window=Window("mydraw") plane=Plane() draw=Draw(plane,window) while draw.window.open: draw.check() draw.control() draw.clear() draw.line(WHITE,(3,-5),(-2,6)) draw.show()
def jouer(self, plateau, fenetre, tour): input = np.array(plateau.grille) predictions = self.model.predict([input]) #print(predictions) raw_choice = predictions.argmax() choice = [ raw_choice // plateau.taille[0], raw_choice % plateau.taille[0] ] #print("Network choice:",choice) if choice in plateau.mouvements: pass #print("Valid") else: #print("Random") choice = random.choice(plateau.mouvements) #print("Final choice: ",choice) #print("") return choice if __name__ == "__main__": window = Window(taille=[800, 800], set=False) players = [Beast(2), NeuralNetwork()] training = Trainer(window, players, affichage=True) training(5) window.set() print(training.players[1].training_data) game = Othello(window, [NeuralNetwork(), Robot(1)]) #game=Othello(window,[Humain(),training.players[1]],affichage=True) game()
class Segment: def __init__(self, p1, p2, thickness=1, color=mycolors.RED): """Create a Segment using its arguments: - tuples p1, p2 : points of the segment extremities - int thickness : thickness of the segment (1px by default) - bool show : display the segment""" self.p1 = list(p1) self.p2 = list(p2) self.thickness = thickness self.color = color def show(self, window): """Show the segment on the window.""" window.draw.line(window.screen, self.color, self.p1, self.p2, self.thickness) if __name__ == "__main__": from mywindow import Window w = Window(size=[600, 400], call=True) w.clear() c = Circle(center=[300, 200], radius=50) s = Segment(p1=[100, 100], p2=[500, 300], color=mycolors.RED) w.clear(color=mycolors.WHITE) while w.open: w.check() c.show(w) s.show(w) w.flip()
from mywindow import Window class Circle: def __init__(self, x, y, radius, color=(255, 255, 255), width=1): """Create a circle object using x, y and radius and optional color and width.""" self.x = x self.y = y self.radius = radius self.color = color self.width = width def show(self, window, color=None, width=None): """Show the circle on screen using the window.""" if not color: color = self.color if not width: width = self.width window.draw.circle(window.screen, color, [self.x, self.y], self.radius, width) def __call__(self): """Return all coordonnates of the circle.""" return [self.x, self.y, self.radius] if __name__ == "__main__": window = Window("Circles") c = Circle(400, 200, 20, (255, 0, 0)) c.show(window) window()