Python壁球小游戏(3)

在壁球小游戏(2)的基础上添加新的功能。使用鼠标控制壁球的移动:当鼠标左键按下时使壁球停止移动,当鼠标左键释放时使壁球移动到鼠标按下的位置并让鼠标继续移动;按下鼠标左键时拖动鼠标使壁球跟随着鼠标移动。学习了如何使配合鼠标使游戏能有更多的操作,增加游戏的可玩性。笔记都在代码的注释中:

import sys
import pygame

pygame.init()
#icon = pygame.image.load("biu.jpg")
#pygame.display.set_icon(icon)          #设置窗口图标
#Vinfo = pygame.display.Info()       #获取当前操作系统的屏幕尺寸
#size = width, height = Vinfo.current_w, Vinfo.current_h     #将系统的屏幕尺寸作为显示窗口的尺寸
size = width,height = 900 ,600               #设置屏幕的长度和宽度
speed = [1,1]                                #设置移动速度[x,y]
BLACK = 0,0,0                                #设置背景颜色
#screen = pygame.display.set_mode(size)       #初始化显示窗口
screen = pygame.display.set_mode(size,pygame.RESIZABLE)       #将屏幕设置为大小可调
#screen = pygame.display.set_mode(size,pygame.NOFRAME)         #将屏幕设置为无边框
#screen = pygame.display.set_mode(size,pygame.FULLSCREEN)       #将屏幕设置为全屏模式
pygame.display.set_caption("壁球游戏")       #将窗口命名为“壁球游戏”
ball = pygame.image.load("biu.jpg")          #加载图片
ballrect = ball.get_rect()                   #返回一个覆盖图像的矩形Rect对象,载入图像的外切矩形
"""Rect对象有一些重要的属性,比如top、bottom、left、right表示上下左右
width、height表示宽度和高度"""
fps = 300
fclock = pygame.time.Clock()
still = False


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            #按左键改变壁球左右移动方向,按一次方向取反
            if event.key == pygame.K_LEFT:
                speed[0] = -speed[0]
            #按上键改变壁球上下移动方向,按一次方向取反
            elif event.key == pygame.K_UP:
                speed[1] = -speed[1]
            elif event.key == pygame.K_ESCAPE:
                sys.exit("游戏结束!")
        elif event.type == pygame.VIDEORESIZE:
            size = width, height = event.size[0], event.size[1]
            screen = pygame.display.set_mode(size,pygame.RESIZABLE)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
                """当鼠标左键按下时,将控制壁球移动的变量设置为True,
                让壁球停止移动,因为后边是用if pygame.display.get_active() and not still:
                来控制壁球移动的,将still取反了"""
        elif event.type == pygame.MOUSEBUTTONUP:
            still = False
            if event.button == 1:
                ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
                """当鼠标按键释放时,让壁运动的相对球移动到鼠标所在的位置,同时让壁球继续运动
                move函数的参数是指两次距离,将两个位置的相对距离赋值给函数
                """
        elif event.type == pygame.MOUSEMOTION:
            if event.buttons[0] == 1:         # 当鼠标左键按下的时候
                ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
                """当鼠标左键处于按下状态并且在移动时,使壁球的图片跟随着鼠标一起移动"""

    if pygame.display.get_active() and not still:
        ballrect = ballrect.move(speed[0],speed[1])     #在横轴和纵轴移动x,y像素
        """ 设置窗口感知,当显示为界面的时候,返回True,图标化的时候返回False
        这样设置如果将窗口最小化,壁球会停止运动
        当打开的时候壁球将会继续运动"""
    #判断图像的边缘位置,如果碰到上下左右边缘,使其速度取反
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
        #sys.exit("Game Over!")
        if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
            speed[0] = - speed[0]
            """如果壁球的右边缘的坐标位置大于屏幕宽度,则让壁球改变运动方向"""

    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]
        #sys.exit("Game Over")
        if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
            speed[1] = - speed[1]

    screen.fill(BLACK)          #填充背景颜色
    screen.blit(ball,ballrect)
    """将一个图像绘制在另一个图像上,即将ball绘制到ballrect位置上。
    通过Rect对象引导对移动图像的绘制,让图像跟着外切矩形的移动而移动
    这样就形成了图像的移动"""
    pygame.display.update()     #刷新屏幕,仅仅重新绘制窗口中有变化的地方,比flip函数速度更快,flip函数重新绘制整个窗口
    fclock.tick(fps)



发布了61 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41575507/article/details/96762436