2015-10-13 58 views
0

剛剛開始在Python中使用PyGame。我設法將player_image設置爲跟隨鼠標光標,但是圖像沒有居中放置在光標位置上。光標始終似乎位於圖像的左上角。任何人都可以幫助我獲得它,使圖像在鼠標光標的中心?然後,我可以關閉鼠標光標並且圖像正確跟蹤移動。PyGame播放器鼠標光標中心圖片

我的代碼下面的部分,到目前爲止,在Pygame的窗口設置背景:

player_image = pygame.image.load("spaceship.png").convert_alpha() 
player_image_rect = player_image.get_rect() 
player_image_rect.centerx = player_image.get_rect().centerx 
player_image_rect.centery = player_image.get_rect().centery 
screen.blit(player_image, player_image_rect) 
pygame.mouse.set_visible(True) 

感謝

的完整代碼(我已經慢慢在功能的混合物加入):

import sys 
import pygame 
import random 


black = (0, 0, 0) 
white = (255, 255, 255) 

# Initialise PyGame 
pygame.init() 

#Set screen dimensions and title 
width = 802 
height = 585 
screen=pygame.display.set_mode([width, height]) 
pygame.display.set_caption("Space Attack Game v1") 

#Load and set up graphics position 
background_position = [0, 0] 
background_image = pygame.image.load("solarsystem.jpg").convert() 


#Set game title on screen text 
basicfont = pygame.font.SysFont(None, 68) 
text = basicfont.render('Space Attack', True, white).convert_alpha() 
text_rect = text.get_rect() 
#textrect.centerx = screen.get_rect().centerx 
#textrect.centery = screen.get_rect().centery 

#Set player mouse control image 
player_image = pygame.image.load("spaceship.png").convert_alpha() 
player_image_rect = player_image.get_rect() 
pos = pygame.mouse.get_pos() 
player_image_rect.center = pos 
#player_image_rect.centerx = player_image.get_rect().centerx 
#player_image_rect.centery = player_image.get_rect().centery 
screen.blit(player_image, player_image_rect.center) 
pygame.mouse.set_visible(True) 

#Load star 
star_image = pygame.image.load("star.png").convert_alpha() 
screen.blit(star_image, (random.randint(0,width),random.randint(0,height))) 
#star_image_rect = star_image.get_rect() 
pygame.display.flip() 

####################################################### 
done = False 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     screen.blit(star_image, (random.randint(0,width),random.randint(0,height))) 

    class ball: 
     def __init__(self,X,Y): 
      self.velocity = [1,1] 
      self.ball_image = pygame.image.load('alien.png').convert() 
      self.ball_image.set_colorkey(black) 
      self.ball_boundary = self.ball_image.get_rect (center=(X,Y)) 
      self.screen = pygame.display.get_surface() 
      #self.sound = pygame.mixer.Sound ('Collide.wav') 


    if __name__ =='__main__': 

     num_balls = 5 
     ball_list = [] 

     for i in range(num_balls): 
      ball_list.append(ball(random.randint(0, width),random.randint(0, height))) 
     while True: 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        sys.exit(0) 

      for ball in ball_list: 
       if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width: 
        #ball.sound.play() 
        ball.velocity[0] = -1 * ball.velocity[0] 
       if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height: 
        #ball.sound.play() 
        ball.velocity[1] = -1 * ball.velocity[1] 

       ball.ball_boundary = ball.ball_boundary.move (ball.velocity) 
       screen.blit (ball.ball_image, ball.ball_boundary) 

      player_position = pygame.mouse.get_pos() 
      x = player_position[0] 
      y = player_position[1] 
      pygame.display.flip() 

      screen.blit(background_image, background_position) 
      screen.blit(text, text_rect) 

      screen.blit(player_image, [x,y]) 

pygame.quit() 
+0

像素的寬度是多少? – pydude

+0

我知道中心座標是48和47. –

+0

你確定你的gameloop保持運行嗎?懶惰的代碼絕對有效。 – xXliolauXx

回答

0

只需設置你的Rect鼠標位置的center,像

pos = pygame.mouse.get_pos() 
player_image_rect.center = pos 
+0

不幸的是,仍然沒有做到這一點。我在屏幕blit之前插入並且沒有改變。 –