-
Notifications
You must be signed in to change notification settings - Fork 83
Home
ArtyProg edited this page Jun 20, 2016
·
12 revisions
Using Hexi in Python
The purpose of this wiki is to introduce kids to game programming,
using Python as main language.
To translate this script in Javascript :
- Install Python 3.x
- Install Transcrypt : pip install transcrypt from command line
- Translate the script : transcrypt your_script.py from commande line
This script is the base for a memory game.
It's by no mean complete. For now, it allows the comparison
between two cells content
import random
# The cells content will be filled with the following values
# Le contenu des cellules seront remplies avec les valeurs suivantes
choices = ["A", "B", "C", "D"]
# Mouse manager
# Gestion de la souris
class Mouse:
def __init__(self, game):
self.pointer = game.pointer
self.pointer.tap = self.tap
self.tapped = False
def tap(self):
self.tapped = True
# Main Grid wich contents all the cells
# Grille principale contenant les cellules
class Grid:
def __init__(self, game, rows=8, cols=8):
self.offset = 4
self.game = game
self.rows = rows
self.cols = rows
self.ligs = [[0 for i in range(self.rows)] for j in range(self.cols)]
def display(self):
num = 1
for i in range(self.cols):
for j in range(self.rows):
rect = self.game.rectangle(48, 48, "blue")
rect.x = j *(48 + self.offset )
rect.y = i * (48 + self.offset )
rect.num = num
rect.content = random.choice(choices)
self.ligs[i][j] = rect
num += 1
# Game Handler
# Gestion du Jeu
class Memory:
def __init__(self, width=512, height=512):
self.game = hexi(width, height, self.setup)
self.game.backgroundColor = "black"
self.mouse = Mouse(self.game).pointer
self.grid = Grid(self.game)
self.curcell = None
self.counter = -1
self.comparator = []
def setup(self):
self.game.scaleToWindow("seaGreen")
self.grid.display()
self.game.state = self.play
def get_curcell(self):
for i in range(self.grid.rows):
for j in range(self.grid.cols):
curcell = self.grid.ligs[i][j]
if(self.game.hit(self.game.pointer, curcell)):
self.curcell = curcell
def inc_counter(self):
self.counter += 1
if self.counter > 1:
self.counter = 0
self.comparator = []
def check_comparator(self):
cella, cellb = self.comparator[0], self.comparator[1]
if len(self.comparator) > 1:
if (cella.num != cellb.num ):
if(cella.content == cellb.content ):
cella.fillStyle = "#ff0000"
cellb.fillStyle = "#ff0000"
def play(self):
self.get_curcell()
if (self.mouse.tapped):
self.inc_counter()
self.comparator[self.counter] = self.curcell
self.mouse.tapped = False
self.check_comparator()
def start(self):
self.game.start()
memory = Memory()
memory.start()