Untitled
Anonymous
plain_text
01/27/2026 4:30 PM
9.7 KB
11
Indexable
from pygame import *
from random import randint
mixer.init()
font.init()
WIDTH = 800
HEIGHT = 600
FPS = 60
window = display.set_mode((WIDTH, HEIGHT))
display.set_caption("Reflect Attack")
clock = time.Clock()
def start_screen():
start = transform.scale(image.load('images/start.png'), (200, 70))
start_rect = start.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 50))
window.blit(start, start_rect)
exit = transform.scale(image.load('images/exit.png'), (200, 70))
exit_rect = exit.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
window.blit(exit, exit_rect)
return start_rect, exit_rect
start_rect, stop_rect = start_screen()
class GameSprite(sprite.Sprite):
def __init__(self, picture, x, y, w, h):
super().__init__()
self.image = transform.scale(image.load(picture), (w, h)).convert_alpha()
self.rect = self.image.get_rect(topleft=(x, y))
def draw(self, window, camera):
window.blit(self.image, (self.rect.x - camera.x, self.rect.y - camera.y))
class Player(GameSprite):
def __init__(self, picture, x, y, w, h, speed):
super().__init__(picture, x, y, w, h)
self.speed = speed
self.rotation = 0
self.pick_ind = 0
self.facing_left = False
self.health = 100
self.attacking = False
self.reflect_rect = self.rect.copy()
self.load_animations()
def load_animations(self, path="images/Warrior/"):
self.stay_right = [transform.scale(image.load(f"{path}{i}.png"), (self.rect.width, self.rect.height)).convert_alpha() for i in range(1, 11)]
self.stay_left = [transform.flip(p, True, False) for p in self.stay_right]
self.move_right = [transform.scale(image.load(f"{path}{i}.png"), (self.rect.width, self.rect.height)).convert_alpha() for i in range(11, 19)]
self.move_left = [transform.flip(p, True, False) for p in self.move_right]
self.atk_right = [transform.scale(image.load(f"{path}{i}.png"), (self.rect.width, self.rect.height)).convert_alpha() for i in range(41, 48)]
self.atk_left = [transform.flip(p, True, False) for p in self.atk_right]
def animation(self):
if self.facing_left:
self.reflect_rect = Rect(self.rect.x - 20, self.rect.y, self.rect.width // 2, self.rect.height)
else:
self.reflect_rect = Rect(self.rect.x + self.rect.width // 2, self.rect.y, self.rect.width // 2, self.rect.height)
if self.rotation == 0:
self.image = self.stay_right[self.pick_ind // 6]
elif self.rotation == 1:
self.image = self.stay_left[self.pick_ind // 6]
elif self.rotation == 2:
self.image = self.move_right[self.pick_ind // 7]
self.rect.x += self.speed
elif self.rotation == 3:
self.image = self.move_left[self.pick_ind // 7]
self.rect.x -= self.speed
elif self.rotation == 4:
self.image = self.atk_left[self.pick_ind // 4] if self.facing_left else self.atk_right[self.pick_ind // 4]
if self.rotation == 4:
if self.pick_ind < len(self.atk_right) * 4 - 1:
self.pick_ind += 1
else:
self.pick_ind = 0
self.rotation = 0
self.attacking = False
else:
self.pick_ind = (self.pick_ind + 1) % 54
def attack(self):
if not self.attacking:
self.rotation = 4
self.pick_ind = 0
self.attacking = True
class Enemy(GameSprite):
def __init__(self, picture, x, y):
super().__init__(picture, x, y, 120, 120)
self.health = 50
def update(self):
if self.rect.x < 7500:
self.rect.x += 1
def fire(self):
return Bullet('images/Enemy/bullet.png', self.rect.left, self.rect.centery - 10, 40, 20, randint(4, 7))
class Bullet(GameSprite):
def __init__(self, picture, x, y, w, h, speed):
super().__init__(picture, x, y, w, h)
self.speed = speed
self.direction = -1
def update(self):
self.rect.x += self.speed * self.direction
if self.rect.right < 0 or self.rect.left > bg_width:
self.kill()
bg_menu = transform.scale(image.load('images/bg_menu.jpeg'), (WIDTH, HEIGHT))
bg = transform.scale(image.load('images/background.png'), (8000, HEIGHT))
bg_width, bg_height = bg.get_size()
menu_music = mixer.Sound('sounds/menu_music.wav')
menu_music.play(-1)
game_music = mixer.Sound('sounds/music.wav')
hero = Player("images/Warrior/1.png", 380, 380, 100, 120, 3)
enemy = Enemy('images/Enemy/enemy.png', 1200, 380)
bullets = sprite.Group()
ENEMY_FIRE = USEREVENT + 1
time.set_timer(ENEMY_FIRE, 3000)
camera = Rect(0, 0, WIDTH, HEIGHT)
run = True
main_menu = True
game_start = False
while run:
clock.tick(FPS)
for e in event.get():
if e.type == QUIT:
run = False
if main_menu and e.type == MOUSEBUTTONDOWN:
if start_rect.collidepoint(e.pos):
main_menu = False
game_start = True
menu_music.stop()
game_music.play(-1)
if game_start:
if e.type == ENEMY_FIRE:
bullets.add(enemy.fire())
if e.type == KEYDOWN and e.key == K_SPACE:
hero.attack()
if main_menu:
window.blit(bg_menu, (0, 0))
start_screen()
if game_start:
keys = key.get_pressed()
if not hero.attacking:
if keys[K_d]:
hero.rotation = 2
hero.facing_left = False
elif keys[K_a]:
hero.rotation = 3
hero.facing_left = True
else:
hero.rotation = 1 if hero.facing_left else 0
camera.x = hero.rect.x - WIDTH // 2
camera.x = max(0, min(camera.x, bg_width - WIDTH))
window.blit(bg, (-camera.x, 0))
hero.animation()
enemy.update()
bullets.update()
hero.draw(window, camera)
enemy.draw(window, camera)
for bullet in list(bullets):
if hero.attacking and hero.reflect_rect.colliderect(bullet.rect):
bullet.direction = 1
elif bullet.rect.colliderect(enemy.rect) and bullet.direction == 1:
enemy.health -= 10
bullet.kill()
if enemy.health <= 0:
enemy.kill()
elif bullet.rect.colliderect(hero.rect):
hero.health -= 10
bullet.kill()
bullet.draw(window, camera)
hp_bar_width = 200
hp_bar_height = 16
hp_x = 10
hp_y = 10
draw.rect(
window,
(50, 50, 50),
(hp_x - 2, hp_y - 2, hp_bar_width + 4, hp_bar_height + 4)
)
hp_fill = max(0, hero.health) / 100 * hp_bar_width
draw.rect(
window,
(200, 0, 0),
(hp_x, hp_y, hp_fill, hp_bar_height)
)
font_t = font.Font(None, 20)
hp_text = font_t.render(f'HP: {max(0, hero.health)}', True, (255, 255, 255))
window.blit(hp_text, (hp_x + hp_bar_width + 8, hp_y - 2))
display.flip()
quit()
Editor is loading...
Leave a Comment