Trying to move enemy towards player

Trying to move enemy towards player

Postby Hissing Cockroach » Mon Oct 21, 2013 2:50 am

Hi. I have made a game and have been trying to move the enemy towards the character.
So far, I have gotten the enemy to move towards the player, but never actually meets
the x and y of the player. Can anyone help?
Here is the code:

Code: Select all
import pygame, sys
import time
import os

pygame.init()




DISPLAYSURF = pygame.display.set_mode((900,700),0,32)
pygame.display.set_caption('Freeroam')



WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
myfont = pygame.font.SysFont("Lucida Console", 70)
label = myfont.render("GAME OVER", 1, RED)
img = pygame.image.load('playerpng')
enem = pygame.image.load('enemy.png')
x = 200
y = 300
ex = 200
ey = 200
direc = 'up'
while True:
    DISPLAYSURF.fill(WHITE)
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        x -= 9
    if key[pygame.K_RIGHT]:
        x += 8
    if key[pygame.K_UP]:
        y -= 8
    if key[pygame.K_DOWN]:
        y += 8
    if ex <= x:
        ey += 8
    if ex >= x:
        ey -= 8
    if ey <= y:
        ex -= 8
    if ey >= y:
        ex += 8
    if ex == x and ey == y:
        x = 3
        y = 20
        DISPLAYSURF.blit(label, (350,350))
   


       

    DISPLAYSURF.blit(enem, (ex,ey))
    DISPLAYSURF.blit(img, (x,y))
   

    if x <= 91 and y <= 92:
        x = 3
        y = 20
        DISPLAYSURF.blit(label, (350,350))
    if x >= 733:
        x = 732
    if y <= -26:
        y = -29
    if x <= -25:
        x = -24
    if y >= 595:
        y = 594

    if ex >= 733:
        ex = 732
    if ey <= -26:
        ey = -29
    if ex <= -25:
        ex = -24
    if ey >= 595:
        ey = 594
   
   
       

 
    print x
    print y
    os.system('cls')

   
    pygame.draw.rect(DISPLAYSURF, BLACK, (1, 1, 170, 150))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()


    pygame.display.update()
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Trying to move enemy towards player

Postby metulburr » Mon Oct 21, 2013 3:28 am

I have made a game and have been trying to move the enemy towards the character.
So far, I have gotten the enemy to move towards the player, but never actually meets
the x and y of the player. Can anyone help?


1) I dont see how this is successful? The so called enemy is all over the place when you move the character ... not moving towards the player. It is almost by chance that they meet.

2) Use just a square image surface to represent each character.
Code: Select all
#img = pygame.image.load('playerpng')
img = pygame.Surface([50,50])
#enem = pygame.image.load('enemy.png')
enem = pygame.Surface([50,50])


3) this is windows only:
Code: Select all
os.system('cls')


4)
Code: Select all
    if key[pygame.K_LEFT]:
        x -= 9
    if key[pygame.K_RIGHT]:
        x += 8
    if key[pygame.K_UP]:
        y -= 8
    if key[pygame.K_DOWN]:
        y += 8
    if ex <= x:
        ey += 8
    if ex >= x:
        ey -= 8
    if ey <= y:
        ex -= 8
    if ey >= y:
        ex += 8
    if ex == x and ey == y:
        x = 3
        y = 20
        DISPLAYSURF.blit(label, (350,350))
   


       

    DISPLAYSURF.blit(enem, (ex,ey))
    DISPLAYSURF.blit(img, (x,y))
   

    if x <= 91 and y <= 92:
        x = 3
        y = 20
        DISPLAYSURF.blit(label, (350,350))
    if x >= 733:
        x = 732
    if y <= -26:
        y = -29
    if x <= -25:
        x = -24
    if y >= 595:
        y = 594

    if ex >= 733:
        ex = 732
    if ey <= -26:
        ey = -29
    if ex <= -25:
        ex = -24
    if ey >= 595:
        ey = 594

These are all just magic numbers. What is this all suppose to mean?

5) It would make it a lot easier if you start using classes/functions. And if you do not know how...You should stop pygame and start learning basic programming.

6) i strongly get the feeling that you are over your head, but whatever. Check out:
Enemy.pos_towards_player() and Enemy.update() in the code:
https://github.com/metulburr/fighter/bl ... fighter.py
Seriously...if it looks like gibberish, or unkown python code, your way over your head and should return to basic python coding
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1502
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Trying to move enemy towards player

Postby Mekire » Mon Oct 21, 2013 9:17 am

As Metul said, as harsh as it may sound, the way you are coding currently just isn't going to get you very far.

Try at the very least to use pygame.Rect in your next program. You shouldn't have one letter global variables like x, y, ex, and ey.

Also I would like to know if you understand basic triangle geometry. Currently you are arbitrarily changing the enemies speed based on the x and y of the player, but there isn't much logic behind it. If you don't know the Pythagorean theorem, you will need to look it up; and if you don't understand sine, cosine, and tangent you likely have a bit of math related reading ahead of you.

Also, I STRONGLY advise you to go through a basic, non game-related, function and class tutorial.
Give these two interactive tutorials at Codecademy a try. They are pretty light weight.
Functions
Classes

No one wants you to give up, but you have to learn things in the right order. The way you are proceeding now is going to create unmaintainable, completely undebuggable code.
-Mek
User avatar
Mekire
 
Posts: 1013
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest