Move_ip不起作用,移动Sprites

时间:2014-06-24 19:33:53

标签: python pygame

我正在尝试让我的精灵在屏幕上移动,我已经创建了应该让它发生的功能,但它不起作用>问题在于move_ip函数,但我不知道如何修复它。这是我第一次上课,所以欢迎任何建议。

import pygame 
pygame.init() 

finish = False 
white = ( 255, 255, 255)
black = (0, 0, 0)
grey = (211, 211, 211)
font = pygame.font.Font("C:/Windows/Fonts/BRITANIC.TTF", 20) 
screen = pygame.display.set_mode((600, 400)) 
pygame.display.set_caption("Game") 

class Sprite(object):
    def __init__(self, name):
        self.health = 3
        self.name = name
        self.points = 0
        self.x = 25
        self.y = 25


    def printName(self):
        print (self.name)

class Dog(Sprite):
    def __init__(self, name):
        super(Dog, self).__init__(name)
        self.dog_img = pygame.image.load("dog_left.png")
        self.dog_rect = self.dog_img.get_rect()


    def drawDog(self, screen):
        screen.blit(self.dog_img, (self.x, self.y))

    def takeBone(self, sp1, sp2):
        takebone = pygame.sprite.collide_rect(sp1, sp2)
        if takebone == True:
            self.points += 1

    def moving(self):
        self.player_move_x = 0
        self.player_move_y = 0
        self.move = self.dog_img.move_ip(player_move_x, player_move_y)

class Bone(Sprite):
    def __init__(self, name):
        super(Bone, self).__init__(name)
        self.neme = "Bone"
        self.bone = pygame.image.load("bone.png")
        self.bone_rect = self.bone.get_rect()
        self.x = 250
        self.y = 250

    def drawBone(self, screen):
        screen.blit(self.bone, (self.x, self.y))


end_pos = 170

player_x = 10
player_y = 10


background = pygame.image.load("grass.png") 
background_rect = background.get_rect() 
size =  background.get_size() 
screen2 = pygame.display.set_mode(size) 

player = Dog("Tom")
bone = Bone("Bone")


timer = pygame.time.Clock()

while finish == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finish = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_move_x = -5
            if event.key == pygame.K_RIGHT:
                player_move_x = 5
            if event.key == pygame.K_UP:
                player_move_y = -5
            if event.key == pygame.K_DOWN:
                player_move_y = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player_move_x = 0
            if event.key == pygame.K_RIGHT:
                player_move_x = 0
            if event.key == pygame.K_UP:
                player_move_y = 0
            if event.key == pygame.K_DOWN:
                player_move_y = 0


    screen.blit(background, background_rect)   
    player.drawDog(screen)
    bone.drawBone(screen)         




    pygame.display.flip()

    timer.tick(25)

pygame.quit()

我对代码进行了更改:

import pygame

pygame.init() 

finish = False 
white = ( 255, 255, 255)
black = (0, 0, 0)
grey = (211, 211, 211)
font = pygame.font.Font("C:/Windows/Fonts/BRITANIC.TTF", 20) 
screen = pygame.display.set_mode((600, 400)) 
pygame.display.set_caption("Game") 

class Sprite(object):
    def __init__(self, name):
        self.health = 3
        self.name = name
        self.points = 0
        self.x = 25
        self.y = 25


    def printName(self):
        print (self.name)

class Dog(Sprite):
    def __init__(self, name):
        super(Dog, self).__init__(name)
        self.dog_img = pygame.image.load("dog_left.png")
        self.dog_rect = self.dog_img.get_rect()


    def drawDog(self, screen):
        screen.blit(self.dog_img, (self.x, self.y))

    def takeBone(self, sp1, sp2):
        takebone = pygame.sprite.collide_rect(sp1, sp2)
        if takebone == True:
            self.points += 1

    def moving(self):
        self.player_move_x = 0
        self.player_move_y = 0
        self.move = self.dog_rect.move_ip(self.player_move_x, self.player_move_y)

class Bone(Sprite):
    def __init__(self, name):
        super(Bone, self).__init__(name)
        self.neme = "Bone"
        self.bone = pygame.image.load("bone.png")
        self.bone_rect = self.bone.get_rect()
        self.x = 250
        self.y = 250

    def drawBone(self, screen):
        screen.blit(self.bone, (self.x, self.y))

background = pygame.image.load("grass.png") 
background_rect = background.get_rect() 
size =  background.get_size() 
screen2 = pygame.display.set_mode(size) 

player = Dog("Tom")
bone = Bone("Bone")


timer = pygame.time.Clock()

while finish == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finish = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.player_move_x = -5
            if event.key == pygame.K_RIGHT:
                self.player_move_x = 5
            if event.key == pygame.K_UP:
                player.dog_rect.move_ip(-5, 0)
                player_move_y = -5
            if event.key == pygame.K_DOWN:
                self.player_move_y = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.player_move_x = 0
            if event.key == pygame.K_RIGHT:
                self.player_move_x = 0
            if event.key == pygame.K_UP:
                player.dog_rect.move_ip(0, 0)
                player_move_y = 0
            if event.key == pygame.K_DOWN:
                self.player_move_y = 0


    screen.blit(background, background_rect)   
    player.drawDog(screen)
    player.moving()
    bone.drawBone(screen)         

    pygame.display.flip()

    timer.tick(25)

pygame.quit()

2 个答案:

答案 0 :(得分:2)

首先,您不应该为您的班级Sprite命名,因为该名称与pygame的Sprite类发生冲突。

此外,如果您想使用Rect课程,您的课程不需要xy字段来存储其位置,因为您可以使用Rect为此。

让我们看一下moving函数:

self.player_move_x = 0
self.player_move_y = 0
self.move = self.dog_rect.move_ip(self.player_move_x, self.player_move_y)

在此处设置player_move_xplayer_move_y0,然后您希望使用这些值移动dog_rect。除了您实际上从未使用dog_rect在正确的位置对图像进行布局之外,将Rect移动到0, 0并不会做任何事情。

您给出的代码示例中还有一些错误,但您的代码可能看起来像这样(我添加了一些注释以供解释):

import pygame

pygame.init() 

background = pygame.image.load("grass.png") 
background_rect = background.get_rect() 
size =  background.get_size() 
# only call set_mode once
screen = pygame.display.set_mode(size) 
pygame.display.set_caption("Game") 

# no need for three different classes
class MySprite(pygame.sprite.Sprite):
    def __init__(self, name, img_path, pos):
        super(MySprite, self).__init__()
        self.name = name
        self.image = pygame.image.load(img_path)
        self.rect = self.image.get_rect(topleft=pos)

    # use rect for drawing
    def draw(self, screen):
        screen.blit(self.image, self.rect)

    # use rect for moving
    def move(self, dir):
        self.rect.move_ip(dir)

player = MySprite("Tom", "dog_left.png", (10, 10))
bone = MySprite("Bone", "bone.png", (250, 250))

timer = pygame.time.Clock()

def main():
    points = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return points

        # get all pressed keys
        pressed = pygame.key.get_pressed()
        # pressed[some_key] is 1 if the key some_key is pressed
        l, r, u, d = [pressed[k] for k in pygame.K_a, pygame.K_d, pygame.K_w, pygame.K_s]
        # create a tuple describing the direction of movement 
        # TODO: normalizing/different speed
        player.move((-l + r, -u + d))

        if pygame.sprite.collide_rect(player, bone):
            points += 1

        screen.blit(background, background_rect)   
        player.draw(screen)
        bone.draw(screen)

        pygame.display.flip()
        timer.tick(25)

if __name__ == '__main__':
    print main()

答案 1 :(得分:0)

您似乎没有任何要求从主循环移动的电话。这可能是个问题。