Python,无法控制敌人的精灵

时间:2013-06-14 23:27:11

标签: python pygame

完整的新手,一般的编程新手。我正在尝试使用pygame在python中编写一个侧滚动游戏。我为我的精灵类创建了三个不同的库:玩家,敌人和土地。我让这片土地变成了精灵,这样玩家就可以与陆地上的不同物体互动(碰撞)而不能通过它们。我遇到的问题是我希望敌人精灵也与陆地精灵互动。理想情况下,我希望敌人精灵从点“x”开始并设置为运动(-2)直到它与陆地精灵接触,此时我希望它反向。我一直在尝试我能想到的一切,并在网上寻找解决方案,使这项工作取得成功。它似乎应该非常简单,但我无法使其发挥作用。

感谢您的时间。

这是我的代码:

土地精灵:

import pygame

class Object(pygame.sprite.Sprite):
    def __init__(self,image_file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()

玩家精灵:

import pygame

class Player(pygame.sprite.Sprite):
    change_x = 0
    change_y = 0
    jump_ready = False
    frame_since_collision = 0
    frame_since_jump = 0
    frame = 0
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.images = []
        for i in range(1,9):
            img = pygame.image.load("pit"+str(i)+".png").convert()
            img.set_colorkey((0,0,0))
            self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()

    def changespeed_x(self,x):
        self.change_x = x
    def changespeed_y(self,y):
        self.change_y = y
    def update(self,ground,brick,enemy):
        if self.change_x < 0:
            self.frame += 1
            if self.frame > 3*4:
                self.frame = 0
            self.image = self.images[self.frame//4]
        if self.change_x > 0:
            self.frame += 1
            if self.frame > 3*4:
                self.frame = 0
            self.image = self.images[self.frame//4+4]

        old_x = self.rect.x
        new_x = old_x + self.change_x
        self.rect.x = new_x

        player_health = 5
        hurt = pygame.sprite.spritecollide(self,enemy,False)
        if hurt:
            player_health -= 1
            print(player_health)

        brick_break = pygame.sprite.spritecollide(self,brick,True)
        collide = pygame.sprite.spritecollide(self,ground,False)
        if collide:
            self.rect.x = old_x
        old_y = self.rect.y
        new_y = old_y + self.change_y
        self.rect.y = new_y

        touch_list = pygame.sprite.spritecollide(self,ground,False)

        for ground in touch_list:
            self.rect.y = old_y
            self.rect.x = old_x
            self.change_y = 0
            self.frame_since_collision = 0
        if self.frame_since_collision < 6 and self.frame_since_jump < 6:
            self.frame_since_jump = 100
            self.change_y -= 8
        self.frame_since_collision += 1
        self.frame_since_jump += 1

    def calc_grav(self):
        self.change_y += .35
        if self.rect.y >= 450 and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = 450
            self.frame_since_collision = 0
    def jump(self,blocks):
        self.jump_ready = True
        self.frame_since_jump = 0

这是敌人精灵有效,它只向左移动,每当我尝试变换碰撞代码时,就像我在玩家类中一样,精灵会在它与陆地精灵相撞时停止

敌人精灵:

import pygame


class Enemy(pygame.sprite.Sprite):
    def __init__(self,image_file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.image = self.image.convert()
        self.image.set_colorkey((0,0,0))
        self.rect = self.image.get_rect()

    def update(self,ground):
        change_x = -2
        self.rect.x += change_x

和我的主程序代码:

# first must import
import pygame
import random
import thing
import enemy
import player



# initialize the game engine
pygame.init()

# define some colors
# more color combos at www.colorpicker.com
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = (0, 255, 0)
red   = (255, 0, 0)
blue = (131,226,252)


# open and set window size.
screen_width = 700
screen_height = 350
screen = pygame.display.set_mode([screen_width,screen_height])

break_list = pygame.sprite.Group()
land_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

cloud = pygame.image.load("cumulus-huge.png").convert()

for x in range(300,500,60):
    brick = thing.Object("birck.png")
    brick.rect.x = x
    brick.rect.y = 180
    break_list.add(brick)
    all_sprites_list.add(brick)

for x in range(0,200,50):
    wall = thing.Object("Sky3.png")
    wall.rect.x = -180
    wall.rect.y = x
    land_list.add(wall)
    all_sprites_list.add(wall)


for x in range (-50,1400,70):
    ground = thing.Object("Ground2.png")
    ground.rect.x = x
    ground.rect.y = 305
    land_list.add(ground)
    all_sprites_list.add(ground)

monster = enemy.Enemy("monster1.png")
monster.rect.x = 650
monster.rect.y = 250
enemy_list.add(monster)
all_sprites_list.add(monster)

for x in range(760,1070,300):
    pipe = thing.Object("pipe-top.png")
    pipe.rect.x = x
    pipe.rect.y = 225
    land_list.add(pipe)
    all_sprites_list.add(pipe)

player = player.Player()
player.rect.x = 10
player.rect.y = 230
all_sprites_list.add(player)


# set the window title
pygame.display.set_caption("Scroller")

# the following code sets up the main program loop

# Boolean Variable to loop until the user clicks the close button.
done = False # loop control

# used to manage how fast the screen updates
clock = pygame.time.Clock() # controls how fast game runs


# Main Program Loop
while done == False:
    # ALL EVENT PROCESSING (input) SHOULD GO BELOW THIS COMMENT 

    for event in pygame.event.get(): # user did something
        if event.type == pygame.QUIT: #If user clicked close
            done = True # flag that we are done so we exit this loop

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed_x(-6)
            if event.key == pygame.K_RIGHT:
                player.changespeed_x(6)
            if event.key == pygame.K_UP:
                player.jump(land_list)
            if event.key == pygame.K_DOWN:
                player.changespeed_y(6)

        if event.type == pygame.KEYUP: 
            if event.key == pygame.K_LEFT: 
                player.changespeed_x(-0)
            if event.key == pygame.K_RIGHT: 
                player.changespeed_x(0)

    monster.update()
    player.update(land_list,break_list,enemy_list)
    player.calc_grav()  


    # ALL EVENT PROCESSING (input) SHOULD GO ABOVE THIS COMMENT

    # ALL GAME LOGIC (process) SHOULD GO BELOW THIS COMMENT



    if player.rect.x >= 500:
        diff = player.rect.x - 500
        player.rect.x=500
        for ground in land_list:
            ground.rect.x -= diff
        for brick in break_list:
            brick.rect.x -= diff
        for monster in enemy_list:
            monster.rect.x -= diff

    if player.rect.x <= 15:
        diff = 15 - player.rect.x
        player.rect.x = 15
        for ground in land_list:
            ground.rect.x += diff
        for brick in break_list:
            brick.rect.x += diff
        for monster in enemy_list:
            monster.rect.x += diff

    # ALL GAME LOGIC (process) SHOULD GO ABOVE THIS COMMENT

    # ALL CODE TO DRAW (output) SHOULD GO BELOW THIS COMMENT

    # First, clear the screen. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.fill(blue)
    screen.blit(cloud,[200,0])
    cloud.set_colorkey(black)
    all_sprites_list.draw(screen)


    # ALL CODE TO DRAW (output) SHOULD GO ABOVE THIS COMMENT

    # This will update the screen with what's been drawn.
    pygame.display.flip()

    # limit to 30frames per second
    clock.tick(30)

pygame.quit()

1 个答案:

答案 0 :(得分:1)

你的敌人类应该是这样的:

class Enemy(pygame.sprite.Sprite):
    def __init__(self,image_file):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.image = self.image.convert()
        self.image.set_colorkey((0,0,0))
        self.rect = self.image.get_rect()
        self.vel = -2

    def update(self,ground):
        self.rect.x += self.vel

然后在您的更新循环中,实现此伪代码:

...

if monster collides with ground:
    monster.vel *= -1

...