简体   繁体   中英

Moving a sprite to another sprite in pygame

I am new to pygame and trying to develop an 8-Bit Shooter. What I want is when the user presses space, a bullet will fire and go until it hits a mob or the edge of the window. Here is the bullet class:

class BULLET:
    def __init__(self, location):
        self.pos = [0,0]
        self.image = NOBULLET
        self.rect = self.image.get_rect()
        self.direction = 0
    def move(self):
        if self.rect.right >= screen.get_rect().right:
            self.rect.right = self.rect.right - 10
        if self.rect.left <= screen.get_rect().left:
            self.rect.right = self.rect.right + 10
        if self.rect.top <= screen.get_rect().top:
            self.rect.top = self.rect.top + 10
        if self.rect.bottom >= screen.get_rect().bottom:
                self.rect.bottom = self.rect.bottom - 10
    def update(self):
        while True:
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if (event.key == K_SPACE and Gun.image == NOGUN):
                        self.pos == Player.pos()
                        self.direction == Player.direction

What I want is for the bullet to go to the Player when Space is pressed, and move in the direction the player is facing until it hits a mob or the edge of the window.

[edit] This is the main shooting loop:

if AMMO > 0:
    if event.type == pygame.KEYDOWN and Gun.image == NOGUN:
        if event.key == pygame.K_SPACE and Gun.image == NOGUN:
            Bullet.direction = Player.direction
            Bullet.pos == Player.pos
            shot.play()
            print "BANG"
            AMMO = AMMO - 1
            time.sleep(0.09)
            Bullet.image == NOBULLET 

put a location and direction as parameters in your __init__ I see that you already have a location. Now you need to store those from the constructor to the class

where you have self.pos = [0,0] you need to instead set that = to the location passed in as an argument

do the same thing with direction by using self.direction = orientation

[edit] Here i'll go into more detail for you, this is your init method:

def __init__(self, location):
    self.pos = [0,0]
    self.image = NOBULLET
    self.rect = self.image.get_rect()
    self.direction = 0

You need to add an argument to it, right now it has two, one is self, one is location, you also need direction, it should end up looking like this def __init__(self, location, orientation): Now when you create the bullet you will have to pass it the position of the player (x,y) and the direction he is facing (probably 0,1,2,3 for N,E,S,W)

Then you need to store those arguments by doing self.variable = argument for both location and orientation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM