I'm making a top-down dungeon minigame where you are a knight and can attack zombies by swinging a sword.
So far I haven't implemented collision, player rect()
, enemy rect()
, a game over screen, or the player sword animation, but all the code I have so far is working well, even though the main loop is crowded.
I was wondering how I could either condense the code or streamline it?
import pygame, random, os
from pygame.locals import *
pygame.init()
scr_width = 1020
scr_height = 510
screen = pygame.display.set_mode((scr_width, scr_height))
pygame.display.set_caption('Dungeon Minigame')
clock = pygame.time.Clock()
images = {}
path = 'Desktop/Files/Dungeon Minigame/'
filenames = [f for f in os.listdir(path) if f.endswith('.png')]
for name in filenames:
imagename = os.path.splitext(name)[0]
images[imagename] = pygame.image.load(os.path.join(path, name))
font = pygame.font.SysFont('Times_New_Roman', 27)
white = [240, 240, 240]
def main_menu():
onclick = False
while True:
mx, my = pygame.mouse.get_pos()
screen.blit(images['background'], (0, 0))
button = screen.blit(images['button'], (480, 300))
if button.collidepoint((mx, my)):
if onclick:
game()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
onclick = True
pygame.display.update()
clock.tick(60)
def game():
lives = 3
score = 0
def player(x, y):
screen.blit(images['r_knight'], (playerX, playerY))
class Enemy:
def __init__(self):
self.x = random.randint(8, 800)
self.y = random.randint(8, 440)
self.moveX = 0
self.moveY = 0
def move(self, speed = 1):
if self.x > playerX:
self.x -= speed
elif self.x < playerX:
self.x += speed
if self.y < playerY:
self.y += speed
elif self.y > playerY:
self.y -= speed
def draw(self):
screen.blit(images['r_zombie'], (self.x, self.y))
def enemy(x, y):
screen.blit(images['r_zombie'], (x, y))
enemy_list = []
for i in range(4):
new_enemy = Enemy()
enemy_list.append(new_enemy)
playerX = 510
playerY = 220
while True:
screen.blit(images['background'], (0, 0))
score_text = font.render('Score: ' + str(score), True, white)
lives_text = font.render('Lives: ', True, white)
screen.blit(score_text, (20, 20))
screen.blit(lives_text, (840, 20))
screen.blit(images['r_knight'], (playerX, playerY))
if lives == 3:
screen.blit(images['triple_heart'], (920, 0))
if lives == 2:
screen.blit(images['double_heart'], (920, 0))
if lives == 1:
screen.blit(images['single_heart'], (920, 0))
if lives <= 0:
screen.blit(images['triple_empty_heart'], (920, 0))
if lives < 0:
lives = 0
onpress = pygame.key.get_pressed()
if onpress[pygame.K_a]:
playerX -= 3
screen.blit(images['l_knight'], (playerX, playerY))
if onpress[pygame.K_w]:
playerY -= 3
screen.blit(images['l_knight'], (playerX, playerY))
if onpress[pygame.K_d]:
playerX += 3
screen.blit(images['r_knight'], (playerX, playerY))
if onpress[pygame.K_s]:
playerY += 3
screen.blit(images['r_knight'], (playerX, playerY))
if onpress[pygame.K_w] and onpress[pygame.K_a]:
screen.blit(images['l_knight'], (playerX, playerY))
if onpress[pygame.K_s] and onpress[pygame.K_a]:
screen.blit(images['l_knight'], (playerX, playerY))
if onpress[pygame.K_w] and onpress[pygame.K_d]:
screen.blit(images['r_knight'], (playerX, playerY))
if onpress[pygame.K_s] and onpress[pygame.K_d]:
screen.blit(images['r_knight'], (playerX, playerY))
if playerX <= -8:
playerX = -8
elif playerX >= 965:
playerX = 965
if playerY <= 5:
playerY = 5
elif playerY >= 440:
playerY = 440
for enemy in enemy_list:
enemy.move()
enemy.draw()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
clock.tick(60)
pygame.display.update()
main_menu()