def draw(self): Entity.draw(self) if self.world.debug_mode: pt = self.world.camera.offset(self.pos) pygame.draw.circle(self.world.screen, COLOR_WHITE, (int(pt.x), int(pt.y)), int(BODY_RADIUS * self.world.camera.zoom)) detection_length = self.get_detection_box_length() pygame.draw.circle(self.world.screen, COLOR_BLACK, (int(pt.x), int(pt.y)), int(detection_length * self.world.camera.zoom), 1) pt2 = pt + self.heading * detection_length * self.world.camera.zoom pygame.draw.line(self.world.screen, COLOR_BLACK, (int(pt.x), int(pt.y)), (int(pt2.x), int(pt2.y)))
def draw(self): #for b in self.bullets: # b.draw() if self.alive: Entity.draw(self)
def draw(self, surface): Entity.draw(self)
class Snake: animation = Animation(["⚁", "⚂", "⚃", "⚄", "⚅"], [0.5, 0.2, 0.1, 0.1, 0.1]) def __init__(self, stdsrc): self.stdsrc = stdsrc self.head = Entity(1, 1, self.animation, self.stdsrc) self.parts = [] self.currentDir = KEY_UP self.grow(5) def grow(self, n: int): x, y = (self.parts[0].x, self.parts[0].y) if self.parts else (0, 0) self.parts.extend( [Entity(x, y, self.animation, self.stdsrc) for i in range(n)]) def draw(self): self.head.draw(A_BOLD) for part in self.parts: part.draw(A_BOLD) def update(self): isDir = lambda c: 258 <= c <= 261 c = self.stdsrc.getch() if isDir(c) and not self._areOpposite(c, self.currentDir): self._move(c) self.currentDir = c else: self._move(self.currentDir) def _move(self, direction: int): self._moveParts() self._moveHead(direction) self._fixBorders() def _moveParts(self): i = len(self.parts) - 1 while i > 0: self.parts[i].x = self.parts[i - 1].x self.parts[i].y = self.parts[i - 1].y i -= 1 self.parts[0].x = self.head.x self.parts[0].y = self.head.y def _moveHead(self, direction: int): if direction == KEY_UP: self.head.y -= 1 elif direction == KEY_DOWN: self.head.y += 1 elif direction == KEY_RIGHT: self.head.x += 1 elif direction == KEY_LEFT: self.head.x -= 1 def _fixBorders(self): maxy, maxx = self.stdsrc.getmaxyx() if self.head.x >= maxx: self.head.x = 0 elif self.head.x < 0: self.head.x = maxx - 1 if self.head.y >= maxy: self.head.y = 0 elif self.head.y < 0: self.head.y = maxy - 1 def _areOpposite(self, dir1: int, dir2: int) -> bool: return (dir1 == KEY_UP and dir2 == KEY_DOWN) or\ (dir1 == KEY_DOWN and dir2 == KEY_UP) or\ (dir1 == KEY_LEFT and dir2 == KEY_RIGHT) or\ (dir1 == KEY_RIGHT and dir2 == KEY_LEFT) def hasEaten(self, food) -> bool: return self.head.contains(food) def isAlive(self) -> bool: ''' check if the snake eat part of its body ''' for part in self.parts: if self.head.contains(part): return False return True
def run_game(self): player = Player("Player", 20, SCREEN_HEIGHT / 2 - 25, "./assets/player.png", 50, 50) direction = (0, 0) max_mob_speed = self.cur_wins**2 + 10 num_enemies = int(1 + self.cur_wins / 5) enemies = [] for i in range(num_enemies): # first_x = 20 * 2 + 50 # last_x = SCREEN_WIDTH - first_x - 50 x_pos = int(((SCREEN_WIDTH - 20 * 2 - 50 * 2) / (num_enemies + 1)) * (i + 1)) y_pos = randint(20, SCREEN_HEIGHT - 20 - 50) mob_speed = randint(10 + self.cur_wins, max_mob_speed) enemies.append( NPC("Squiggles" + str(i), x_pos, y_pos, "./assets/enemy.png", 50, 50, start_speed=mob_speed)) treasure = Entity("Treasure", SCREEN_WIDTH - 20 - 50, SCREEN_HEIGHT / 2 - 25, "./assets/treasure.png", 50, 50) while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False # Can definitely do prettier control handling. elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.running = False break # wow 2 whole directions, golly gee. elif event.key == pygame.K_RIGHT: direction = (1, 0) elif event.key == pygame.K_LEFT: direction = (-1, 0) elif event.type == pygame.KEYUP: if event.key in (pygame.K_RIGHT, pygame.K_LEFT): direction = (0, 0) # self.game_screen.blit(self.images['player'], (0, 0)) player.move(direction, self.game_screen) for mob in enemies: mob.move(self.game_screen) for mob in enemies: if player.collide(mob): text = self.render_text("You suck.") self.lose_game(text) self.run_game() if player.collide(treasure): text = self.render_text("There is only harshness ahead.") self.win_game(text) self.run_game() # self.game_screen.fill(COLOR_MAIN) self.game_screen.blit(self.background, (0, 0)) player.draw(self.game_screen) # Is this significantly less efficient than having the one loop handle collision + drawing? # I guess in complex stuff its not even feasible to do together, but doesn't that increase inefficiency? Pah. for mob in enemies: mob.draw(self.game_screen) treasure.draw(self.game_screen) pygame.display.update() self.clock.tick(FPS) # pygame.draw.rect(self.game_screen, COLOR_WHITE, [400, 100, 100, 100]) # pygame.draw.circle(self.game_screen, COLOR_BLACK, (450, 150), 50) pygame.quit() quit() # Hmm.
class GLWidget(QtOpenGL.QGLWidget): def __init__(self, parent = None): super(GLWidget, self).__init__(parent) self.manager = GLManager(self) self.coordinate = Coordinate() self.entity = None def loadobj(self, filename): self.entity = Entity() self.entity.load_obj(filename) #threading.Thread(target=self.entity.load_obj, args=(filename, ), #name='loadobj').start() def paintGL(self): self.manager.clear() self.coordinate.draw() if self.entity is not None: self.entity.draw() self.swapBuffers() def resizeGL(self, w, h): self.width, self.height = w, h self.setGeometry(QtCore.QRect(0, 0, w, h)) self.manager.resize(w, h) def initializeGL(self): self.manager.init() def update(self): self.manager.update() self.updateGL() def change(self, selected): if selected == 1: self.manager.zoom_view(0) elif selected == 2: self.manager.zoom_view(1) elif selected == 3: self.manager.move_view(0) elif selected == 4: self.manager.move_view(1) self.update() def light_toggle(self): res = self.manager.light_toggle() self.update() return res def show_line_toggle(self): res = self.entity.show_line_toggle() self.update() return res def show_texture_toggle(self): res = self.entity.show_texture_toggle() self.update() return res def loop_subdivision(self): self.entity.gen_half_edge_structure() self.entity.loop_subdivision() self.entity.display_subdivision_toggle() self.update() def paint_lists(self): if self.entity is not None: self.entity.display_lists_toggle() self.update() def rotate(self, value): if self.entity is not None: self.entity.set_rotation(value) self.update() def scale(self, axis, value): if self.entity is not None: self.entity.set_scale(axis, value) self.update() def translate(self, axis, value): if self.entity is not None: self.entity.set_translation(axis, value) self.update()
def draw(self): Entity.draw(self)
def draw(self, surface, camera): Entity.draw(self, surface, camera) camerax, cameray = camera.getPosition() if (self.texture != None): surface.blit(self.texture, (self.x - camerax, self.y - cameray))
key = curses.KEY_RIGHT while True: win.border(0) win.timeout(100) key = win.getch() if key == ord('q'): curses.nocbreak() screen.keypad(False) curses.echo() curses.endwin() break if key == curses.KEY_LEFT: player.move(0, -1) if key == curses.KEY_RIGHT: player.move(0, 1) if key == curses.KEY_UP: player.move(-1, 0) if key == curses.KEY_DOWN: player.move(1, 0) key = -1 player.draw(win)