定义鼠标光标初始化的位置
import pygameimport 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, bg_size
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 - self.glass_rect.width) // 2, \
(bg_size - 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 =
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 = -each.speed
each.speed = -each.speed
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------
【我的问题】
1、红色部分代码好像运行后并无法将光标默认在玻璃面板的左上角,那代码的意思和作用何在?
2、如果要将光标初始化定义在玻璃面板的正中央的话,代码应如何修改?
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! 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 轴)坐标,方法也如上! 本帖最后由 lzb1001 于 2022-7-31 10:20 编辑
python爱好者. 发表于 2022-7-31 05:15
1.不,那是因为运行速度足够快,导致你看不到其最开始位置的!
2.应改为:
感谢大神的热心指点。
1、点击运行后,我就放开鼠标了,但进入游戏画面后鼠标也不在玻璃面板的左上角啊
2、这里假设:width,height,glass,glass_rect = 16,16,2
等号左边四个变量名,右边三个数值
是否有错? lzb1001 发表于 2022-7-31 10:15
感谢大神的热心指点。
1、点击运行后,我就放开鼠标了,但进入游戏画面后鼠标也不在玻璃面板的左上 ...
1.可能你会有轻微的晃动.....吧...
2.把 "." 写成 "," 了,抱歉,应该为:
width,height,glass.glass_rect = 16,16,2 本帖最后由 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, bg_size
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 - self.glass_rect.width) // 2, \
(bg_size - 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() # 小甲鱼书面教材中有这行,但教学视频中不知什么原因遗漏,而我是照着视频中的代码敲击,所以运行后鼠标光标并不在玻璃面板的正中央!
# 若想将鼠标光标初始化的位置定义在玻璃面板正中央,则:
#pygame.mouse.set_pos()
# 将原鼠标光标设置为不可见!
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 =
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 = -each.speed
each.speed = -each.speed
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main() lzb1001 发表于 2022-7-31 10:42
重大发现:
小甲鱼的书面教材中有一行代码定义了鼠标光标的初始化位置,而教学视频中则恰好漏掉了这 ...
似乎没有意义吧.... python爱好者. 发表于 2022-7-31 10:59
似乎没有意义吧....
那个中心坐标需要经过偏移后才算真正中心! python爱好者. 发表于 2022-7-31 11:00
那个中心坐标需要经过偏移后才算真正中心!
谢谢大神提醒
我研究下:
那么想要算出舞台中心横轴(x轴)位置,就需要算出舞台宽度的一半(width // 2 = 8),
然而,这样算出来的并不能算是其真正的中心横轴(x轴)位置(这样算出来的在执行时会发生偏移(不在中心)(不信你可以自己尝试)),
因为我们没有考虑到角色本身的宽度(2),所以我们应该再将结果 8 减去 自身宽度的一半 (8 - glass.glass_rect.width // 2 = 7),这样的结果才是中心的 x 位置!
注意:这里使用的是地板除,是为了防止算出的结果有小数,如果算出小数,就会导致程序报错!
寻找中心纵轴(y 轴)坐标,方法也如上! python爱好者. 发表于 2022-7-31 10:59
似乎没有意义吧....
1、有无初始化位置或初始化位置定义在哪确实感觉没有太大意义,我是想学习了解下代码
2、self.mouse_rect.left, self.mouse_rect.top = \
self.glass_rect.left, self.glass_rect.top
既然此行代码并不是定义鼠标光标初始化时的位置,那此行代码在整个程序中的作用和意义何在呢??? lzb1001 发表于 2022-7-31 11:08
1、有无初始化位置或初始化位置定义在哪确实感觉没有太大意义,我是想学习了解下代码
2、self.mouse_r ...
1.嗯
2.为了程序的后期易改性、扩展性 python爱好者. 发表于 2022-7-31 12:34
1.嗯
2.为了程序的后期易改性、扩展性
在代码中加入:
---pygame.mouse.set_pos()
经测试,可以初始化鼠标位置在玻璃面板中心(不知道是否是真的中心),但如果考虑坐标轴偏移量,将代码换成:
---pygame.mouse.set_pos()
鼠标初始化位置仍在玻璃面板的左上角,知道怎么回事呢? lzb1001 发表于 2022-7-31 16:40
在代码中加入:
---pygame.mouse.set_pos()
不会吧......你把源代码发过来让我看看 本帖最后由 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, bg_size
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 - self.glass_rect.width) // 2, \
(bg_size - 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() # 小甲鱼书面教材中有这行,但教学视频中不知什么原因遗漏
# 将鼠标光标初始化的位置定义在玻璃面板正中央:
#pygame.mouse.set_pos()
# 考虑坐标轴的偏移量,按如下代码设置,但经测试但鼠标光标并不在玻璃面板正中心的位置!!!
pygame.mouse.set_pos()
# 将原鼠标光标设置为不可见!
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 =
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 = -each.speed
each.speed = -each.speed
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
页:
[1]