PyGame鼠标光标中心的播放器图像

时间:2015-10-13 19:36:26

标签: python image pygame mouse

刚开始在Python中使用PyGame。我设法将player_image设置为跟随鼠标光标,但图像不在光标位置的中心。光标似乎总是位于图像的左上角。任何人都可以帮助我获取它,使图像在鼠标光标的中心?然后,我可以关闭鼠标光标,图像可以正确跟踪移动。

到目前为止,我有以下代码部分,在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()

1 个答案:

答案 0 :(得分:0)

只需将center的{​​{1}}设置为鼠标位置,例如

Rect