def paintEvent(self, event): painter = QPainter(self) painter.drawRoundedRect(rect, 1, 1) Window.normalizedObjects() Clipping().clip(Window.listObjects) Viewport.transformViewport() for object in Viewport.listObjects: object.draw(painter)
def transformViewport(): Viewport.listObjects = Window.copyListObjects(Window.listObjects) [xmin, ymin], [xmax, ymax] = Window.expanded_boundaries() for object in Viewport.listObjects: listPoints = [] for point in object.points: x, y = point xvp = ((x - xmin) / (xmax - xmin)) * (Viewport.xmax - Viewport.xmin) yvp = (1 - ((y - ymin) / (ymax - ymin))) * (Viewport.ymax - Viewport.ymin) listPoints.append([xvp, yvp]) object.points = listPoints
def liangBarsky(self, line): minimum, maximum = Window.boundaries() xmin, ymin = minimum xmax, ymax = maximum [[x1, y1], [x2, y2]] = line t = [None, None, None, None] p = [-(x2 - x1), x2 - x1, -(y2 - y1), y2 - y1] q = [x1 - xmin, xmax - x1, y1 - ymin, ymax - y1] t1 = 0 t2 = 1 for i in range(0, 4): if p[i] > 0: t[i] = q[i] / p[i] t2 = min(t2, t[i]) elif p[i] < 0: t[i] = q[i] / p[i] t1 = max(t1, t[i]) elif q[i] < 0: return None if t1 == 0 and t2 == 0: return line if t1 < t2: return [[x1 + t1 * p[1], y1 + t1 * p[3]], [x1 + t2 * p[1], y1 + t2 * p[3]]] else: return None
def __init__(self): self._saved_path = r'../graphics/gateway' self._saved_path_glonass = r'../graphics/gateway/glonass' self._manager = plt.get_current_fig_manager() self._window = Window(*self._manager.window.maxsize()) self._full_screen_enable()
def pointClipping(self, listObjects): minimum, maximum = Window.boundaries() xmin, ymin = minimum xmax, ymax = maximum for i, object in enumerate(listObjects): if len(object.points) == 1: x, y = object.points[0] if not (xmin <= x <= xmax and ymin <= y <= ymax): listObjects[i].clip = True
def sutherlandHodgman(self, subject): minimum, maximum = Window.boundaries() xmin, ymin = minimum xmax, ymax = maximum clip = [[xmin, ymax], [xmin, ymin], [xmax, ymin], [xmax, ymax]] def inside(p, cp0, cp1): [x, y] = p [x0, y0] = cp0 [x1, y1] = cp1 return (x1 - x0) * (y - y0) > (y1 - y0) * (x - x0) def intersect(p1, p2, cp0, cp1): [x0, y0] = cp0 [x1, y1] = cp1 dc = (x0 - x1, y0 - y1) dp = (p1[0] - p2[0], p1[1] - p2[1]) n1 = x0 * y1 - y0 * x1 n2 = p1[0] * p2[1] - p1[1] * p2[0] n3 = 1.0 / (dc[0] * dp[1] - dc[1] * dp[0]) return ((n1 * dp[0] - n2 * dc[0]) * n3, (n1 * dp[1] - n2 * dc[1]) * n3) output = subject.copy() cp0 = clip[-1] for clip_vertex in clip: cp1 = clip_vertex inputList = output output = [] # Polygon out of window, do not draw if len(inputList) == 0: return None s = inputList[-1] for subjectVertex in inputList: e = subjectVertex if inside(e, cp0, cp1): if not inside(s, cp0, cp1): output.append(intersect(s, e, cp0, cp1)) output.append(e) elif inside(s, cp0, cp1): output.append(intersect(s, e, cp0, cp1)) s = e cp0 = cp1 return output
def cohenSutherland(self, line): [p0, p1] = line minimum, maximum = Window.boundaries() xmin, ymin = minimum xmax, ymax = maximum codeLineStart = self.computeCode(p0, xmin, ymin, xmax, ymax) codeLineEnd = self.computeCode(p1, xmin, ymin, xmax, ymax) while (True): outcodeOut = codeLineEnd if codeLineEnd > codeLineStart else codeLineStart if not (codeLineStart | codeLineEnd): return line elif (codeLineStart & codeLineEnd): return None else: [[x0, y0], [x1, y1]] = line x_new = None y_new = None if outcodeOut & self.TOP: x_new = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0) y_new = ymax elif outcodeOut & self.BOTTOM: x_new = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0) y_new = ymin elif outcodeOut & self.RIGHT: y_new = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0) x_new = xmax elif outcodeOut & self.LEFT: y_new = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0) x_new = xmin if (outcodeOut == codeLineStart): line[0] = [x_new, y_new] codeLineStart = self.computeCode(line[0], xmin, ymin, xmax, ymax) else: line[1] = [x_new, y_new] codeLineEnd = self.computeCode(line[1], xmin, ymin, xmax, ymax)
def moveLookUp(self): Window.move([0, 15]) #alterar para ser o look
def rotateWindow(self): angleWin = int(self.rotateWinAng.displayText()) Window.rotateWindow(angleWin)
def moveUp(self): Window.move([0, 15])
def zoomOut(self): Window.zoom(1.1)
def zoomIn(self): Window.zoom(0.9)
def moveLookRight(self): Window.move([15, 0]) #alterar para ser o look
def moveDown(self): Window.move([0, -15])
def main(): ''' The main function, provided *as* a function so as to deal with Sphinx's autodocs ''' # Main globals DIMENSIONS = (1280, 720) FONT_SIZE = 100 window = Window(dimensions=DIMENSIONS, resizable=True) invoker = Invoker() sizer = Sizer(window=window, font_size=FONT_SIZE) # Text colours text_colours = { K_q: Colour('white'), K_w: Colour('white'), K_e: Colour('white'), K_r: Colour('white'), } # Pressed keys cache pressed_keys = [] counter = 0 cast = False first = False last_cast = None while not window.is_closed: # Add the frame ticker to the top left # window.draw_font('Tick {:X}'.format(counter), location=(0, 250), size=32) # Create all the sprites needed window.sprites.empty() # Background block a = Block(dimensions=sizer('34%', '14%')) a.rect.midbottom = sizer('50%', '100%') # Left line b = Block(dimensions=sizer('5px', '14%'), colour=Colour('white')) b.rect.midbottom = sizer('41.5%', '100%') # Middle line c = Block(dimensions=sizer('5px', '14%'), colour=Colour('white')) c.rect.midbottom = sizer('50%', '100%') # Right line d = Block(dimensions=sizer('5px', '14%'), colour=Colour('white')) d.rect.midbottom = sizer('58.5%', '100%') # Add to window window.add_sprites(a, b, c, d) # Capture keydown and keyup events for i in window.events: if i.type == KEYDOWN: # Spell keys if i.key in [K_q, K_w, K_e]: text_colours[i.key] = Colour('red') pressed_keys.append(i.unicode.upper()) cast = False # Cast key elif i.key == K_r: text_colours[i.key] = Colour('red') if len(pressed_keys) >= 3: cast = True first = True elif i.type == KEYUP: if i.key in [K_q, K_w, K_e, K_r]: text_colours[i.key] = Colour('white') # Get the font sizes full_size = int(sizer('1em')) half_size = int(sizer('.5em')) # Draw the QWE onto the screen window.draw_font('Q', location=sizer('35.0%', '88.9%'), size=full_size, colour=text_colours[K_q]) window.draw_font('W', location=sizer('43.3%', '89.4%'), size=full_size, colour=text_colours[K_w]) window.draw_font('E', location=sizer('52.5%', '89.2%'), size=full_size, colour=text_colours[K_e]) window.draw_font('R', location=sizer('60.8%', '89.4%'), size=full_size, colour=text_colours[K_r]) # Output the goal spell you want to achieve window.draw_font('Goal: {.goal.name}'.format(invoker), location=sizer(5, '.5em'), size=half_size, colour=Colour('black')) # Output the socre window.draw_font('Score: {.score}'.format(invoker), location=sizer(5, 5), size=half_size, colour=Colour('black')) # Temp output the last three characters window.draw_font(''.join(pressed_keys[-3:]), location=sizer(5, '1em'), size=half_size, colour=Colour('black')) # Get the working spell, if any x = invoker.cast(pressed_keys) # Determine whether a spell has been cast and no other buttons pressed if cast: # Run the first time that there has been no cast spell if first: last_cast = x first = False if last_cast == invoker.goal: invoker.score += 1 invoker.make_goal() # Draw spell onto screen pressed_keys = [] l = sizer(5, '1.5em') if pressed_keys else sizer(5, '1em') window.draw_font('Cast: {0!s}'.format(last_cast), location=l, size=half_size, colour=Colour('black')) # Run the program window.run() counter += 1 # Frame tick
def moveRight(self): Window.move([15, 0])
def moveLeft(self): Window.move([-15, 0])
def moveLookDown(self): Window.move([0, -15]) #alterar para ser o look
def moveLookLeft(self): Window.move([-15, 0]) #alterar para ser o look
import random from models.window import Window pygame.init() pygame.mixer.pre_init(44100, 16, 2, 4096) TITLE = "My First Game" SIZE = (1080, 720) BACKGROUND = "./assets/bg.jpg" PLAYER = "./assets/player.png" MUMMY = "./assets/mummy.png" BANNER = "./assets/banner.png" BUTTON = "./assets/button.png" FPS = 30 clock = pygame.time.Clock() game = Window(SIZE, TITLE) game.set_background(BACKGROUND, (0, -220)) game.set_banner(BANNER, (int(game.screen.get_width() / 4), 0)) game.set_button( BUTTON, (int(game.screen.get_width() / 3.33), int(game.screen.get_width() / 3))) game.set_player((450, 500)) for i in range(2): if random.randint(1, 2) == 1: game.add_mummy((game.screen.get_width(), 550), "left") else: game.add_mummy((0, 550), "right") if random.randint(1, 2) == 1: game.add_alien((game.screen.get_width(), 400), "left") else:
from PyQt5.QtCore import QRectF, QPoint from PyQt5.QtWidgets import QWidget from PyQt5.QtGui import QPainter, QPalette, QColor from PyQt5 import QtCore from models.viewport import Viewport from models.window import Window from operations.clipping import Clipping [xmin, ymin], [xmax, ymax] = Window.expanded_boundaries() xini = ((0 - xmin) / (xmax - xmin)) * (Viewport.xmax - Viewport.xmin) yini = (1 - ((0 - ymin) / (ymax - ymin))) * (Viewport.ymax - Viewport.ymin) xfin = ((800 - xmin) / (xmax - xmin)) * (Viewport.xmax - Viewport.xmin) yfin = (1 - ((450 - ymin) / (ymax - ymin))) * (Viewport.ymax - Viewport.ymin) rect = QRectF(QPoint(xini, yini), QPoint(xfin, yfin)) class RightWidget(QWidget): def __init__(self): super().__init__() self.setFixedSize(Viewport.xmax, Viewport.ymax) self.setAutoFillBackground(True) palette = self.palette() palette.setColor(QPalette.Window, QColor('white')) self.setPalette(palette) self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.update) self.timer.start(1000 / 60) def paintEvent(self, event):