0

I'm trying to make a game with pygame and I ran into a problem with enemy spawning. I was just testing it out of see if it would work but when the player moves the game would lag like here. When I try to run the program it's gives an error that the variable xenemy is not defined. I took that out when I ran the game, but added in here because it is also a bug.

Here is my code:

import pygame,sys
import random
import time
from pygame.locals import *

pygame.init()

#WINDOW
WIDTH = 352
HEIGHT = 122
pygame.display.set_caption('My First Python Game')
screen=pygame.display.set_mode((WIDTH,HEIGHT))

#LOADING IMAGES
pg = "player.gif"
ey = "enemy.gif"
bg = "y.gif"
a = 0
enemypic = pygame.image.load(ey)
player = pygame.image.load(pg)
spriteWidth, spriteHeight = player.get_rect().size
background = pygame.image.load(bg)

#MUSIC
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play(-1,0.0)

#X & Y AXIS
x = 0
y = 0

#Enemy
def enemy():
    xenemy = random.randrange(24,147)
    yenemy = random.randrange(1,135)
    global  xenemy
    global  yenemy

#Main Game Loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        sys.exit
        x = min(max(x, 0), WIDTH - spriteWidth)
        y = min(max(y, 0), HEIGHT - spriteHeight)
        screen.blit(background,[0,0])
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                x  += 45
        screen.blit(enemypic, (xenemy,yenemy))
        screen.blit(player, (y,x))
        pygame.display.update()
        if x <= WIDTH:
            x = 0
        if y <= HEIGHT:
            y = 0
        if a == 0:
            pygame.display.flip()
            time.sleep(1)
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
j4awesome
  • 57
  • 10
  • Is this code formatted correctly? Because as it is now, everything is happening inside the event loop, and for every event it is blitting the background, player and enemy. For every. Single, Event. – marienbad May 12 '16 at 22:12
  • To fix the `xenemy` issue, it (and probably `yenemy`) should be declared `global` at the beginning of every function that changes it (or them). Then make sure the `ememy()` function at least once before entering the `while True` processing loop so they'll be defined when referenced within it. Generally, to speed the game up, only do the minimal amount of processing that needs to be done in the event loop for each type of event encountered. Lastly, suggest you read and follow [PEP 8 - Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). – martineau May 12 '16 at 22:55
  • I did not work it still is giving the error that xenemy is not defined and I the global in the front of the while statement. – j4awesome May 12 '16 at 23:19
  • 1
    BTW, while Stack Overflow can be a bit unfriendly to "help me debug" questions, that's one of the main things we handle over at [the r/LearnPython subreddit](https://www.reddit.com/r/learnpython). I only mention this because there's a close vote here, and I'd hate to see your programming hopes squashed because of the specific rules of the site you posted on initially. – Xiong Chiamiov May 12 '16 at 23:33
  • Is your question about the lag or the xenemy error? I recommend you keep the question simpler, and paste the *exact* code that is causing lag and ask about a single question, rather than 2 separate ones. See: [mcve] – DJMcMayhem May 13 '16 at 01:56

0 Answers0