-1

As said I was trying to add an enemy to my pygame and everything just died. I don't really know what happened I think the "player" stuff got confused with "enemy" and kind of glued together or something.

This is for a school project and I am basically trying to make a "Street fighter" on a budget, with minimum animation and stuff :)

(I'm very new to programing i kinda suck)

import pygame

# Intialize the pygame
pygame.init()

# screen and size
screen = pygame.display.set_mode((800, 600))

# title and logo
pygame.display.set_caption("Båtisens Herre")
icon = pygame.image.load('img.png')
pygame.display.set_icon(icon)

# background
background = pygame.image.load("bakgrunn.png")

# player
playerImg = pygame.image.load('King Arthur2.png')
playerX = 100
playerY = 200


def player(x, y):
    screen.blit(playerImg, (x, y))

def enemy(x, y):
    screen.blit(playerImg, x, y))

# enemy
enemyImg = pygame.image.load('Black knight1.png')
enemyX = 370
enemyY = 480
enemyX_change = 0


    # game loop

    clock = pygame.time.Clock()
    running = True


while running:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        playerX -= 4
    if keys[pygame.K_d]:
        playerX += 4

    screen.blit(background, (0, 0,))
    player(playerX, playerY)
    pygame.display.update()

    # RGB (red, green blue)
    screen.fill((248, 58, 226))
    playerY -= 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # background(2)
    screen.blit(background, (0, 0,))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    player(playerX, playerY)
    enemy(enemyX, enemyY)
    pygame.display.update()

1 Answers1

1

In the enemy function is a ( missing:

screen.blit(playerImg, x, y))

screen.blit(enemyImg, (x, y))

Remove the multiple event loops from your code.

pygame.event.get() get all the messages and remove them from the queue. See the documentation:. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.


Complete code

import pygame

# Intialize the pygame
pygame.init()

# screen and size
screen = pygame.display.set_mode((800, 600))

# title and logo
pygame.display.set_caption("Båtisens Herre")
icon = pygame.image.load('img.png')
pygame.display.set_icon(icon)

# background
background = pygame.image.load("bakgrunn.png")

# player
playerImg = pygame.image.load('King Arthur2.png')
playerX = 100
playerY = 200


def player(x, y):
    screen.blit(playerImg, (x, y))

def enemy(x, y):
    screen.blit(enemyImg, (x, y))

# enemy
enemyImg = pygame.image.load('Black knight1.png')
enemyX = 370
enemyY = 480
enemyX_change = 0

# game loop

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        playerX -= 4
    if keys[pygame.K_d]:
        playerX += 4
    
    screen.blit(background, (0, 0))
    player(playerX, playerY)
    enemy(enemyX, enemyY)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174