2014-06-24 51 views
0

我想讓我的Sprite在屏幕上移動,我創建了應該使它發生的功能,但它不起作用>問題在於move_ip函數,但我不知道如何解決它。這些是我第一次嘗試上課,因此歡迎任何建議。Move_ip不起作用,移動Sprites

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() 
+0

我不熟悉的pygame的,但我沒有看到'KEYDOWN'或'KEYUP' if語句中的移動信息是如何傳送給你的'player'對象的。你需要調用你創建的'moving'方法,或者設置'player.player_move_x','player.player_move_y'? –

回答

2

首先,你不應該命名你的職業Sprite,因爲這個名字與Sprite類的pygame相沖突。

另外,如果你想用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() 
+0

我喜歡那個按鍵的檢測。 –

0

你似乎並不需要從主循環移動任何電話。這可能是問題所在。

+0

我已經刪除了那部分意外,但即使與該調用,功能不起作用 –

+0

@JoeDoe您是否編輯了上面的代碼以包含函數? –

+0

我編輯了我的問題 –