2017-08-11 90 views
0

首先,我加載了一張船的圖片並初始化了它的位置。之後我在我的程序中添加了子彈。之後,我發現無論如何調試它,它都不能在正確的位置。pygame位置錯誤

# 1. - import library 
import pygame,sys 
from pygame.locals import * 
from pygame.sprite import Sprite 

class Player(Sprite): 
    def __init__(self): 
     super().__init__() 
     self.image = pygame.image.load('image/pig.bmp') 
     self.rect = self.image.get_rect() 
     self.screen_rect = screen.get_rect() 


class Bullet(Sprite):   
    def __init__(self, player): 
     super().__init__() 
     self.rect = pygame.Rect(0, 0, bullet_width, bullet_height) 
     self.color = bullet_color 
     self.rect.center = player.rect.center 
     self.rect.left = player.rect.right 

# 2. - Initialize the game 
pygame.init() 
width,height = 800,600 
screen = pygame.display.set_mode((width,height)) 
keys = [False,False,False,False] 
playerpos = [0,288] 
bullet_width = 15 
bullet_height = 6 
bullet_color = (200, 200 , 0) 

player = Player() 
bullet = Bullet(player) 

grass = pygame.image.load("image/bg.bmp") 

# 4. - keep looping through 
while True: 
    # 5. - clear the screen before drawing it again. 
    screen.fill(0) 
    # 6. - Draw the screen elements. 
    screen.blit(grass,(0,0)) 
    screen.blit(player.image, playerpos) 

    pygame.draw.rect(screen, bullet.color, bullet.rect) 
    # 7. - update the screen 
    pygame.display.flip() 
    # 8. - loop through the events 
    for event in pygame.event.get(): 
     # check if the event is the X button. 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

爲什麼子彈出現在左上角 enter image description here

我希望子彈出現在船的右邊,但如果我不使用座標我不能這樣做(X,Y) ,我該怎麼做?

回答

0

您正在使用playerpos將船舶拖曳到與其矩形位置無關的位置。你需要使船舶的位置鏈接到其矩形的鏈接,從而使子彈能夠訪問它:

# 1. - import library 
import pygame,sys 
from pygame.locals import * 
from pygame.sprite import Sprite 

class Player(Sprite): 
    def __init__(self): 
     super().__init__() 
     self.image = pygame.image.load('image/pig.bmp') 
     self.image.fill((255, 0, 0)) 
     self.rect = self.image.get_rect() 
     self.screen_rect = screen.get_rect() 


class Bullet(Sprite): 
    def __init__(self, player): 
     super().__init__() 
     self.rect = pygame.Rect(0, 0, bullet_width, bullet_height) 
     self.color = bullet_color 
     self.rect.center = player.rect.center 
     self.rect.left = player.rect.right 

# 2. - Initialize the game 
pygame.init() 
width,height = 800,600 
screen = pygame.display.set_mode((width,height)) 
keys = [False,False,False,False] 

bullet_width = 15 
bullet_height = 6 
bullet_color = (200, 200 , 0) 

player = Player() 
player.rect.topleft = [0,288] 
bullet = Bullet(player) 

grass = pygame.image.load("image/bg.bmp") 

# 4. - keep looping through 
while True: 
    # 5. - clear the screen before drawing it again. 
    screen.blit(grass, (0, 0)) 
    # 6. - Draw the screen elements. 
    screen.blit(player.image, player.rect.topleft) 

    pygame.draw.rect(screen, bullet.color, bullet.rect) 
    # 7. - update the screen 
    pygame.display.flip() 
    # 8. - loop through the events 
    for event in pygame.event.get(): 
     # check if the event is the X button. 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

這是因爲表面的get_rect()方法不知道那裏的表面將被位圖混合到另一個表面,所以它的位置就是(0,0)。 get_rect()僅用於獲取曲面的尺寸。