2
enemy.py
|
|
@ -16,7 +16,7 @@ class Enemy(pygame.sprite.Sprite):
|
|||
random.uniform(0, SCREEN_HEIGHT),
|
||||
)
|
||||
)
|
||||
self.speed = random.uniform(1,2)
|
||||
self.speed = random.uniform(4,10)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
115
main.py
|
|
@ -14,6 +14,7 @@ from pygame.locals import (
|
|||
QUIT,
|
||||
K_SPACE,
|
||||
K_f,
|
||||
K_RETURN,
|
||||
)
|
||||
|
||||
pygame.mixer.init()
|
||||
|
|
@ -23,99 +24,155 @@ pygame.font.init()
|
|||
my_font = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
|
||||
pygame.mixer.music.load("sounds/aura.mp3")
|
||||
pygame.mixer.music.play(loops=-1)
|
||||
|
||||
collision_sound = pygame.mixer.Sound("sounds/dead.ogg")
|
||||
|
||||
|
||||
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
ADDENEMY = pygame.USEREVENT + 1
|
||||
pygame.time.set_timer(ADDENEMY, 1000)
|
||||
|
||||
running = True
|
||||
game_started = False
|
||||
|
||||
# Initialize player, enemy, and sprite groups
|
||||
player = Player()
|
||||
|
||||
enemy = Enemy()
|
||||
|
||||
enemies = pygame.sprite.Group()
|
||||
all_sprites = pygame.sprite.Group()
|
||||
|
||||
all_sprites.add(player)
|
||||
all_sprites.add(enemy)
|
||||
|
||||
enemies.add(enemy)
|
||||
|
||||
bg =pygame.image.load("sprites/Graveyard.png").convert()
|
||||
bg1 = pygame.transform.scale(pygame.image.load("sprites/Day.city.png").convert(), (SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
while Start:
|
||||
# Start screen loop
|
||||
while not game_started:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.type==K_SPACE:
|
||||
Start = False
|
||||
if event.key == K_SPACE:
|
||||
game_started = True # Start the game when Space is pressed
|
||||
if event.key == K_f:
|
||||
Start = False
|
||||
game_started = True # Start the game when F is pressed (optional)
|
||||
elif event.type == QUIT:
|
||||
Start = False
|
||||
running = False
|
||||
game_started = True # Exit the loop if quit event occurs
|
||||
|
||||
screen.fill((0, 0, 0))
|
||||
text = my_font.render("Start", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT-text.get_height())/2
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2,
|
||||
))
|
||||
text = my_font.render("Press Space", True, (255,255,255))
|
||||
text = my_font.render("Press Space to Start", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH-text.get_width())/2
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height() - 200)
|
||||
))
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(1000)
|
||||
clock.tick(30) # Limit FPS to 30
|
||||
|
||||
pygame.mixer.music.play(loops=-1, fade_ms=1000)
|
||||
|
||||
|
||||
# Main game loop
|
||||
while running:
|
||||
screen.blit(bg, (0,0))
|
||||
screen.fill((0, 0, 0)) # Background color
|
||||
screen.blit(bg1, (0, 0)) # Draw the background image
|
||||
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_ESCAPE:
|
||||
running = False
|
||||
running = False # Quit on Escape
|
||||
elif event.key == K_RETURN: # Press Enter to restart
|
||||
# Reset the game state
|
||||
player.kill() # Remove player from all_sprites
|
||||
enemy.kill() # Remove enemy from all_sprites
|
||||
enemies.empty() # Empty enemies group
|
||||
all_sprites.empty() # Empty all_sprites group
|
||||
|
||||
# Reinitialize player, enemies, and all sprites
|
||||
player = Player()
|
||||
enemy = Enemy()
|
||||
enemies.add(enemy)
|
||||
all_sprites.add(player)
|
||||
all_sprites.add(enemy)
|
||||
|
||||
# Reset background and re-enter game loop
|
||||
continue # Restart game loop from the beginning
|
||||
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
running = False # Quit on window close
|
||||
|
||||
elif event.type == ADDENEMY:
|
||||
# Add a new enemy each second
|
||||
new_enemy = Enemy()
|
||||
enemies.add(new_enemy)
|
||||
all_sprites.add(new_enemy)
|
||||
|
||||
|
||||
pressed_keys = pygame.key.get_pressed()
|
||||
|
||||
player.update(pressed_keys)
|
||||
|
||||
enemies.update()
|
||||
|
||||
# Render all sprites
|
||||
for entity in all_sprites:
|
||||
screen.blit(entity.surf, entity.rect)
|
||||
|
||||
# Check for collisions between player and enemies
|
||||
if pygame.sprite.spritecollideany(player, enemies):
|
||||
player.kill() # Remove player when it collides with an enemy
|
||||
pygame.time.delay(250) # Wait a bit before showing the game over screen
|
||||
|
||||
pygame.mixer.stop
|
||||
|
||||
player.kill()
|
||||
collision_sound.play()
|
||||
pygame.time.delay(1000)
|
||||
running=False
|
||||
# Game Over screen after collision
|
||||
screen.fill((0, 0, 0)) # Clear the screen with a black background
|
||||
|
||||
text =my_font.render("Ya dead yet?", True, (255,255,0))
|
||||
screen.blit(text, (SCREEN_WIDTH-text.get_width(), 0))
|
||||
text = my_font.render("Game Over", True, (255, 0, 0))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2
|
||||
))
|
||||
|
||||
text = my_font.render("Press Enter to Restart", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2 + 50,
|
||||
))
|
||||
|
||||
text = my_font.render("Press Escape to Quit", True, (255, 255, 255))
|
||||
screen.blit(text, (
|
||||
(SCREEN_WIDTH - text.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - text.get_height()) / 2 +100,
|
||||
))
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
clock.tick(5050)
|
||||
# Wait for the player to press Enter to restart or Escape to quit
|
||||
waiting_for_input = True
|
||||
while waiting_for_input:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if event.key == K_RETURN:
|
||||
waiting_for_input = False # Player pressed Enter to restart
|
||||
if event.key == K_ESCAPE:
|
||||
running = False # Player pressed Escape to quit
|
||||
waiting_for_input = False
|
||||
elif event.type == QUIT:
|
||||
waiting_for_input = False
|
||||
running = False
|
||||
|
||||
pygame.display.flip() # Update the screen
|
||||
|
||||
clock.tick(60) # Limit FPS to 60
|
||||
|
||||
pygame.mixer.music.stop()
|
||||
pygame.mixer.quit()
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
|
|
|||
|
|
@ -23,13 +23,13 @@ class Player(pygame.sprite.Sprite):
|
|||
#self.surf = pygame.Surface((40, 40))
|
||||
def update(self, pressed_keys):
|
||||
if pressed_keys[K_UP]:
|
||||
self.rect.move_ip(0, -1)
|
||||
self.rect.move_ip(0, -4)
|
||||
if pressed_keys[K_DOWN]:
|
||||
self.rect.move_ip(0, 1)
|
||||
self.rect.move_ip(0, 4)
|
||||
if pressed_keys[K_LEFT]:
|
||||
self.rect.move_ip(-1, 0)
|
||||
self.rect.move_ip(-4, 0)
|
||||
if pressed_keys[K_RIGHT]:
|
||||
self.rect.move_ip(1, 0)
|
||||
self.rect.move_ip(4, 0)
|
||||
|
||||
if self.rect.left < 0:
|
||||
self.rect.left = 0
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
SCREEN_WIDTH = 800
|
||||
SCREEN_HEIGHT = 600
|
||||
SCREEN_WIDTH = 1920
|
||||
SCREEN_HEIGHT = 1080
|
||||
|
After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 527 B After Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B After Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B After Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 527 B After Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B After Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B After Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 527 B After Width: | Height: | Size: 527 B |
|
Before Width: | Height: | Size: 517 B After Width: | Height: | Size: 517 B |
|
Before Width: | Height: | Size: 519 B After Width: | Height: | Size: 519 B |
|
Before Width: | Height: | Size: 522 B After Width: | Height: | Size: 522 B |
|
Before Width: | Height: | Size: 600 B After Width: | Height: | Size: 600 B |
|
Before Width: | Height: | Size: 590 B After Width: | Height: | Size: 590 B |
|
Before Width: | Height: | Size: 594 B After Width: | Height: | Size: 594 B |
|
Before Width: | Height: | Size: 585 B After Width: | Height: | Size: 585 B |