|
楼主 |
发表于 2022-7-31 17:00:26
|
显示全部楼层
本帖最后由 lzb1001 于 2022-7-31 17:03 编辑
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() |
|