【问题标题】:pygame Image Position On The Mouse Is Off How To fix? [duplicate]pygame 鼠标上的图像位置关闭如何解决? [复制]
【发布时间】:2021-03-03 06:42:10
【问题描述】:

所以我很想将悬停在鼠标旁边的图像定位,但我希望我的图像在鼠标上而不是与鼠标分开一点 VIDEO EXAMPLE

# how I draw my image
MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()


# how I displayed my image next to the mouse
window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos()))

这是我的完整游戏代码

import pygame
pygame.init()

# draw our window
window = pygame.display.set_mode((500,540))
pygame.display.set_caption("Tic Tac TOE")



MANUAL_CURSOR = pygame.image.load('nw.png').convert_alpha()


MANUAL_CURSOR2 = pygame.image.load('nOW.png').convert_alpha()



bg = pygame.image.load("Player 1.png")
fps = 40

clock = pygame.time.Clock()

class button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.over = False

    def draw(self,window,outline=None):
                #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(window, outline, (self.x-2,self.y-2,self.width+4,self.height+4),0)
                    
        pygame.draw.rect(window, self.color, (self.x,self.y,self.width,self.height),0)
                
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 60)
            text = font.render(self.text, 1, (0,0,0))
            window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self, pos):
                #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
                    
        return False

    def playSoundIfMouseIsOver(self, pos, sound):
        if self.isOver(pos):            
            if not self.over:
                beepsound.play()
                self.over = True
        else:
            self.over = False




                    
white = (250,250,250)
greenbutton2 = button((0,255,0),210,220,100,100, '')

greenbutton3 = button((0,255,0),350,220,100,100, '')

greenbutton4 = button((0,255,0),70,220,100,100, '')

greenbutton5 = button((0,255,0),70,350,100,100, '')



            
font = pygame.font.Font("fos.ttf", 60)
score = 0
cointext = font.render("" + str(score), True, (255,255,255))
coinrect = cointext.get_rect()
coinrect.center = ((620,50))


def redraw():
    window.fill((174, 214, 241))
    window.blit(bg,(0,0))
    greenbutton2.draw(window)
    greenbutton3.draw(window)
    greenbutton4.draw(window)
    greenbutton5.draw(window)


    if score >= 0:
        window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos(+10)))

    if score >= 1:
        window.blit(MANUAL_CURSOR2,(pygame.mouse.get_pos()))

    if score >= 2:
        window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos()))

    if score >= 3:
        window.blit(MANUAL_CURSOR2,(pygame.mouse.get_pos()))
        

    if score >= 4:
        window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos()))

pos = pygame.mouse.get_pos()
        
# our main loop
runninggame = True
while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type ==  pygame.QUIT:
            runninggame = False


        if event.type == pygame.MOUSEBUTTONDOWN:
            if greenbutton2.isOver(pos):
                score += 1
                cointext = font.render("" + str(score), True, (255,255,255))
                coinrect.center = ((620,50))
            
    redraw()


    pygame.display.update()

pygame.quit()

    

以及我正在使用的图片

【问题讨论】:

  • window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos())) 表示您将图像的左上角置于鼠标位置的中心。如果你想让它显示在中心,你必须添加一个图像宽度和高度一半的 X 和 Y 偏移量。

标签: python html css python-3.x pygame


【解决方案1】:

使用get_rect o 得到一个图像大小的矩形,并用鼠标位置设置矩形的中心。使用矩形blit 图像。例如:

window.blit(MANUAL_CURSOR,(pygame.mouse.get_pos()))

window.blit(MANUAL_CURSOR, MANUAL_CURSOR.get_rect(center = pygame.mouse.get_pos()))

pygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数center 指定矩形的中心。这些关键字参数在返回之前应用于pygame.Rect 的属性(有关关键字参数的完整列表,请参见pygame.Rect)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多