From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Sun, 2 Jul 2023 01:13:37 +0000 (-0400) Subject: menu uses sdl2 now X-Git-Tag: game~8 X-Git-Url: http://git.skullheadx.com/nixos/static/projects.html?a=commitdiff_plain;h=17eb9ca55f87c87a0ba47d81cd46e9e06caefe89;p=fruit-ninja.git menu uses sdl2 now --- diff --git a/effect.py b/effect.py index 0035c3a..ba608dd 100644 --- a/effect.py +++ b/effect.py @@ -24,7 +24,7 @@ class BloodEffect: pygame.transform.rotate(pygame.transform.scale(frame, (int(self.radius * 2), int(self.radius * 2))), self.angle) for frame in random.choice(self.blood_frames)] if color is None: - color = random.choice(COLORS) + color = random.choice(EFFECT_COLORS) else: color = color for frame in self.frames: @@ -67,7 +67,7 @@ class BloodSplatter: color_frames = [dict(), dict()] - for c in COLORS: + for c in EFFECT_COLORS: for i, f in enumerate(blood_frames): c_f = f.copy() px_array = pygame.PixelArray(c_f) @@ -90,7 +90,7 @@ class BloodSplatter: self.alpha = 255 if color is None: - color = random.choice(COLORS) + color = random.choice(EFFECT_COLORS) self.frame = pygame.transform.rotate( pygame.transform.scale(self.color_frames[img_index][color], (int(self.radius * 2), int(self.radius * 2))), diff --git a/fruit.py b/fruit.py index 1c9e6f4..90128ec 100644 --- a/fruit.py +++ b/fruit.py @@ -36,10 +36,9 @@ class Fruit: self.image = pygame.transform.scale(random.choice(self.HEADS), (self.radius * 2, self.radius * 2)) self.angle = lerp(0, 360, random.random()) self.direction = random.choice([-1, 1]) - # self.angle = 0 - # self.direction = 0 self.width, self.height = (self.radius * 2, self.radius * 2) + self.fruit_txt = Texture.from_surface(renderer, self.image) def update(self, delta): # self.previous_position = self.position.copy() - self.velocity / 1000 * 30 @@ -52,12 +51,6 @@ class Fruit: return pygame.Rect(self.position - pygame.Vector2(self.radius / 2, self.radius / 2), pygame.Vector2(self.radius, self.radius)) - def draw(self, surf): - # rotated_image = pygame.transform.rotate(self.image, self.angle) - # new_rect = rotated_image.get_rect(center=self.image.get_rect(topleft=(self.position.x - self.radius, self.position.y - self.radius)).center) - # surf.blit(rotated_image, new_rect.topleft) - # pygame.draw.circle(surf, BLACK, self.position, self.radius) + def draw(self): if self.position.y - self.radius <= HEIGHT: - rotated_image, position = rotate_center(self.image, self.angle, self.position) - self.width, self.height = rotated_image.get_size() - surf.blit(rotated_image, position) + self.fruit_txt.draw(None, self.position - pygame.Vector2(self.radius,self.radius), angle=self.angle, origin=None) diff --git a/game.py b/game.py index 7fab447..eb3ecd6 100644 --- a/game.py +++ b/game.py @@ -121,7 +121,7 @@ class Game: for hit, mouse_direction, mouse_position in hits: - color = random.choice(COLORS) + color = random.choice(EFFECT_COLORS) self.effects[0].append( BloodSplatter(hit.position, hit.radius, determine_angle(hit.position, hit.position + mouse_direction),color)) self.effects[1].append(BloodEffect(hit.position, hit.radius,lighten(color, 0.15))) diff --git a/image.png b/image.png new file mode 100644 index 0000000..73e6ada Binary files /dev/null and b/image.png differ diff --git a/main.py b/main.py index a76124d..f1051cc 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ -from game import Game +# from game import Game from menu import Menu from setup import * + FPS = 60 clock = pygame.time.Clock() @@ -10,18 +11,23 @@ scene = Menu() is_running = True while is_running: delta = clock.tick(FPS) + renderer.clear() + + status = scene.update(delta) - scene.draw(screen) + scene.draw() + fps_text = font.render(f"FPS {int(clock.get_fps())}", True, WHITE) - screen.blit(fps_text, (WIDTH - fps_text.get_width() - 7, 0)) + fps_text_txt = Texture.from_surface(renderer, fps_text) + fps_text_txt.draw(None, (WIDTH - fps_text.get_width() - 7, 0)) - pygame.display.update() + renderer.present() if status == COMMAND_EXIT: is_running = False - elif status == COMMAND_START: - scene = Game() - elif status == COMMAND_MENU: - scene = Menu() + # elif status == COMMAND_START: + # scene = Game() + # elif status == COMMAND_MENU: + # scene = Menu() pygame.quit() diff --git a/menu.py b/menu.py index 74e6020..e7254e2 100644 --- a/menu.py +++ b/menu.py @@ -1,14 +1,12 @@ -import random - -import pygame - from setup import * from player import Player from fruit import Fruit from effect import SlashEffect, SplitEffect, BloodEffect, FadeOutEffect, BloodSplatter +from rect import Rect class Menu: + # Creating background image background = pygame.Surface((WIDTH, HEIGHT)) tile_cols = 4 tile_rows = 4 @@ -17,36 +15,60 @@ class Menu: for x in range(tile_cols): for y in range(tile_rows): background.blit(background_tile, (x * WIDTH / tile_cols, y * HEIGHT / tile_rows)) + bg_txt = Texture.from_surface(renderer, background) - slash_sounds = [pygame.mixer.Sound(f"assets/sounds/Swishes/long-medium-swish-44324.wav"), - pygame.mixer.Sound(f"assets/sounds/Swishes/swing-6045.wav"), - pygame.mixer.Sound(f"assets/sounds/Swishes/swish-sound-94707.wav"), - ] + # Slash sound effects + slash_sounds = [ + pygame.mixer.Sound(f"assets/sounds/Swishes/long-medium-swish-44324.wav"), + pygame.mixer.Sound(f"assets/sounds/Swishes/swing-6045.wav"), + pygame.mixer.Sound(f"assets/sounds/Swishes/swish-sound-94707.wav"), + ] def __init__(self): - self.background_music = pygame.mixer.music.load( - "assets/sounds/Of Far Different Nature - Ethnic Beat (CC-BY).ogg") + # Music + pygame.mixer.music.load("assets/sounds/Of Far Different Nature - Ethnic Beat (CC-BY).ogg") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(-1) pygame.mixer.music.pause() + # Player self.player = Player() + + # Fruit self.fruit = Fruit() self.fruit.position = pygame.Vector2(WIDTH / 2, HEIGHT * 1.5 / 2.5) self.fruit.angle = 0 self.fruit.image = pygame.transform.scale(pygame.image.load("assets/fruits/58.png"), (self.fruit.radius * 2, self.fruit.radius * 2)) - + # Effects self.effects = [] + # Text surfaces self.title_surface = font_large.render("Fruit Shinobi", True, WHITE) self.tutorial_surface = font.render("Drag to slice the fruit", True, WHITE) self.controls_surface = font_small.render("Press M to unmute music", True, WHITE) - self.credit_surface = font_small.render("Made by: Skullheadx", True, WHITE) + + self.title_txt = Texture.from_surface(renderer, self.title_surface) + self.tutorial_txt = Texture.from_surface(renderer, self.tutorial_surface) + self.controls_txt = Texture.from_surface(renderer, self.controls_surface) + self.credit_txt = Texture.from_surface(renderer, self.credit_surface) + + # Rect textures + self.tutorial_surface_pos = ( + WIDTH / 2, HEIGHT * 2 / 3 + self.tutorial_surface.get_height() / 2 + self.fruit.get_rect().height + 30) + + self.r1 = Rect(self.tutorial_surface.get_rect(center=self.tutorial_surface_pos).inflate(25, 25), DARK_GRAY, 10) + self.r2 = Rect(self.tutorial_surface.get_rect(center=self.tutorial_surface_pos).inflate(25, 25), BLACK, 10, 5) + + self.r3 = Rect(self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3)).inflate(50, 50), GRAY, 10) + self.r4 = Rect(self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3)).inflate(50, 50), BLACK, 10, 5) + + # blacked out self.blacked_out = False def update(self, delta): + # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE): pygame.mixer.music.stop() @@ -57,30 +79,41 @@ class Menu: pygame.mixer.music.pause() else: pygame.mixer.music.unpause() + + # Update player self.player.update(delta) + # Update fruit if not self.blacked_out: - hit_status = self.player.hits(self.fruit) - if hit_status and SplitEffect.should_split(self.fruit.image, self.fruit.angle, self.fruit.position, self.player.previous_mouse_pos, self.player.mouse_direction): - color = random.choice(COLORS) - self.effects.append(BloodEffect(self.fruit.position, self.fruit.radius,lighten(color, 0.15))) - - half1, half2, pos1, pos2 = SplitEffect.split_image(self.fruit.image, self.fruit.angle, self.fruit.position, self.player.previous_mouse_pos, - self.player.mouse_direction) - + hit_status = self.player.hits(self.fruit) # Check if player hits fruit + # Check if fruit should split + if hit_status and SplitEffect.should_split(self.fruit.image, self.fruit.angle, self.fruit.position, + self.player.previous_mouse_pos, self.player.mouse_direction): + # Split fruit + color = random.choice(EFFECT_COLORS) + n1, n2 = SplitEffect.find_normals(self.player.mouse_direction.normalize() * 5) + + self.effects.append(BloodEffect(self.fruit.position, self.fruit.radius, lighten(color, 0.15))) + half1, half2, pos1, pos2 = SplitEffect.split_image(self.fruit.image, self.fruit.angle, + self.fruit.position, self.player.previous_mouse_pos, + self.player.mouse_direction) self.effects.append(BloodSplatter(self.fruit.position, self.fruit.radius, determine_angle(self.fruit.position, - self.fruit.position + self.player.mouse_direction),color)) - self.effects.append(SlashEffect(self.fruit.position, self.fruit.angle)) + self.fruit.position + self.player.mouse_direction), + color)) - n1, n2 = SplitEffect.find_normals(self.player.mouse_direction.normalize()) - c = 5 - self.effects.append(SplitEffect(pos1, half1, pygame.Vector2(0,0), n1 * c)) - self.effects.append(SplitEffect(pos2, half2, pygame.Vector2(0,0), n2 * c)) + self.effects.append(SplitEffect(pos1, half1, pygame.Vector2(0, 0), n1)) + self.effects.append(SplitEffect(pos2, half2, pygame.Vector2(0, 0), n2)) + self.effects.append(SlashEffect(self.fruit.position, self.fruit.angle)) + # Play slash sound pygame.mixer.Sound.play(random.choice(self.slash_sounds)) + + # cue fadeout self.blacked_out = True self.effects.append(FadeOutEffect()) + + # Update effects for effect in self.effects: effect_status = effect.update(delta) if effect_status: @@ -88,28 +121,27 @@ class Menu: return COMMAND_START self.effects.remove(effect) - def draw(self, surf): - surf.blit(self.background, (0, 0)) + def draw(self): + self.bg_txt.draw(None, (0, 0)) + if not self.blacked_out: - self.fruit.draw(surf) + self.fruit.draw() + + self.r1.draw() + self.r2.draw() + + self.tutorial_txt.draw(None, self.tutorial_surface.get_rect( + center=self.tutorial_surface_pos)) + + self.r3.draw() + self.r4.draw() + + self.title_txt.draw(None, self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3))) + + self.controls_txt.draw(None, self.controls_surface.get_rect(bottomleft=(10, HEIGHT - 10))) + self.credit_txt.draw(None, self.credit_surface.get_rect(bottomright=(WIDTH - 10, HEIGHT - 10))) + + self.player.draw() - tutorial_surface_pos = ( - WIDTH / 2, HEIGHT * 2 / 3 + self.tutorial_surface.get_height() / 2 + self.fruit.get_rect().height + 30) - pygame.draw.rect(surf, DARK_GRAY, self.tutorial_surface.get_rect(center=tutorial_surface_pos).inflate(25, 25), - border_radius=10) - pygame.draw.rect(surf, BLACK, self.tutorial_surface.get_rect(center=tutorial_surface_pos).inflate(25, 25), 5, - border_radius=10) - surf.blit(self.tutorial_surface, self.tutorial_surface.get_rect( - center=tutorial_surface_pos)) - - pygame.draw.rect(surf, GRAY, self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3)).inflate(50, 50), - border_radius=10) - pygame.draw.rect(surf, BLACK, self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3)).inflate(50, 50), 5, - border_radius=10) - surf.blit(self.title_surface, self.title_surface.get_rect(center=(WIDTH / 2, HEIGHT / 3))) - - surf.blit(self.controls_surface, self.controls_surface.get_rect(bottomleft=(10, HEIGHT - 10))) - surf.blit(self.credit_surface, self.credit_surface.get_rect(bottomright=(WIDTH - 10, HEIGHT - 10))) - self.player.draw(surf) for effect in self.effects: - effect.draw(surf) + effect.draw() diff --git a/out.prof b/out.prof deleted file mode 100644 index 9d6452d..0000000 Binary files a/out.prof and /dev/null differ diff --git a/player.py b/player.py index 91a6e83..7883670 100644 --- a/player.py +++ b/player.py @@ -74,27 +74,10 @@ class Player: return True return False - def draw(self, surf): - # for line in self.lines: - # pygame.draw.circle(surf, BLUE, line[0], 5) - # # pygame.draw.rect(surf, RED, hitbox) - # pygame.draw.line(surf, GREEN, line[0], line[1], 4) - # mx, my = line[1] - line[0] - # if mx == 0: - # mx = 0.01 - # m = my / mx - # c = line[0].y - m * line[0].x - # - # x1 = line[0].x - # y1 = m * x1 + c - # x2 = line[1].x - # y2 = m * x2 + c - # - # pygame.draw.line(surf, RED, (x1, y1), (x2, y2), 4) - # pygame.draw.rect(surf, RED, hitbox) - # pygame.draw.line(surf, RED, line[0], line[1], 4) - # for pos, time in self.sliced_points: - # pygame.draw.circle(surf, RED, pos, 3) + def draw(self): + renderer.draw_color = LIGHT_GRAY if len(self.sliced_points) > 1: - pygame.draw.lines(surf, BLACK, False, [a for a, b in self.sliced_points], 6) - pygame.draw.lines(surf, LIGHT_GRAY, False, [a for a, b in self.sliced_points], 4) + for i in range(len(self.sliced_points) - 1): + renderer.draw_line(self.sliced_points[i][0], self.sliced_points[i + 1][0]) + renderer.draw_line(self.previous_mouse_pos, self.previous_mouse_pos - self.mouse_direction) + diff --git a/rect.py b/rect.py new file mode 100644 index 0000000..7c0fbf6 --- /dev/null +++ b/rect.py @@ -0,0 +1,18 @@ +from setup import * + + +def get_rect_txt(rect, color, border_radius, thickness=0): + r = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA) + pygame.draw.rect(r, color, (0, 0, rect.width, rect.height), border_radius=border_radius, + width=thickness) + return Texture.from_surface(renderer, r) + + +class Rect: + + def __init__(self, rect, color, border_radius=0, thickness=0): + self.position = pygame.Vector2(rect.x, rect.y) + self.texture = get_rect_txt(rect, color, border_radius, thickness) + + def draw(self): + self.texture.draw(None, self.position) diff --git a/setup.py b/setup.py index 9b85da7..3ff1d53 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ import pygame +from pygame._sdl2 import Window, Renderer, Texture import random import os import math @@ -6,7 +7,9 @@ from functools import cache pygame.init() WIDTH, HEIGHT = pygame.display.Info().current_w, pygame.display.Info().current_h -screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) +display = pygame.display.set_mode((WIDTH, HEIGHT)) +window = Window.from_display_module() +renderer = Renderer(window) SCALE = pygame.Vector2(WIDTH / 1536, HEIGHT / 864) @@ -20,76 +23,61 @@ font = pygame.font.Font("assets/font/go3v2.ttf", int(60 * SCALE.x)) font_large = pygame.font.Font("assets/font/go3v2.ttf", int(100 * SCALE.x)) # colors -WHITE = (255, 255, 255) -LIGHT_GRAY = (211, 211, 211) -GRAY = (128, 128, 128) -DARK_GRAY = (25, 25, 25) -BLACK = (0, 0, 0) - -RED = (255, 0, 0) -GREEN = (0, 255, 0) -BLUE = (0, 0, 255) -ORANGE = (255, 165, 0) -YELLOW = (255, 255, 0) -PURPLE = (128, 0, 128) - -BROWN = (139, 69, 19) -DARK_BROWN = (119, 49, 0) - -DARK_RED = (139, 0, 0) -DARK_GREEN = (0, 100, 0) -DARK_BLUE = (0, 0, 139) -DARK_ORANGE = (255, 140, 0) -DARK_YELLOW = (255, 215, 0) -DARK_PURPLE = (75, 0, 130) - -DEFAULT_COLORS = [ - RED, - GREEN, - BLUE, - ORANGE, - YELLOW, - PURPLE, - DARK_RED, - DARK_GREEN, - DARK_BLUE, - DARK_ORANGE, - DARK_YELLOW, - DARK_PURPLE -] - -COLORS = [ - (252, 166, 168), - (247, 203, 168), - (203, 172, 239), - (160, 247, 208), - (222, 244, 141), - (205, 255, 135), - (174, 252, 201), - (247, 167, 111), - (225, 162, 239), - (209, 239, 119), - (211, 255, 178), - (119, 249, 215), - (252, 113, 146), - (204, 247, 160), - (247, 161, 148), - (218, 186, 255), - (112, 239, 116), - (237, 186, 125), - (198, 202, 255), - (197, 252, 174), +WHITE = (255, 255, 255, 255) +LIGHT_GRAY = (211, 211, 211, 255) +GRAY = (128, 128, 128, 255) +DARK_GRAY = (25, 25, 25, 255) +BLACK = (0, 0, 0, 255) + +RED = (255, 0, 0, 255) +GREEN = (0, 255, 0, 255) +BLUE = (0, 0, 255, 255) +ORANGE = (255, 165, 0, 255) +YELLOW = (255, 255, 0, 255) +PURPLE = (128, 0, 128, 255) + +BROWN = (139, 69, 19, 255) +DARK_BROWN = (119, 49, 0, 255) + +DARK_RED = (139, 0, 0, 255) +DARK_GREEN = (0, 100, 0, 255) +DARK_BLUE = (0, 0, 139, 255) +DARK_ORANGE = (255, 140, 0, 255) +DARK_YELLOW = (255, 215, 0, 255) +DARK_PURPLE = (75, 0, 130, 255) + +EFFECT_COLORS = [ + (252, 166, 168, 255), + (247, 203, 168, 255), + (203, 172, 239, 255), + (160, 247, 208, 255), + (222, 244, 141, 255), + (205, 255, 135, 255), + (174, 252, 201, 255), + (247, 167, 111, 255), + (225, 162, 239, 255), + (209, 239, 119, 255), + (211, 255, 178, 255), + (119, 249, 215, 255), + (252, 113, 146, 255), + (204, 247, 160, 255), + (247, 161, 148, 255), + (218, 186, 255, 255), + (112, 239, 116, 255), + (237, 186, 125, 255), + (198, 202, 255, 255), + (197, 252, 174, 255), ] def darken(color, factor=0.5): - r, g, b = color - return (r * factor, g * factor, b * factor) + r, g, b, a = color + return r * factor, g * factor, b * factor, a def lighten(color, factor=0.5): - r, g, b = color - return (min(255, r * (1 + factor)), min(255, g * (1 + factor)), min(255, b * (1 + factor))) + r, g, b, a = color + return min(255, r * (1 + factor)), min(255, g * (1 + factor)), min(255, b * (1 + factor)), a # commands @@ -97,11 +85,6 @@ COMMAND_EXIT = 0 COMMAND_START = 1 COMMAND_MENU = 2 -screen.fill(BROWN) -loading_text = font_large.render("Loading...", True, BLACK) -screen.blit(loading_text, loading_text.get_rect(center=(WIDTH / 2, HEIGHT / 2))) -pygame.display.update() - def lerp(start, end, weight): return weight * (end - start) + start @@ -118,134 +101,19 @@ def rotate(image, angle): def rotate_center(image, angle, position): rotated_image = rotate(image, round(angle)) - new_rect = rotated_image.get_rect(center=image.get_rect(topleft=( - position.x - image.get_rect().width / 2, position.y - image.get_rect().height / 2)).center) + new_rect = rotated_image.get_rect(center=image.get_rect(topleft=(position.x - image.get_rect().width / 2, + position.y - image.get_rect().height / 2)).center) return rotated_image, new_rect.topleft def determine_angle(pos1, pos2): pos1 = pygame.Vector2(pos1) pos2 = pygame.Vector2(pos2) - if pos1.x == pos2.x: pos2.x += 0.0001 - a = math.degrees(math.atan((pos2.y - pos1.y) / (pos2.x - pos1.x))) - - # if pos2.x < pos1.x: - # a += 180 return -a -# def split_image(image, angle, image_position, mouse_position, mouse_direction): -# img = image.copy() -# ip = pygame.Vector2(img.get_width() / 2, img.get_height() / 2) -# mp = mouse_position - ip -# -# if mouse_direction.x == 0: -# a = 90 -# else: -# a = math.degrees(math.atan(mouse_direction.y/mouse_direction.x)) -# -# mp.rotate_ip(-a) -# mp += ip -# -# pygame.draw.line(img, RED,mouse_position-image_position, mouse_direction * 100+ mouse_position-image_position, 20) -# -# rot_img,pos = rotate_center(img, -a, image_position) -# return img, rot_img, (WIDTH/2, HEIGHT/2), (0,0) -# crop_y = clamp(int(mp.y), 0, img.get_height()) -# -# half1 = pygame.transform.rotate(img2.subsurface((0, 0, img.get_width(), crop_y)), a) -# half2 = pygame.transform.rotate(img2.subsurface((0, crop_y, img.get_width(), img.get_height() - crop_y)), a) -# # -# # return half1, half2, half1.get_rect(topleft=pos1).center, half2.get_rect(topleft=pos2).center - -# good one :D -# def split_image(image, angle, image_position, mouse_position, mouse_direction): -# img = image.copy() -# -# if mouse_direction.x == 0: -# mouse_direction.x += 0.0001 -# -# a = math.degrees(math.atan(mouse_direction.y / mouse_direction.x)) -# -# img = rotate_center(img, a, pygame.Vector2(0, 0))[0] -# -# top_left = pygame.Vector2(image_position) - pygame.Vector2(img.get_width() / 2, img.get_height() / 2) -# rot_center = pygame.Vector2(image_position) - top_left -# -# # finding end and start points of the splitting line -# # [x,y] = mouse_position + t * mouse_direction # vector equation -# # x = mouse_position.x + t * mouse_direction.x -# # y = mouse_position.y + t * mouse_direction.y -# -# mp = mouse_position - top_left -# -# t1 = (- mp.x) / mouse_direction.x -# p1 = mp + t1 * mouse_direction -# -# t2 = (img.get_width() - mp.x) / mouse_direction.x -# p2 = mp + t2 * mouse_direction -# -# p3 = (p1 - rot_center).rotate(-a) + rot_center -# p4 = (p2-rot_center).rotate(-a) + rot_center -# -# half1 = img.subsurface(pygame.Rect(0, 0, img.get_width(), clamp(p3.y,0, img.get_height()))) -# half2 = img.subsurface(pygame.Rect(0, clamp(p3.y,0, img.get_height()), img.get_width(), clamp(img.get_height() - p3.y, 0, img.get_height()))) -# -# p5 = half1.get_rect().center - rot_center -# pos1 = (p5).rotate(a) + image_position -# -# p6 = half2.get_rect().center - rot_center + pygame.Vector2(0, clamp(p3.y,0, img.get_height())) -# pos2 = (p6).rotate(a) + image_position -# -# r_half1 = pygame.transform.rotate(half1, -a) -# r_half2 = pygame.transform.rotate(half2, -a) -# -# return r_half1, r_half2, pos1, pos2 - - -# def split_image(image, angle, pos1, pos2, image_position): -# pos1 = pygame.Vector2(pos1) - image_position + pygame.Vector2(image.get_width() / 2, image.get_height() / 2) -# pos2 = pygame.Vector2(pos2) - image_position + pygame.Vector2(image.get_width() / 2, image.get_height() / 2) -# -# if pos1.x == pos2.x: -# pos2.x += 0.0001 -# -# img = image.copy() -# -# center = pygame.Vector2(img.get_width() / 2, img.get_height() / 2) -# -# # pygame.draw.circle(img, BLACK, center, 5) -# # pygame.draw.circle(img, RED, pos1, 15) -# # pygame.draw.circle(img, GREEN, pos2, 15) -# # pygame.draw.line(img, BLACK, pos1, pos2, 5) -# -# a = math.degrees(math.atan((pos2.y - pos1.y) / (pos2.x - pos1.x))) -# img = rotate_center(img, a, pygame.Vector2(0, 0))[0] -# p1 = (pos1 - center).rotate(-a) + img.get_rect().center -# p2 = (pos2 - center).rotate(-a) + img.get_rect().center -# -# # pygame.draw.circle(img, BLACK, center, 5) -# # pygame.draw.circle(img, BLUE, p1, 15) -# # pygame.draw.circle(img, WHITE, p2, 15) -# -# rot_center = pygame.Vector2(img.get_width() / 2, img.get_height() / 2) -# p3 = (-rot_center.copy()).rotate(-a) + center -# p4 = pygame.Vector2(-image.get_width() / 2, 0).rotate(-a) + center -# -# half1 = pygame.transform.rotate(img.subsurface(pygame.Rect(0, 0, img.get_width(), min(p1.y, img.get_height()))), -# -a).convert_alpha() -# half2 = pygame.transform.rotate( -# img.subsurface(pygame.Rect(0, min(p1.y, img.get_height()), img.get_width(), max(img.get_height() - p1.y, 0))), -# -a).convert_alpha() -# # -# if 0 < a < 90: -# p3, p4 = p4, p3 -# -# half1, pos1 = rotate_center(half1, angle, -# p3 + image_position + pygame.Vector2(half1.get_width() / 2, half1.get_height() / 2)) -# half2, pos2 = rotate_center(half2, angle, -# p4 + image_position + pygame.Vector2(half2.get_width() / 2, half2.get_height() / 2)) -# -# return half1, half2, pos1, pos2 + + + diff --git a/test.py b/test.py new file mode 100644 index 0000000..143e804 --- /dev/null +++ b/test.py @@ -0,0 +1,33 @@ +import pygame +from pygame._sdl2 import Window, Renderer, Texture + +class App: + + def __init__(self): + pygame.init() + + self.clock = pygame.time.Clock() + self.display = pygame.display.set_mode((1366, 768)) + self.window = Window.from_display_module() + self.renderer = Renderer(self.window) + self.renderer.draw_color = (255, 0, 0, 255) + + a_surface = pygame.image.load('image.png') + self.an_image = Texture.from_surface(self.renderer, a_surface) + + def run(self): + while True: + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + raise SystemExit + + self.renderer.clear() + self.an_image.draw(None, (20, 20)) + self.renderer.present() + self.clock.tick(60) + + +app = App() +app.run() \ No newline at end of file