鱼C论坛

 找回密码
 立即注册
查看: 2124|回复: 12

定义鼠标光标初始化的位置

[复制链接]
发表于 2022-7-31 01:48:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame
import sys
from pygame.locals import *
from random import *



class Ball(pygame.sprite.Sprite):

   
    def __init__(self, image, position, speed, bg_size):
        pygame.sprite.Sprite.__init__(self)
      

      
        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.speed = speed
        self.width, self.height = bg_size[0], bg_size[1]
        self.radius = self.rect.width / 2
        


   
    def move(self):
        self.rect = self.rect.move(self.speed)

      
        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.bottom < 0:
            self.rect.top = self.height

        elif self.rect.top > self.height:
            self.rect.bottom = 0



class Glass(pygame.sprite.Sprite):

   
    def __init__(self, glass_image, mouse_image, bg_size):
       pygame.sprite.Sprite.__init__(self)


      
       self.glass_image = pygame.image.load(glass_image).convert_alpha()
       self.glass_rect = self.glass_image.get_rect()
       self.glass_rect.left, self.glass_rect.top = \
                             (bg_size[0] - self.glass_rect.width) // 2, \
                             (bg_size[1] - self.glass_rect.height)
      

       self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
       self.mouse_rect = self.mouse_image.get_rect()
       self.mouse_rect.left, self.mouse_rect.top = \
                             self.glass_rect.left, self.glass_rect.top

      
       pygame.mouse.set_visible(False)
      
           


def main():
    pygame.init()


   
    ball_image = 'gray_ball.png'
    glass_image = 'glass.png'
    mouse_image = 'hand.png'
    bg_image = 'background.png'


    running = True


   
    pygame.mixer.music.load('bg_music.ogg')
   
    pygame.mixer.music.play()
   

   
    loser_sound = pygame.mixer.Sound('loser.wav')
   
    laugh_sound = pygame.mixer.Sound('laugh.wav')
   
    winner_sound = pygame.mixer.Sound('winner.wav')
   
    hole_sound = pygame.mixer.Sound('hole.wav')
   


   
    GAMEOVER = USEREVENT
    pygame.mixer.music.set_endevent(GAMEOVER)
   

    bg_size = width, height = 1024, 681
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('Play the ball - FishC Demo')


    background = pygame.image.load(bg_image).convert_alpha()

    balls = []
    group = pygame.sprite.Group()


   
    for i in range(5):
   
        position = randint(0, width-100), randint(0, height-100)
        speed = [randint(-10, 10), randint(-10, 10)]
        ball = Ball(ball_image, position, speed, bg_size)
        while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):
            ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
        balls.append(ball)
        group.add(ball)

   
    glass = Glass(glass_image, mouse_image, bg_size)
   

    clock = pygame.time.Clock()

            
   
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
               

            elif event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                running = False


        
        
        screen.blit(background, (0, 0))
        
        screen.blit(glass.glass_image, glass.glass_rect) # 注意:不能直接使用glass_image和glass_rect!!!
        


        
        glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
      

        
        if glass.mouse_rect.left < glass.glass_rect.left: # 若鼠标的位置的左侧小于玻璃面板左侧的位置的坐标,表明已出界需归正
            glass.mouse_rect.left = glass.glass_rect.left
        if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width: # 右侧
            glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
        if glass.mouse_rect.top < glass.glass_rect.top: # 上部
            glass.mouse_rect.top = glass.glass_rect.top
        if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height: # 下部
            glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height


      
        screen.blit(glass.mouse_image, glass.mouse_rect)
        
            
        
        for each in balls:
            each.move()
            screen.blit(each.image, each.rect)
            


        for each in group:
            group.remove(each)
            
            if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]
               
            group.add(each)


      


      
        pygame.display.flip()
        clock.tick(30)
        


if __name__ == '__main__':
    main()

------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------

【我的问题】

1、红色部分代码好像运行后并无法将光标默认在玻璃面板的左上角,那代码的意思和作用何在?

2、如果要将光标初始化定义在玻璃面板的正中央的话,代码应如何修改?


******************************

感谢大神不吝赐教,为新手解疑释惑。

赠人玫瑰,手有余香,好人一生平安!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-31 05:15:23 | 显示全部楼层
1.不,那是因为运行速度足够快,导致你看不到其最开始位置的!
2.应改为:
self.mouse_rect.left, self.mouse_rect.top = \
                            width // 2 - glass.glass_rect.width //  2, height // 2 - glass.glass_rect.height // 2
这里假设:
width,height,glass,glass_rect = 16,16,2
那么想要算出舞台中心横轴(x轴)位置,就需要算出舞台宽度的一半(width // 2 = 8),
然而,这样算出来的并不能算是其真正的中心横轴(x轴)位置(这样算出来的在执行时会发生偏移(不在中心)(不信你可以自己尝试)),
因为我们没有考虑到角色本身的宽度(2),所以我们应该再将结果 8 减去 自身宽度的一半 (8 - glass.glass_rect.width // 2 = 7),这样的结果才是中心的 x 位置!
注意:这里使用的是地板除,是为了防止算出的结果有小数,如果算出小数,就会导致程序报错!
寻找中心纵轴(y 轴)坐标,方法也如上!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 10:15:28 | 显示全部楼层
本帖最后由 lzb1001 于 2022-7-31 10:20 编辑
python爱好者. 发表于 2022-7-31 05:15
1.不,那是因为运行速度足够快,导致你看不到其最开始位置的!
2.应改为:


感谢大神的热心指点。

1、点击运行后,我就放开鼠标了,但进入游戏画面后鼠标也不在玻璃面板的左上角啊

2、这里假设:width,height,glass,glass_rect = 16,16,2

等号左边四个变量名,右边三个数值

是否有错?

评分

参与人数 1鱼币 +5 贡献 +3 收起 理由
python爱好者. + 5 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-31 10:28:25 | 显示全部楼层
lzb1001 发表于 2022-7-31 10:15
感谢大神的热心指点。

1、点击运行后,我就放开鼠标了,但进入游戏画面后鼠标也不在玻璃面板的左上 ...

1.可能你会有轻微的晃动.....吧...
2.把 "." 写成 "," 了,抱歉,应该为:
width,height,glass.glass_rect = 16,16,2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 10:42:55 | 显示全部楼层
本帖最后由 lzb1001 于 2022-7-31 10:44 编辑
python爱好者. 发表于 2022-7-31 05:15
1.不,那是因为运行速度足够快,导致你看不到其最开始位置的!
2.应改为:


重大发现:

小甲鱼的书面教材中有一行代码定义了鼠标光标的初始化位置,而教学视频中则恰好漏掉了这行代码,所以导致鼠标光标的初始化位置不在玻璃面板左上角的位置。

详见下方代码中红色的部分:

import pygame
import sys
from pygame.locals import *
from random import *



class Ball(pygame.sprite.Sprite):

   
    def __init__(self, image, position, speed, bg_size):
        pygame.sprite.Sprite.__init__(self)
      

      
        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.speed = speed
        self.width, self.height = bg_size[0], bg_size[1]
        self.radius = self.rect.width / 2
        


   
    def move(self):
        self.rect = self.rect.move(self.speed)

      
        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.bottom < 0:
            self.rect.top = self.height

        elif self.rect.top > self.height:
            self.rect.bottom = 0



class Glass(pygame.sprite.Sprite):

   
    def __init__(self, glass_image, mouse_image, bg_size):
       pygame.sprite.Sprite.__init__(self)


      
       self.glass_image = pygame.image.load(glass_image).convert_alpha()
       self.glass_rect = self.glass_image.get_rect()
       self.glass_rect.left, self.glass_rect.top = \
                             (bg_size[0] - self.glass_rect.width) // 2, \
                             (bg_size[1] - self.glass_rect.height)
      

       self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
       self.mouse_rect = self.mouse_image.get_rect()
       self.mouse_rect.left, self.mouse_rect.top = \
                             self.glass_rect.left, self.glass_rect.top # 此行代码并不是定义鼠标光标初始化时的位置!----此行代码的作用和意义???
      

       # 下面这行代码才是定义鼠标光标初始化时的位置!
       pygame.mouse.set_pos([self.glass_rect.left, self.glass_rect.top]) # 小甲鱼书面教材中有这行,但教学视频中不知什么原因遗漏,而我是照着视频中的代码敲击,所以运行后鼠标光标并不在玻璃面板的正中央!
       # 若想将鼠标光标初始化的位置定义在玻璃面板正中央,则:
       #pygame.mouse.set_pos([self.glass_rect.centerx, self.glass_rect.centery])

       # 将原鼠标光标设置为不可见!
       pygame.mouse.set_visible(False)
      
           


def main():
    pygame.init()


   
    ball_image = 'gray_ball.png'
    glass_image = 'glass.png'
    mouse_image = 'hand.png'
    bg_image = 'background.png'


    running = True


   
    pygame.mixer.music.load('bg_music.ogg')
   
    pygame.mixer.music.play()
   

   
    loser_sound = pygame.mixer.Sound('loser.wav')
   
    laugh_sound = pygame.mixer.Sound('laugh.wav')
   
    winner_sound = pygame.mixer.Sound('winner.wav')
   
    hole_sound = pygame.mixer.Sound('hole.wav')
   


   
    GAMEOVER = USEREVENT
    pygame.mixer.music.set_endevent(GAMEOVER)
   

    bg_size = width, height = 1024, 681
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('Play the ball - FishC Demo')


    background = pygame.image.load(bg_image).convert_alpha()

    balls = []
    group = pygame.sprite.Group()


   
    for i in range(5):
   
        position = randint(0, width-100), randint(0, height-100)
        speed = [randint(-10, 10), randint(-10, 10)]
        ball = Ball(ball_image, position, speed, bg_size)
        while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):
            ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
        balls.append(ball)
        group.add(ball)

   
    glass = Glass(glass_image, mouse_image, bg_size)
   

    clock = pygame.time.Clock()

            
   
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
               

            elif event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                running = False


        
        
        screen.blit(background, (0, 0))
        
        screen.blit(glass.glass_image, glass.glass_rect) # 注意:不能直接使用glass_image和glass_rect!!!
        


        
        glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
      

        
        if glass.mouse_rect.left < glass.glass_rect.left: # 若鼠标的位置的左侧小于玻璃面板左侧的位置的坐标,表明已出界需归正
            glass.mouse_rect.left = glass.glass_rect.left
        if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width: # 右侧
            glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
        if glass.mouse_rect.top < glass.glass_rect.top: # 上部
            glass.mouse_rect.top = glass.glass_rect.top
        if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height: # 下部
            glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height


      
        screen.blit(glass.mouse_image, glass.mouse_rect)
        
            
        
        for each in balls:
            each.move()
            screen.blit(each.image, each.rect)
            


        for each in group:
            group.remove(each)
            
            if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]
               
            group.add(each)


      


      
        pygame.display.flip()
        clock.tick(30)
        


if __name__ == '__main__':
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-31 10:59:49 | 显示全部楼层
lzb1001 发表于 2022-7-31 10:42
重大发现:

小甲鱼的书面教材中有一行代码定义了鼠标光标的初始化位置,而教学视频中则恰好漏掉了这 ...

似乎没有意义吧....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-31 11:00:28 | 显示全部楼层

那个中心坐标需要经过偏移后才算真正中心!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 11:06:32 | 显示全部楼层
python爱好者. 发表于 2022-7-31 11:00
那个中心坐标需要经过偏移后才算真正中心!

谢谢大神提醒

我研究下:

那么想要算出舞台中心横轴(x轴)位置,就需要算出舞台宽度的一半(width // 2 = 8),
然而,这样算出来的并不能算是其真正的中心横轴(x轴)位置(这样算出来的在执行时会发生偏移(不在中心)(不信你可以自己尝试)),
因为我们没有考虑到角色本身的宽度(2),所以我们应该再将结果 8 减去 自身宽度的一半 (8 - glass.glass_rect.width // 2 = 7),这样的结果才是中心的 x 位置!
注意:这里使用的是地板除,是为了防止算出的结果有小数,如果算出小数,就会导致程序报错!
寻找中心纵轴(y 轴)坐标,方法也如上!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 11:08:16 | 显示全部楼层

1、有无初始化位置或初始化位置定义在哪确实感觉没有太大意义,我是想学习了解下代码

2、self.mouse_rect.left, self.mouse_rect.top = \
                             self.glass_rect.left, self.glass_rect.top
既然此行代码并不是定义鼠标光标初始化时的位置,那此行代码在整个程序中的作用和意义何在呢???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-31 12:34:35 | 显示全部楼层
lzb1001 发表于 2022-7-31 11:08
1、有无初始化位置或初始化位置定义在哪确实感觉没有太大意义,我是想学习了解下代码

2、self.mouse_r ...

1.嗯
2.为了程序的后期易改性、扩展性
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 16:40:18 | 显示全部楼层
python爱好者. 发表于 2022-7-31 12:34
1.嗯
2.为了程序的后期易改性、扩展性

在代码中加入:

---pygame.mouse.set_pos([self.glass_rect.centerx, self.glass_rect.centery])

经测试,可以初始化鼠标位置在玻璃面板中心(不知道是否是真的中心),但如果考虑坐标轴偏移量,将代码换成:

---pygame.mouse.set_pos([self.glass_rect.width // 2 - self.mouse_rect.width // 2, self.glass_rect.height // 2 - self.mouse_rect.height // 2])

鼠标初始化位置仍在玻璃面板的左上角,知道怎么回事呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-31 16:48:16 | 显示全部楼层
lzb1001 发表于 2022-7-31 16:40
在代码中加入:

---pygame.mouse.set_pos([self.glass_rect.centerx, self.glass_rect.centery])

不会吧......你把源代码发过来让我看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-31 17:00:26 | 显示全部楼层
本帖最后由 lzb1001 于 2022-7-31 17:03 编辑
python爱好者. 发表于 2022-7-31 16:48
不会吧......你把源代码发过来让我看看


import pygame
import sys
from pygame.locals import *
from random import *



class Ball(pygame.sprite.Sprite):

   
    def __init__(self, image, position, speed, bg_size):
        pygame.sprite.Sprite.__init__(self)
      

      
        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.speed = speed
        self.width, self.height = bg_size[0], bg_size[1]
        self.radius = self.rect.width / 2
        


   
    def move(self):
        self.rect = self.rect.move(self.speed)

      
        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.bottom < 0:
            self.rect.top = self.height

        elif self.rect.top > self.height:
            self.rect.bottom = 0



class Glass(pygame.sprite.Sprite):

   
    def __init__(self, glass_image, mouse_image, bg_size):
       pygame.sprite.Sprite.__init__(self)


      
       self.glass_image = pygame.image.load(glass_image).convert_alpha()
       self.glass_rect = self.glass_image.get_rect()
       self.glass_rect.left, self.glass_rect.top = \
                             (bg_size[0] - self.glass_rect.width) // 2, \
                             (bg_size[1] - self.glass_rect.height)
      

       self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
       self.mouse_rect = self.mouse_image.get_rect()
       self.mouse_rect.left, self.mouse_rect.top = \
                             self.glass_rect.left, self.glass_rect.top # 此行代码并不是定义鼠标光标初始化时的位置!----此行代码的作用和意义???
      

       # 鼠标光标初始化时的位置设置为屏幕左上角:
       #pygame.mouse.set_pos([self.glass_rect.left, self.glass_rect.top]) # 小甲鱼书面教材中有这行,但教学视频中不知什么原因遗漏

      
       # 将鼠标光标初始化的位置定义在玻璃面板正中央:
       #pygame.mouse.set_pos([self.glass_rect.centerx, self.glass_rect.centery])

       # 考虑坐标轴的偏移量,按如下代码设置,但经测试但鼠标光标并不在玻璃面板正中心的位置!!!
       pygame.mouse.set_pos([self.glass_rect.width // 2 - self.mouse_rect.width // 2, self.glass_rect.height // 2 - self.mouse_rect.height // 2])



       # 将原鼠标光标设置为不可见!
       pygame.mouse.set_visible(False)
      
           


def main():
    pygame.init()


   
    ball_image = 'gray_ball.png'
    glass_image = 'glass.png'
    mouse_image = 'hand.png'
    bg_image = 'background.png'


    running = True


   
    pygame.mixer.music.load('bg_music.ogg')
   
    pygame.mixer.music.play()
   

   
    loser_sound = pygame.mixer.Sound('loser.wav')
   
    laugh_sound = pygame.mixer.Sound('laugh.wav')
   
    winner_sound = pygame.mixer.Sound('winner.wav')
   
    hole_sound = pygame.mixer.Sound('hole.wav')
   


   
    GAMEOVER = USEREVENT
    pygame.mixer.music.set_endevent(GAMEOVER)
   

    bg_size = width, height = 1024, 681
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('Play the ball - FishC Demo')


    background = pygame.image.load(bg_image).convert_alpha()

    balls = []
    group = pygame.sprite.Group()


   
    for i in range(5):
   
        position = randint(0, width-100), randint(0, height-100)
        speed = [randint(-10, 10), randint(-10, 10)]
        ball = Ball(ball_image, position, speed, bg_size)
        while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):
            ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
        balls.append(ball)
        group.add(ball)

   
    glass = Glass(glass_image, mouse_image, bg_size)
   

    clock = pygame.time.Clock()

            
   
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
               

            elif event.type == GAMEOVER:
                loser_sound.play()
                pygame.time.delay(2000)
                laugh_sound.play()
                running = False


        
        
        screen.blit(background, (0, 0))
        
        screen.blit(glass.glass_image, glass.glass_rect) # 注意:不能直接使用glass_image和glass_rect!!!
        


        
        glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
      

        
        if glass.mouse_rect.left < glass.glass_rect.left: # 若鼠标的位置的左侧小于玻璃面板左侧的位置的坐标,表明已出界需归正
            glass.mouse_rect.left = glass.glass_rect.left
        if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width: # 右侧
            glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
        if glass.mouse_rect.top < glass.glass_rect.top: # 上部
            glass.mouse_rect.top = glass.glass_rect.top
        if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height: # 下部
            glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height


      
        screen.blit(glass.mouse_image, glass.mouse_rect)
        
            
        
        for each in balls:
            each.move()
            screen.blit(each.image, each.rect)
            


        for each in group:
            group.remove(each)
            
            if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]
               
            group.add(each)


      


      
        pygame.display.flip()
        clock.tick(30)
        


if __name__ == '__main__':
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 10:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表