Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- import random
- pygame.init()
- width = 928
- height = 600
- clock = pygame.time.Clock()
- FPS = 60
- angle1 = 0
- angle2 = 0
- center_x1, center_y1 = 454, 150
- center_x2, center_y2 = 454, 380
- center_x3, center_y3 = 860, 570
- loop_width1 = 35
- loop_height1 = 10
- loop_width2 = 20
- loop_height2 = 8
- intro_count = 3
- last_count = pygame.time.get_ticks()
- score = [0, 0]
- roundover = False
- roundover_cooldown = 3000
- screen = pygame.display.set_mode((width, height))
- pygame.display.set_caption('FIGHTER')
- bg_img = pygame.image.load('assets/bg.png').convert_alpha()
- menu_img = pygame.image.load('assets/menu.png').convert_alpha()
- txt_img = pygame.image.load('assets/txt.png').convert_alpha()
- play_img = pygame.image.load('assets/play.png').convert_alpha()
- quit_img = pygame.image.load('assets/quit.png').convert_alpha()
- p1_sprite_sheet = pygame.image.load('assets/sprites1.png').convert_alpha()
- p2_sprite_sheet = pygame.image.load('assets/sprites2.png').convert_alpha()
- p3_sprite_sheet = pygame.image.load('assets/sprites3.png').convert_alpha()
- p4_sprite_sheet = pygame.image.load('assets/sprites4.png').convert_alpha()
- p5_sprite_sheet = pygame.image.load('assets/sprites5.png').convert_alpha()
- bgm = pygame.mixer.Sound('music/bgm.mp3')
- bgm.set_volume(100)
- fight_fx = pygame.mixer.Sound('music/fight.mp3')
- fight_fx.set_volume(0.5)
- hit1_fx = pygame.mixer.Sound('music/hit1.mp3')
- hit2_fx = pygame.mixer.Sound('music/hit2.mp3')
- sword1_fx = pygame.mixer.Sound('music/sword1.mp3')
- sword2_fx = pygame.mixer.Sound('music/sword2.mp3')
- miss1_fx = pygame.mixer.Sound('music/miss1.mp3')
- miss2_fx = pygame.mixer.Sound('music/miss2.mp3')
- font1 = pygame.font.Font('assets/font.otf', 80)
- font2 = pygame.font.Font('assets/font.otf', 40)
- p1_anim_steps = [8, 3, 3, 10, 3, 7, 8, 7]
- p2_anim_steps = [8, 3, 2, 8, 2, 7, 8, 8]
- p3_anim_steps = [8, 3, 3, 10, 3, 11, 7, 6]
- p4_anim_steps = [8, 3, 2, 4, 2, 7, 4, 4]
- p5_anim_steps = [8, 4, 2, 8, 2, 6, 6, 6]
- p1_size = 162
- p1_scale = 4
- p1_offset = [72, 60]
- p1_data = [p1_size, p1_scale, p1_offset]
- p2_size = 250
- p2_scale = 3
- p2_offset = [108, 113]
- p2_data = [p2_size, p2_scale, p2_offset]
- p3_size = 300
- p3_scale = 4.5
- p3_offset = [108, 113]
- p3_data = [p3_size, p3_scale, p3_offset]
- p4_size = 200
- p4_scale = 4
- p4_offset = [108, 113]
- p4_data = [p4_size, p4_scale, p4_offset]
- p5_size = 200
- p5_scale = 4
- p5_offset = [108, 113]
- p5_data = [p5_size, p5_scale, p5_offset]
- def draw_bg():
- scaled_bg = pygame.transform.scale(bg_img, (width, height))
- screen.blit(scaled_bg, (0, 0))
- def draw_text(text, font, color, x, y):
- txt_surf = font.render(text, True, color)
- screen.blit(txt_surf, (x, y))
- def draw_healthbars(health, x, y):
- ratio = health / 100
- pygame.draw.rect(screen, (200, 200, 200), (x - 2, y - 2, 398, 48))
- pygame.draw.rect(screen, (200, 0, 0), (x, y, 394, 44))
- pygame.draw.rect(screen, (0, 200, 0), (x, y, 394 * ratio, 44))
- def draw_menu():
- screen.blit(menu_img, (0, 0))
- class Fighter:
- def __init__(self, player, x, y, l, b, flip, data, sheet, anim_steps):
- self.player = player
- self.rect = pygame.Rect((x, y, l, b))
- self.vel_y = 0
- self.jumping = False
- self.attacking = False
- self.running = False
- self.hit = False
- self.alive = True
- self.attack_type = 0
- self.attack_cooldown = 0
- self.health = 100
- self.flip = flip
- self.size = data[0]
- self.img_scale = data[1]
- self.offset = data[2]
- self.anim_list = self.load_images(sheet, anim_steps)
- self.action = 3
- self.frame_index = 0
- self.image = self.anim_list[self.action][self.frame_index]
- self.update = pygame.time.get_ticks()
- def load_images(self, sheet, anim_steps):
- anim_list = []
- for y, anim in enumerate(anim_steps):
- temp_list = []
- for x in range(anim):
- temp_img = sheet.subsurface(x * self.size, y * self.size, self.size, self.size)
- temp_list.append(pygame.transform.scale(temp_img, (self.size * self.img_scale, self.size * self.img_scale)))
- anim_list.append(temp_list)
- return anim_list
- def draw(self, surf):
- flipped_img = pygame.transform.flip(self.image, self.flip, False)
- surf.blit(flipped_img, (self.rect.x - (self.offset[0] * self.img_scale),
- self.rect.y - (self.offset[1] * self.img_scale)))
- def move(self, scr_width, scr_height, target, round_over):
- vel = 10
- gravity = 2
- dx = 0
- dy = 0
- self.running = False
- self.attack_type = 0
- keys = pygame.key.get_pressed()
- if not self.attacking and self.alive and not round_over:
- if self.player == 1:
- if keys[pygame.K_LEFT]:
- dx = -vel
- self.running = True
- elif keys[pygame.K_RIGHT]:
- dx = vel
- self.running = True
- if keys[pygame.K_UP] and not self.jumping:
- self.vel_y = -40
- self.jumping = True
- if keys[pygame.K_RCTRL] or keys[pygame.K_RSHIFT]:
- self.attack(target)
- if keys[pygame.K_RCTRL]:
- self.attack_type = 1
- elif keys[pygame.K_RSHIFT]:
- self.attack_type = 2
- elif self.player == 2:
- if keys[pygame.K_a]:
- dx = -vel
- self.running = True
- elif keys[pygame.K_d]:
- dx = vel
- self.running = True
- if keys[pygame.K_w] and not self.jumping:
- self.vel_y = -40
- self.jumping = True
- if keys[pygame.K_LSHIFT] or keys[pygame.K_f]:
- self.attack(target)
- if keys[pygame.K_LSHIFT]:
- self.attack_type = 1
- elif keys[pygame.K_f]:
- self.attack_type = 2
- self.vel_y += gravity
- dy += self.vel_y
- if self.rect.left + dx < 0:
- dx = -self.rect.left
- elif self.rect.right + dx > scr_width:
- dx = scr_width - self.rect.right
- if target.rect.centerx > self.rect.centerx:
- self.flip = False
- else:
- self.flip = True
- if self.rect.bottom + dy > scr_height - 60:
- self.vel_y = 0
- self.jumping = False
- dy = scr_height - 60 - self.rect.bottom
- self.rect.y += dy
- self.rect.x += dx
- if self.attack_cooldown > 0:
- self.attack_cooldown -= 1
- def animation(self):
- if self.health <= 0:
- self.health = 0
- self.update_action(5)
- elif self.hit:
- self.update_action(1)
- elif self.attacking:
- if self.attack_type == 1:
- self.update_action(6)
- elif self.attack_type == 2:
- self.update_action(7)
- elif self.jumping:
- self.update_action(2)
- elif self.running:
- self.update_action(0)
- else:
- self.update_action(3)
- anim_cooldown = 60
- self.image = self.anim_list[self.action][self.frame_index]
- if pygame.time.get_ticks() - self.update > anim_cooldown:
- self.frame_index += 1
- self.update = pygame.time.get_ticks()
- if self.frame_index >= len(self.anim_list[self.action]):
- if self.action == 5:
- self.frame_index = len(self.anim_list[self.action]) - 1
- else:
- self.frame_index = 0
- if self.action in [6, 7]:
- self.attacking = False
- self.attack_cooldown = 20
- if self.action == 1:
- self.hit = False
- self.attacking = False
- self.attack_cooldown = 20
- def update_action(self, new_action):
- if new_action != self.action:
- self.action = new_action
- self.frame_index = 0
- self.update = pygame.time.get_ticks()
- def attack(self, target):
- if self.attack_cooldown == 0:
- self.attacking = True
- attacking_rect = pygame.Rect(self.rect.centerx - (2 * self.rect.width * self.flip), self.rect.y, 2 * self.rect.width, self.rect.height)
- if attacking_rect.colliderect(target.rect):
- x_x_ = random.randint(0, 1)
- if x_x_ == 0:
- target.health -= 8
- x_x_ = random.randint(0, 1)
- elif x_x_ == 1:
- target.health -= 10
- x_x_ = random.randint(0, 1)
- target.hit = True
- if self.player == 1:
- _ = random.randint(0, 1)
- if _ == 0:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- sword1_fx.play()
- else:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- sword2_fx.play()
- elif self.player == 2:
- _ = random.randint(0, 1)
- if _ == 0:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- hit1_fx.play()
- else:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- hit2_fx.play()
- if not attacking_rect.colliderect(target.rect):
- _ = random.randint(0, 1)
- if _ == 0:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- miss1_fx.play()
- else:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- miss2_fx.play()
- fighter1 = Fighter(1, 200, 380, 80, 160, False, p1_data, p1_sprite_sheet, p1_anim_steps)
- fighter2 = Fighter(2, 700, 380, 80, 160, True, p2_data, p2_sprite_sheet, p2_anim_steps)
- state = 'menu'
- is_fight_fx_playing = False
- play_rect = play_img.get_rect()
- quit_rect = quit_img.get_rect()
- while True:
- keys = pygame.key.get_pressed()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- if state == 'menu':
- if play_rect.collidepoint(event.pos):
- state = 'play'
- fighter1 = Fighter(1, 200, 380, 80, 160, False, p1_data, p1_sprite_sheet, p1_anim_steps)
- fighter2 = Fighter(2, 700, 380, 80, 160, True, p2_data, p2_sprite_sheet, p2_anim_steps)
- elif quit_rect.collidepoint(event.pos):
- pygame.quit()
- exit()
- if state == 'menu':
- draw_menu()
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- bgm.play(loops=-1)
- angle1 += 0.03125
- angle2 -= 0.03125
- x1 = center_x1 + loop_width1 * math.sin(angle1)
- y1 = center_y1 + loop_height1 * math.sin(2 * angle1)
- x2 = center_x2 + loop_width2 * math.sin(angle2)
- y2 = center_y2 + loop_height2 * math.sin(2 * angle2)
- txt_rect = txt_img.get_rect(center=(x1, y1))
- play_rect = play_img.get_rect(center=(x2, y2 - 110))
- quit_rect = quit_img.get_rect(center=(x2, y2))
- screen.blit(play_img, play_rect)
- screen.blit(quit_img, quit_rect)
- screen.blit(txt_img, txt_rect)
- elif state == 'play':
- draw_bg()
- bgm.stop()
- angle2 -= 0.03125
- x3 = center_x3 + loop_width2 * math.sin(angle2)
- y3 = center_y3 + loop_height2 * math.sin(2 * angle2)
- scaled_quit = pygame.transform.scale(quit_img, (110, 40))
- quit_rect_ = scaled_quit.get_rect(center=(x3, y3))
- screen.blit(scaled_quit, quit_rect_)
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- if quit_rect_.collidepoint(event.pos):
- pygame.quit()
- exit()
- draw_healthbars(fighter1.health, 6, 12)
- draw_healthbars(fighter2.health, 532, 12)
- draw_text(str(score[0]), font2, (230, 230, 230), 400, 10)
- draw_text(str(score[1]), font2, (230, 230, 230), 490, 10)
- fighter1.animation()
- fighter2.animation()
- fighter1.draw(screen)
- fighter2.draw(screen)
- if not roundover:
- if fighter1.health <= 0:
- score[1] += 1
- roundover = True
- roundover_time = pygame.time.get_ticks()
- elif fighter2.health <= 0:
- score[0] += 1
- roundover = True
- roundover_time = pygame.time.get_ticks()
- elif roundover:
- victory = font1.render("VICTORY", True, (220, 0, 0))
- screen.blit(victory, (200, height / 3))
- if pygame.time.get_ticks() - roundover_time > roundover_cooldown:
- intro_count = 3
- roundover = False
- is_fight_fx_playing = False
- fighter1 = Fighter(1, 200, 380, 80, 160, False, p1_data, p1_sprite_sheet, p1_anim_steps)
- fighter2 = Fighter(2, 700, 380, 80, 160, True, p2_data, p2_sprite_sheet, p2_anim_steps)
- if intro_count <= 0:
- if not pygame.mixer.get_busy():
- pygame.mixer.stop()
- if not is_fight_fx_playing:
- fight_fx.play()
- is_fight_fx_playing = True
- fighter1.move(width, height, fighter2, roundover)
- fighter2.move(width, height, fighter1, roundover)
- else:
- draw_text(str(intro_count), font1, (200, 200, 200), width / 2, height / 3)
- if (pygame.time.get_ticks() - last_count) >= 1000:
- intro_count -= 1
- last_count = pygame.time.get_ticks()
- pygame.display.update()
- clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement