Last active
August 29, 2015 13:57
-
-
Save fitzterra/9492880 to your computer and use it in GitHub Desktop.
Pythonista ball moving on grid test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from scene import * | |
| from random import randrange | |
| from maze import Maze | |
| class MyScene (Scene): | |
| def __init__(self, rows=5, cols=5): | |
| """ | |
| @param rows: Number of rows in maze | |
| @param cols: Number of columns in maze | |
| """ | |
| self.rows = rows | |
| self.cols = cols | |
| self.margin = 5 # margin around maze | |
| def setup(self): | |
| """ | |
| Scene setup method | |
| """ | |
| # Get the full usable area minus margin on both sides | |
| x,y = [s-self.margin*2 for s in self.size.as_tuple()] | |
| # Cell size is the smallest of x/cols and y/rows ratio | |
| self.cellSize = int(min(x/self.cols, y/self.rows)) | |
| # Disc size is a third of the cell size | |
| self.discSize = int(self.cellSize/3) | |
| # Generate the maze | |
| self.maze = Maze(rows, cols) | |
| # randomly select cell for disc | |
| dx,dy = (randrange(self.cols), randrange(self.rows)) | |
| # calc disc coords for this cell | |
| offs = self.cellSize/2-self.discSize/2 | |
| dx = dx*self.cellSize + offs | |
| dy = dy*self.cellSize + offs | |
| self.disc = (dx, dy) | |
| def drawMaze(self): | |
| """ | |
| Draws the maze in self.maze | |
| """ | |
| pass | |
| def drawGrid(self): | |
| stroke(1, 1, 1) | |
| stroke_weight(1) | |
| # first the columns. | |
| # the column start is at 0 and the height is the number | |
| # of rows * cellSize | |
| y1,y2 = 0,self.rows*self.cellSize | |
| for c in range(self.cols): | |
| # the column line pos is the column * cellSize | |
| x = c*self.cellSize | |
| # draw the west line | |
| line(x+self.margin, y1+self.margin, x+self.margin, y2+self.margin) | |
| # set x for the east line | |
| x += self.cellSize-1 | |
| # draw the east line | |
| line(x+self.margin, y1+self.margin, x+self.margin, y2+self.margin) | |
| # now the same for the rows | |
| x1,x2 = 0,self.cols*self.cellSize | |
| for r in range(self.rows): | |
| y = r*self.cellSize | |
| line(x1+self.margin, y+self.margin, x2+self.margin, y+self.margin) | |
| y += self.cellSize-1 | |
| line(x1+self.margin, y+self.margin, x2+self.margin, y+self.margin) | |
| def XYtoCell(self, x, y): | |
| ''' | |
| determine in which cell the supplied x,y coords are | |
| located. | |
| ''' | |
| # remove margin from touch coords | |
| x,y = (x-self.margin, y-self.margin) | |
| # find the row | |
| r = int(y/self.cellSize) | |
| # find thhe column | |
| c = int(x/self.cellSize) | |
| return (c, r) | |
| def cellRect(self, c, r): | |
| ''' | |
| returns a Rect() object definining the cell in | |
| column c, row r | |
| ''' | |
| x = c*self.cellSize+self.margin | |
| y = r*self.cellSize+self.margin | |
| return Rect(x, y, self.cellSize, self.cellSize) | |
| def showTouch(self): | |
| m = 'x: %s px: %s\ny: %s py: %s' | |
| x,y,px,py = '?', '?', '?', '?' | |
| if self.touches: | |
| t = self.touches.values()[0] | |
| x,y = t.location.as_tuple() | |
| inCell = self.XYtoCell(x,y) | |
| text(str(inCell), x=x+50, y=y+50, alignment=1) | |
| px,py = t.prev_location.as_tuple() | |
| text(m % (x,px,y,py), x=0, y=self.size.h, alignment=3) | |
| def showInfo(self): | |
| m = 'marg: %s\ncellSize: %s' %(self.margin, self.cellSize) | |
| text(m, x=self.size.w/2, y=self.size.h, alignment=2) | |
| def drawDisc(self): | |
| fill(0.5, 0.5, 0.5) | |
| x,y = self.disc | |
| if self.touches: | |
| t = self.touches.values()[0] | |
| if t.location in Rect(x,y,self.discSize,self.discSize): | |
| x = t.location.x-self.discSize/2 | |
| y = t.location.y-self.discSize/2 | |
| self.disc = (x,y) | |
| inCell = self.XYtoCell(x,y) | |
| cr = self.cellRect(*inCell) | |
| fill(0,1,0,0.5) | |
| rect(cr.x, cr.y, cr.w, cr.h) | |
| fill(1,0,0) | |
| ellipse(x,y,self.discSize,self.discSize) | |
| def draw(self): | |
| background(0, 0, 0) | |
| self.drawGrid() | |
| self.drawDisc() | |
| self.showInfo() | |
| self.showTouch() | |
| run(MyScene(3, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment