Compare commits

..

No commits in common. "db5839361b41e5ae6dd6aa60c753ef604024ad76" and "7f68c1fc9e31d403c519bd058c3664d36190215f" have entirely different histories.

4 changed files with 80 additions and 94 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

156
main.py
View File

@ -6,43 +6,19 @@ CLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((1000, 800)) SCREEN = pygame.display.set_mode((1000, 800))
BLACK = (0,0,0) BLACK = (0,0,0)
BACKGROUND = pygame.image.load("images/back.png") BACKGROUND = pygame.image.load("images/back.png")
BACK_MAIN = pygame.image.load("images/back_main.jpg")
BACK_MAIN = pygame.transform.scale(BACK_MAIN, (1000, 800)).convert_alpha()
level = 0
G_X, G_Y = 100, 800
class Goblin: G_GRAVITY = 1
def __init__(self): G_JUMP_HEIGHT = 20
self.x = 100 g_y_velocity = 20
self.y = 800 g_jumping = False
self.gravity = 1
self.jump_height = 20
self.y_velocity = 20
self.jumping = False
self.start_x = 100
self.start_y = 800
def reset_position(self):
self.x = self.start_x
self.y = self.start_y
g_facing_right = True g_facing_right = True
class Knight: K_X, K_Y = 300, 700
def __init__(self): K_GRAVITY = 1
self.x = 300 K_JUMP_HEIGHT = 20
self.y = 700 k_y_velocity = 20
self.gravity = 1 k_jumping = False
self.jump_height = 20
self.y_velocity = 20
self.jumping = False
self.start_x = 300
self.start_y = 700
def reset_position(self):
self.x = self.start_x
self.y = self.start_y
k_facing_right = True k_facing_right = True
def get_image(sheet,frame_w, frame_h, width, height, scale, color): def get_image(sheet,frame_w, frame_h, width, height, scale, color):
@ -82,9 +58,6 @@ wall_left = pygame.Rect(0, 0, 1, 800)
wall_bottom = pygame.Rect(0, 800, 1000, 1) wall_bottom = pygame.Rect(0, 800, 1000, 1)
wall_up = pygame.Rect(0, 0, 1000, 1) wall_up = pygame.Rect(0, 0, 1000, 1)
#BUTTONS
button_play = pygame.image.load("images/play_button.png")
button_play_rect = button_play.get_rect(center=(500, 400))
#PLATFORM #PLATFORM
platform_image = pygame.image.load("images/sprites/platforms.png") platform_image = pygame.image.load("images/sprites/platforms.png")
platform = get_image(platform_image, 0, 0, 16, 9, 3, BLACK) platform = get_image(platform_image, 0, 0, 16, 9, 3, BLACK)
@ -103,10 +76,9 @@ while True:
sys.exit() sys.exit()
keys_pressed = pygame.key.get_pressed() keys_pressed = pygame.key.get_pressed()
mouse_pos = pygame.mouse.get_pos()
SCREEN.fill((0,0,0))
prev_G_Y = G_Y
prev_K_Y = K_Y
g_moving = False g_moving = False
if keys_pressed[pygame.K_d]: if keys_pressed[pygame.K_d]:
@ -121,6 +93,7 @@ while True:
g_jumping = True g_jumping = True
g_y_velocity = G_JUMP_HEIGHT g_y_velocity = G_JUMP_HEIGHT
k_moving = False k_moving = False
if keys_pressed[pygame.K_KP6]: if keys_pressed[pygame.K_KP6]:
K_X += 5 K_X += 5
@ -134,30 +107,28 @@ while True:
k_jumping = True k_jumping = True
k_y_velocity = K_JUMP_HEIGHT k_y_velocity = K_JUMP_HEIGHT
# Goblin
g_y_velocity -= G_GRAVITY SCREEN.blit(wall_image, wall_right)
SCREEN.blit(wall_image, wall_left)
SCREEN.blit(wall_image, wall_up)
SCREEN.blit(wall_image, wall_bottom)
SCREEN.blit(BACKGROUND, (0, 0))
SCREEN.blit(door_image, door_rect)
SCREEN.blit(platform, platform_rect)
if g_jumping:
G_Y -= g_y_velocity G_Y -= g_y_velocity
g_y_velocity -= G_GRAVITY
# Knight if g_y_velocity < -G_JUMP_HEIGHT:
k_y_velocity -= K_GRAVITY
K_Y -= k_y_velocity
if G_X < 0: G_X = 0
if G_X > 1000: G_X = 1000
if K_X < 0: K_X = 0
if K_X > 1000: K_X = 1000
# Пол
if G_Y >= 800:
G_Y = 800
g_y_velocity = 0
g_jumping = False g_jumping = False
g_y_velocity = G_JUMP_HEIGHT
if K_Y >= 800: if k_jumping:
K_Y = 800 K_Y -= k_y_velocity
k_y_velocity = 0 k_y_velocity -= K_GRAVITY
if k_y_velocity < -K_JUMP_HEIGHT:
k_jumping = False k_jumping = False
k_y_velocity = K_JUMP_HEIGHT
g_anim_counter = g_anim_counter + 1 if g_moving else 0 g_anim_counter = g_anim_counter + 1 if g_moving else 0
k_anim_counter = k_anim_counter + 1 if k_moving else 0 k_anim_counter = k_anim_counter + 1 if k_moving else 0
@ -171,11 +142,11 @@ while True:
g_surface = STANDING_SURFACE g_surface = STANDING_SURFACE
if not g_facing_right: if not g_facing_right:
g_surface = pygame.transform.flip(g_surface, True, False) g_surface = pygame.transform.flip(g_surface, True, False).convert_alpha()
g_surface.set_colorkey(BLACK)
g_rect = g_surface.get_rect(center=(G_X, G_Y)) g_rect = g_surface.get_rect(center=(G_X, G_Y))
if k_jumping: if k_jumping:
up_thresh = K_JUMP_HEIGHT // 3 up_thresh = K_JUMP_HEIGHT // 3
down_thresh = -up_thresh down_thresh = -up_thresh
@ -192,44 +163,59 @@ while True:
k_surface = K_STAND k_surface = K_STAND
if not k_facing_right: if not k_facing_right:
k_surface = pygame.transform.flip(k_surface, True, False) k_surface = pygame.transform.flip(k_surface, True, False).convert_alpha()
k_surface.set_colorkey(BLACK)
k_rect = k_surface.get_rect(center=(K_X, K_Y)) k_rect = k_surface.get_rect(center=(K_X, K_Y))
if level == 0: if g_rect.colliderect(wall_right):
SCREEN.blit(BACK_MAIN, (0, 0)) G_X -= 5
SCREEN.blit(button_play, button_play_rect) if g_rect.colliderect(wall_left):
G_X += 5
if g_rect.colliderect(wall_bottom):
G_Y -= 5
if g_rect.colliderect(wall_up):
G_Y += 5
if k_rect.colliderect(wall_right):
K_X -= 5
if k_rect.colliderect(wall_left):
K_X += 5
if button_play_rect.collidepoint(mouse_pos) and pygame.mouse.get_pressed()[0]:
level = 1
if level == 1:
SCREEN.blit(BACKGROUND, (0, 0))
SCREEN.blit(door_image, door_rect)
SCREEN.blit(platform, platform_rect)
SCREEN.blit(g_surface, g_rect)
SCREEN.blit(k_surface, k_rect)
# Goblin # Goblin
if g_rect.colliderect(platform_rect) and g_y_velocity <= 0: if g_rect.colliderect(platform_rect):
G_Y = platform_rect.top - g_rect.height / 2 if g_y_velocity < 0:
g_y_velocity = 0 G_Y = platform_rect.top - (g_rect.height / 2)
g_jumping = False g_jumping = False
g_y_velocity = 0
else:
G_Y = platform_rect.bottom + (g_rect.height / 2)
g_y_velocity = 0
else:
pass
# Knight # Knight
if k_rect.colliderect(platform_rect) and k_y_velocity <= 0: if k_rect.colliderect(platform_rect):
K_Y = platform_rect.top - k_rect.height / 2 if k_y_velocity < 0:
k_y_velocity = 0 K_Y = platform_rect.top - (k_rect.height / 2)
k_jumping = False k_jumping = False
k_y_velocity = 0
else:
K_Y = platform_rect.bottom + (k_rect.height / 2)
k_y_velocity = 0
else:
pass
SCREEN.blit(g_surface, g_rect)
SCREEN.blit(k_surface, k_rect)
if g_rect.colliderect(door_rect) and keys_pressed[pygame.K_e]: if g_rect.colliderect(door_rect) and keys_pressed[pygame.K_e]:
print("Goblin wins!") print("Goblin wins!")
level = 0 pygame.quit()
sys.exit()
if k_rect.colliderect(door_rect) and keys_pressed[pygame.K_KP5]: if k_rect.colliderect(door_rect) and keys_pressed[pygame.K_KP5]:
print("Knight wins!") print("Knight wins!")
level = 0 pygame.quit()
pygame.display.update() sys.exit()
pygame.display.update() pygame.display.update()
CLOCK.tick(60) CLOCK.tick(60)