2015-10-13 59 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之前插入并且没有改变。 –