Created
September 9, 2011 23:18
-
-
Save basicxman/1207581 to your computer and use it in GitHub Desktop.
Space Invaders graphics 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 math import floor | |
| from Tkinter import * | |
| from invaders import * | |
| class Invader(object): | |
| def __init__(self, cv, x, y, width, height, invader): | |
| self.cv = cv | |
| self.x = x | |
| self.y = y | |
| self.width = width | |
| self.height = height | |
| self.invader = invader | |
| self.colour = "#8888ff" | |
| self.state = 0 | |
| self.initialize_states() | |
| self.switch_state() | |
| def initialize_states(self): | |
| self.objs = [[], []] | |
| self.add_objects(0, "") | |
| self.add_objects(1, "_anim") | |
| def add_objects(self, state, suffix): | |
| for coord in Invaders.__dict__[self.invader + suffix]: | |
| cx = self.x + coord[1] * self.width | |
| cy = self.y + coord[0] * self.height | |
| self.objs[state].append(self.cv.create_rectangle(cx, cy, cx + self.width, cy + self.height, fill=self.colour, width="0")) | |
| self.cv.pack() | |
| def switch_state(self): | |
| self.hide_objs() | |
| self.state = int(not bool(self.state)) | |
| self.show_objs() | |
| self.timer = self.cv.after(1000, self.switch_state) | |
| def hide_objs(self): | |
| for obj in self.objs[self.state]: | |
| self.cv.itemconfig(obj, fill="") | |
| def show_objs(self): | |
| for obj in self.objs[self.state]: | |
| self.cv.itemconfig(obj, fill=self.colour) | |
| class App(object): | |
| def __init__(self): | |
| self.root = Tk() | |
| self.root.title("Scalable Graphics Test") | |
| self.root.minsize(144, 32) | |
| self.root.aspect(144, 32, 144, 32) | |
| self.cv = Canvas(self.root, width=576, height=128, bg="white", highlightthickness=0) | |
| self.cv.pack(expand=True, fill=BOTH) | |
| self.quantity = 12 | |
| self.choices = ["front_invader", "back_invader", "middle_invader"] | |
| self.rows = [] | |
| self.previous_width, self.previous_height = None, None | |
| self.root.bind("<Configure>", self.redraw) | |
| self.root.bind("q", lambda e: e.widget.quit()) | |
| self.root.mainloop() | |
| def redraw(self, event): | |
| w, h = event.width, event.height | |
| if w == self.previous_width and h == self.previous_height: | |
| return | |
| slot_width = w / self.quantity | |
| slot_height = h / len(self.choices) | |
| invader_width = floor(slot_width / 12) | |
| invader_height = floor(slot_height / 8) | |
| if len(self.rows) > 0: | |
| self.cv.delete(ALL) | |
| for row in range(0, len(self.rows)): | |
| y = row * slot_height | |
| for j in range(0, self.quantity): | |
| x = j * slot_width | |
| self.rows[row][j].width = invader_width | |
| self.rows[row][j].height = invader_height | |
| self.rows[row][j].x = x | |
| self.rows[row][j].y = y | |
| self.rows[row][j].initialize_states() | |
| return | |
| for i in range(0, len(self.choices)): | |
| y = i * slot_height | |
| self.rows.append([]) | |
| choice = self.choices[i] | |
| for j in range(0, self.quantity): | |
| x = j * slot_width | |
| self.rows[i].append(Invader(self.cv, x, y, invader_width, invader_height, choice)) | |
| App() |
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
| class Invaders(object): | |
| front_invader = [ (0,3), (0,4), (0,5), (0,6), (0,7), (0,8), (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (2,0), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (2,10), (2,11), (3,0), (3,1), (3,2), (3,5), (3,6), (3,9), (3,10), (3,11), (4,0), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (4,10), (4,11), (5,3), (5,4), (5,7), (5,8), (6,2), (6,3), (6,5), (6,6), (6,8), (6,9), (7,0), (7,1), (7,10), (7,11) ] | |
| front_invader_anim = [ (0,3), (0,4), (0,5), (0,6), (0,7), (0,8), (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (2,0), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (2,10), (2,11), (3,0), (3,1), (3,2), (3,5), (3,6), (3,9), (3,10), (3,11), (4,0), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (4,10), (4,11), (5,3), (5,4), (5,7), (5,8), (6,1), (6,2), (6,5), (6,6), (6,9), (6,10), (7,2), (7,3), (7,8), (7,9) ] | |
| middle_invader = [ (0,3), (0,9), (1,1), (1,4), (1,8), (1,11), (2,1), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (2,11), (3,1), (3,2), (3,3), (3,5), (3,6), (3,7), (3,9), (3,10), (3,11), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (4,10), (4,11), (5,2), (5,3), (5,4), (5,5), (5,6), (5,7), (5,8), (5,9), (5,10), (6,3), (6,9), (7,2), (7,10) ] | |
| middle_invader_anim = [ (0,3), (0,9), (1,4), (1,8), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (2,9), (3,2), (3,3), (3,5), (3,6), (3,7), (3,9), (3,10), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (4,10), (4,11), (5,1), (5,2), (5,3), (5,4), (5,5), (5,6), (5,7), (5,8), (5,9), (5,10), (5,11), (6,1), (6,3), (6,9), (6,11), (7,4), (7,5), (7,7), (7,8) ] | |
| back_invader = [ (0,5), (0,6), (1,4), (1,5), (1,6), (1,7), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (3,2), (3,3), (3,5), (3,6), (3,8), (3,9), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (5,4), (5,7), (6,3), (6,5), (6,6), (6,8), (7,2), (7,4), (7,7), (7,9) ] | |
| back_invader_anim = [ (0,5), (0,6), (1,4), (1,5), (1,6), (1,7), (2,3), (2,4), (2,5), (2,6), (2,7), (2,8), (3,2), (3,3), (3,5), (3,6), (3,8), (3,9), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7), (4,8), (4,9), (5,3), (5,5), (5,6), (5,8), (6,2), (6,9), (7,3), (7,8) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment