def draw_text_middle(surface, text, size, color): font = pygame.font.SysFont("media\TETRIS.TTF", size, bold=True) label = font.render(text, 1, color) surface.blit(label, (top_left_x + play_width / 2 - (label.get_width() / 2), top_left_y + play_height / 2 - label.get_height() / 2))
def do_blit_test(surface, flag): _fill_surface(surface) pattern = _make_object() y = 0 while y < 600: surface.blit(pattern, (200, y), special_flags=flag) y += 50
def drawShields(surface, shields): x = 10 y = 27 im = pygame.image.load(path.join(imageDir, 'shield.png')).convert() im.set_colorkey(purple) for shield in range(shields): shieldRect = im.get_rect(left=x, top=y) surface.blit(im, shieldRect) x += shieldRect.width + 3
def over_char_render(self, surface): tw = self.tiledmap.tilewidth th = self.tiledmap.tileheight gt = self.tiledmap.get_tile_image for y in range(0, self.tiledmap.height): for x in range(0, self.tiledmap.width): tile = gt(x, y, 3) if tile: surface.blit(tile, (x*tw, y*th))
def drawText(surface, text, fontName, fontSize, x, y, boxSize=()): font = pygame.font.Font(fontName, fontSize) textSurface = font.render(text, True, white) if boxSize: fitRect = textSurface.get_rect(midtop=(x, y)) textRect = pygame.Rect(fitRect.x, fitRect.y, boxSize[0], boxSize[1]) else: textRect = textSurface.get_rect(midtop=(x, y)) surface.blit(textSurface, textRect)
def render(self, surface): """ render visible layers """ ti = self.tmxdata.get_tile_image_by_gid for layer in self.tmxdata.visible_layers: if isinstance(layer, pytmx.TiledTileLayer): for x, y, gid, in layer: tile = ti(gid) if tile: surface.blit(tile, (x * self.tmxdata.tilewidth, y * self.tmxdata.tileheight))
def under_char_render(self, surface): tw = self.tiledmap.tilewidth th = self.tiledmap.tileheight gt = self.tiledmap.get_tile_image for y in range(0, self.tiledmap.height): for x in range(0, self.tiledmap.width): tile = gt(x, y, 2) if tile: surface.blit(tile, (x*tw, y*th)) self.boxcollider.append(Rect(x*tw, y*th, tw, th))
def test_chop(surface): """Simple tests of transform.chop""" obj = _make_object() obj1 = transform.chop(obj, (10, 10, 10, 10)) obj2 = transform.chop(obj, (10, 10, 20, 20)) obj3 = transform.chop(obj, (0, 0, 100, 100)) obj4 = transform.chop(obj, (5, 5, 50, 50)) surface.blit(obj, (20, 20)) surface.blit(obj1, (80, 20)) surface.blit(obj2, (160, 20)) surface.blit(obj3, (240, 20)) surface.blit(obj4, (320, 20))
def drawSelf(self, surface, coords=[0, 0], width=16, color=(50, 50, 50)): # Detects if there is any change in variable. if so, then change the internal variable, so that it's not done every frame. for node in self.connectedNodes: if self.connectedNodes[node][4] != width or self.connectedNodes[node][5] != color: self.changeColorWidth(node, color, width) for connectedNodes in list(self.connectedNodes.values()): # connectedNodes is the values inside the actual connectedNodes addition = ( -math.cos(connectedNodes[1] * math.pi/180) * 16 , math.sin(connectedNodes[1] * math.pi/180) * 16 ) newCoords = [ a + b + c + d if b < 0 else a + c for a, b, c, d in zip(self.coords, connectedNodes[0], coords, addition) ] surface.blit(connectedNodes[3], newCoords ) # Draw arrow that signifies in which direction the car in the road is going to go. GMfun.drawArrow(surface, [ a + b + 8 + ( c / 2 ) for a, b, c in zip(coords, self.coords, connectedNodes[0]) ], 10, 60, 360 - connectedNodes[1], (120, 120, 120), 3)
def test_flip_subsurface(surface): """Test transform.flip with a subsurface as the source""" obj = _make_object() surface.blit(obj, (20, 20)) obj_sub = obj.subsurface((20, 20, 15, 25)) surface.blit(obj_sub, (20, 190)) obj1 = transform.flip(obj_sub, True, False) surface.blit(obj1, (60, 190)) obj1 = transform.flip(obj_sub, False, True) surface.blit(obj1, (20, 390)) obj1 = transform.flip(obj_sub, False, False) surface.blit(obj1, (60, 390))
def test_chop_subsurface(surface): """Test transform.chop with a subsurface as the source""" obj = _make_object() surface.blit(obj, (20, 20)) obj_sub = obj.subsurface((20, 20, 20, 15)) obj1 = transform.chop(obj_sub, (10, 10, 10, 10)) obj2 = transform.chop(obj_sub, (10, 10, 0, 0)) # Chop out everything obj3 = transform.chop(obj_sub, (0, 0, 20, 15)) surface.blit(obj_sub, (20, 190)) surface.blit(obj1, (80, 190)) surface.blit(obj2, (160, 190)) surface.blit(obj3, (190, 190))
def test_rotozoom(surface): obj = _make_object() x = 20 y = 20 space = obj.get_height() for angle in range(1, 200, 14): for scale in range(5, 20, 3): obj1 = transform.rotozoom(obj, angle, scale / 10.0) surface.blit(obj1, (x, y)) x += obj1.get_width() + 5 space = max(space, obj1.get_height()) if x > 650: y += space + 5 x = 20
def draw_window(surface, grid, score=0, last_score=0, pscore=0): font = pygame.font.Font('media\gamefont.ttf', 80) label = font.render('Tetris', 1, (255, 255, 255)) surface.blit(background_image, [-1, 0]) surface.blit(label, (top_left_x + play_width / 2 - (label.get_width() / 2), 15)) for i in range(len(grid)): for j in range(len(grid[i])): pygame.draw.rect(surface, grid[i][j], (top_left_x + j * block_size, top_left_y + i * block_size, block_size, block_size), 0) pygame.draw.rect(surface, (255, 255, 255), (top_left_x + 1, top_left_y, play_width, play_height), 4) font = pygame.font.Font('media\gamefont.ttf', 30) label = font.render("score:" + str(score), 1, (255, 255, 255)) sx = top_left_x + play_width + 50 sy = top_left_y + play_height / 2 - 100 pygame.draw.rect(surface, (255, 255, 255), (sx + 1, sy - 28, 138, 188), 0) pygame.draw.rect(surface, (0, 0, 0), (sx + 5, sy - 25, 130, 182), 0) surface.blit(label, (sx + 10, sy + 130)) font = pygame.font.Font('media\gamefont.ttf', 30) label = font.render("High Score:" + str(last_score), 1, (255, 255, 255)) sx = top_left_x - 200 sy = top_left_y + 200 pygame.draw.rect(surface, (255, 255, 255), (sx - 20, sy + 125, 215, 74), 0) pygame.draw.rect(surface, (0, 0, 0), (sx - 17, sy + 128, 208, 68), 0) surface.blit(label, (sx - 15, sy + 130)) font = pygame.font.Font('media\gamefont.ttf', 30) label = font.render("Previous Score:" + str(pscore), 1, (255, 255, 255)) surface.blit(label, (sx - 15, sy + 160)) draw_grid(surface, grid)
def draw_next_shape(shape, surface): font = pygame.font.Font('media\gamefont.ttf', 30) label = font.render("Next Shape", 1, (255, 255, 255)) sx = top_left_x + play_width + 50 sy = top_left_y + play_height / 2 - 100 format = shape.shape[shape.rotation % len(shape.shape)] for i, line in enumerate(format): row = list(line) for j, column in enumerate(row): if column == '0': pygame.draw.rect( surface, shape.color, ((sx + j * block_size), (sy + i * block_size), block_size, block_size), 0) surface.blit(label, (sx + 10, sy - 25))
def test_flip(surface): """Simple flip tests""" obj = _make_object() surface.blit(obj, (20, 20)) obj1 = transform.flip(obj, True, False) surface.blit(obj1, (80, 80)) obj1 = transform.flip(obj, False, True) surface.blit(obj1, (160, 160)) obj1 = transform.flip(obj, True, True) surface.blit(obj1, (320, 320))
def drawBetterLine(surface, color, x1: float, y1: float, x2: float, y2: float, width=1): # DOESNT WORK vector = pygame.math.Vector2(x2 - x1, y2 - y1) length = round(vector.length()) lineSurf = pygame.Surface((round(length), width), pygame.SRCALPHA) lineSurf.fill(list(color) + [0]) pygame.draw.rect(lineSurf, color, (0, 0, round(length), width)) angle = 360 - (np.arctan2(vector[1], vector[0]) * 180 / math.pi) lineSurf = pygame.transform.rotate(lineSurf, angle) surface.blit(lineSurf, [a + b if b < 0 else a for a, b in zip([x1, y1], vector)])
def test_chop(surface): """Simple tests of transform.chop""" obj = _make_object() obj1 = transform.chop(obj, (10, 10, 10, 10)) obj2 = transform.chop(obj, (10, 10, 20, 20)) obj3 = transform.chop(obj, (0, 0, 100, 100)) obj4 = transform.chop(obj, (5, 5, 50, 50)) surface.blit(obj, (20, 20)) surface.blit(obj1, (80, 20)) surface.blit(obj2, (160, 20)) surface.blit(obj3, (240, 20)) surface.blit(obj4, (320, 20)) obj_sub = obj.subsurface((20, 20, 20, 15)) obj1 = transform.chop(obj_sub, (10, 10, 10, 10)) obj2 = transform.chop(obj_sub, (10, 10, 0, 0)) # Chop out everything obj3 = transform.chop(obj_sub, (0, 0, 20, 15)) surface.blit(obj_sub, (20, 190)) surface.blit(obj1, (80, 190)) surface.blit(obj2, (160, 190)) surface.blit(obj3, (190, 190))
def draw_menu_window(surface): sx = play_width / 2 sy = play_height / 2 surface.blit(background_image, [-1, 0]) pygame.draw.rect(surface, (255, 255, 255), (sx + 167, sy - 3, 166, 116), 0) pygame.draw.rect(surface, (0, 0, 0), (sx + 172, sy + 2, 156, 106), 0)
def test_scale(surface): """Simple scale tests""" obj = _make_object() surface.blit(obj, (20, 20)) obj1 = transform.scale(obj, (100, 100)) surface.blit(obj1, (80, 80)) obj2 = transform.scale(obj1, (60, 60)) surface.blit(obj2, (160, 160)) obj3 = transform.scale(obj, (60, 60)) surface.blit(obj3, (240, 160)) obj1 = transform.scale2x(obj) surface.blit(obj1, (240, 240)) obj1 = transform.scale2x(obj2) surface.blit(obj1, (320, 320))
def test_rotate(surface): """Simple rotation tests""" obj = _make_object() surface.blit(obj, (20, 20)) obj1 = transform.rotate(obj, 90) surface.blit(obj1, (80, 20)) obj1 = transform.rotate(obj, 180) surface.blit(obj1, (160, 20)) obj1 = transform.rotate(obj, 270) surface.blit(obj1, (240, 20)) obj1 = transform.rotate(obj, -90) surface.blit(obj1, (320, 20)) obj1 = transform.rotate(obj, -180) surface.blit(obj1, (480, 20)) x = 20 y = 100 for angle in range(1, 200, 7): obj1 = transform.rotate(obj, angle) surface.blit(obj1, (x, y)) x += obj1.get_width() + 5 if x > 650: y += 2*obj.get_height() + 5 x = 20
pygame.display.set_caption('LogMeIn!') screen= pygame.display.set_mode((660,480),0) surface = pygame.surface.Surface((660,480),0,screen) button1 = Rect (640,20,660,50) cam = pygame.camera.Camera(0,(640,480)) cam.start() #def handleMouseDown(): i=0 while 1: i=i+1 im = cam.get_image(surface) if i in range (300,320): surf2=pygame.image.load("F:\\3.bmp") surface.blit(surf2,(220,50)) if i in range (330,350): surf2=pygame.image.load("F:\\2.bmp") surface.blit(surf2,(220,50)) if i in range (360,380): surf2=pygame.image.load("F:\\1.bmp") surface.blit(surf2,(220,50)) if i==400: pygame.image.save(im, "F:\\a.bmp") #subprocess.call("F:\\random.bat") #print(123) sys.exit() screen.blit(im,(0,0)) #top pygame.draw.line(screen, (255, 0, 0), (220, 50), (460, 50)) #left
(t.hour % 12) * 5 + t.minute // 12) % 60, t.minute, t.second #draw face pygame.draw.circle(surface, pygame.Color('darkslategray'), (H_WIDTH, H_HEIGHT), RADIUS) for digit, pos in clock60.items(): radius = 20 if not digit % 3 and not digit % 5 else 8 if not digit % 5 else 2 pygame.draw.circle(surface, pygame.Color('gainsboro'), get_clock_pos(clock60, digit, 'digit'), radius, 7) #draw clock pygame.draw.line(surface, pygame.Color('orange'), (H_WIDTH, H_HEIGHT), get_clock_pos(clock60, hour, 'hour'), 15) pygame.draw.line(surface, pygame.Color('green'), (H_WIDTH, H_HEIGHT), get_clock_pos(clock60, minute, 'min'), 7) pygame.draw.line(surface, pygame.Color('magenta'), (H_WIDTH, H_HEIGHT), get_clock_pos(clock60, second, 'sec'), 4) pygame.draw.circle(surface, pygame.Color('white'), (H_WIDTH, H_HEIGHT), 8) #digital clock time_render = font.render(f'{t:%H:%M:%S}', True, pygame.Color('forestgreen'), pygame.Color('blue')) surface.blit(time_render, (0, 0)) # draw arc sec_angel = -math.radians(clock60[t.second]) + math.pi / 2 pygame.draw.arc(surface, pygame.Color('magenta'), (H_WIDTH - RADIUS_ARK, H_HEIGHT - RADIUS_ARK, 2 * RADIUS_ARK, 2 * RADIUS_ARK), math.pi / 2, sec_angel, 8) pygame.display.flip() clock.tick(20)
def render(self, surface): surface.blit(self.background, (0, 0)) for creature_type in self.creatures: for creature in self.creatures[creature_type]: creature.render(surface)
def test_rotate(surface): """Simple rotation tests""" obj = _make_object() surface.blit(obj, (20, 20)) obj1 = transform.rotate(obj, 90) surface.blit(obj1, (80, 20)) obj1 = transform.rotate(obj, 180) surface.blit(obj1, (160, 20)) obj1 = transform.rotate(obj, 270) surface.blit(obj1, (240, 20)) obj1 = transform.rotate(obj, -90) surface.blit(obj1, (320, 20)) obj1 = transform.rotate(obj, -180) surface.blit(obj1, (480, 20)) x = 20 y = 100 for angle in range(1, 200, 7): obj1 = transform.rotate(obj, angle) surface.blit(obj1, (x, y)) x += obj1.get_width() + 5 if x > 650: y += 2 * obj.get_height() + 5 x = 20
def draw(self, surface, image): for i in range(0, self.length): surface.blit(image, (self.x[i], self.y[i]))
def draw(self, surface, image): surface.blit(image, (self.x, self.y))
def drawRondas(surface, text, size, x, y): font = pygame.font.SysFont("serif", size) text_suirface = font.render(text, True, white) text_rect = text_suirface.get_rect() text_rect = (x, y) surface.blit(text_suirface, text_rect)
def test_y_smoothscale(surface): """Scale y axis only""" obj = _make_object() transform.set_smoothscale_backend('GENERIC') surface.blit(obj, (20, 20)) obj1 = transform.smoothscale(obj, (20, 25)) surface.blit(obj1, (80, 80)) obj2 = transform.smoothscale(obj, (20, 35)) surface.blit(obj2, (160, 160)) obj3 = transform.smoothscale(obj, (20, 55)) surface.blit(obj3, (240, 160)) obj4 = transform.smoothscale(obj, (20, 15)) surface.blit(obj4, (240, 240)) obj5 = transform.smoothscale(obj, (20, 100)) surface.blit(obj5, (320, 320))
def test_subsurface_smoothscale(surface): """Test scaling a small subsurface""" obj = _make_object() transform.set_smoothscale_backend('GENERIC') obj_sub = obj.subsurface((20, 20, 20, 25)) surface.blit(obj, (20, 20)) surface.blit(obj_sub, (60, 20)) obj1 = transform.smoothscale(obj_sub, (obj_sub.get_width(), obj_sub.get_height())) surface.blit(obj1, (120, 20)) obj1 = transform.smoothscale(obj_sub, (20, 20)) surface.blit(obj1, (60, 60)) obj1 = transform.smoothscale(obj_sub, (40, 40)) surface.blit(obj1, (80, 80)) obj1 = transform.smoothscale(obj_sub, (60, 60)) surface.blit(obj1, (160, 160)) obj1 = transform.smoothscale(obj_sub, (120, 120)) surface.blit(obj1, (220, 220))
def draw(self, surface): t = self.font.render(self.text, True, self.colour) surface.blit(t, (self.x, self.y))
def test_int_smoothscale(surface): """Simple integer scaling tests""" obj = _make_object() transform.set_smoothscale_backend('GENERIC') surface.blit(obj, (20, 20)) obj1 = transform.smoothscale(obj, (20, 20)) surface.blit(obj1, (60, 60)) obj1 = transform.smoothscale(obj, (40, 40)) surface.blit(obj1, (80, 80)) obj1 = transform.smoothscale(obj, (60, 60)) surface.blit(obj1, (160, 160)) obj1 = transform.smoothscale(obj, (120, 120)) surface.blit(obj1, (320, 320))
def test_varied_smoothscale(surface): """Scale with more varies factors""" obj = _make_object() transform.set_smoothscale_backend('GENERIC') surface.blit(obj, (20, 20)) obj1 = transform.smoothscale(obj, (25, 55)) surface.blit(obj1, (80, 80)) obj1 = transform.smoothscale(obj, (14, 13)) surface.blit(obj1, (160, 160)) obj1 = transform.smoothscale(obj, (72, 68)) surface.blit(obj1, (320, 320))