Python壁球小游戏(操控型)与键盘的基本使用

一,壁球小游戏(操控型)             

被用户操控着且四处碰壁的小球

 二,壁球小游戏(操控型)的关键要素

 需求:通过键盘的上下左右控制壁球运动速度,规则如下∶

三,壁球小游戏(操控型)的关键要素

壁球小游戏(操控型)的关键要素

键盘使用︰如何获取键盘的操作事件

速度调节︰根据对应按键调节壁球运动速度

 四,壁球小游戏(操控型)     源代码来了!!!!!

   

import pygame,sys

pygame.init()
size = width,height = 600,400
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()

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] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1  if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
    ballrect = ballrect.move(speed[0],speed[1])
    if ballrect.left < 0 or ballrect.right >width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    screen.fill(BLACK)
    screen.blit(ball,ballrect)
    pygame.display.update()
    fclock.tick(fps)

五,壁球小游戏(节奏型)

 六,代码讲解

import pygame,sys

# 初始化
pygame.init()
size = width,height = 600,400
speed = [1,1]
BLACK = 0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
# pygame. image. load(filename)
# 将filename路径下的图像载入游戏,支持JPG、PNG.GlF(非动画)等13种常用图片格式
ball = pygame.image.load("PYG02-ball.gif")
# Surface对象 ball.get_rect()
# Pygame使用内部定义的Surface对象表示所有载入的图像,其中.get_rect()方法返回一个覆盖图像的矩形Rect对象
# Rect对象
# Rect对象有一些重要属性,例如:top,bottom,left,right表示上下左右width,height表示宽度、高度
ballrect = ball.get_rect()
fps = 300 #Frames per Second每秒帧率参数
# pygame.time.Clock() 创建一个Clock对象,用于操作时间
fclock = pygame.time.Clock()


# 事件处理
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # pygame.KEYDOWN
        # Pygame对键盘敲击的事件定义,键盘每个键对应一个具体定义
        elif event.type == pygame.KEYDOWN:
            # 左
            if event.key == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
            # 右
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
            # 上
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] + 1  if speed[1] > 0 else speed[1] - 1
            # 下
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
    #ballrect.move(x,y)
    #矩形移动一个偏移量(x,y),即在横轴方向移动x像素,纵轴方向移动y像素,xy为整数
    ballrect = ballrect.move(speed[0],speed[1])
    # 壁球的反弹运动
    # 遇到左右两侧,横向速度取反;遇到上下两侧,纵向速度取反。
    if ballrect.left < 0 or ballrect.right >width:
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]


    # 窗口刷新
    # screen.fill(color)
    # 显示窗口背景填充为color颜色,采用RGB色彩体系。由于壁球不断运动,运动后原有位置将默认填充白色,因此需要不断刷新背景色
    screen.fill(BLACK)
    # screen.blit(src, dest)
    # 将一个图像绘制在另一个图像上,即将src绘制到dest位置上。通过Rect对象引导对壁球的绘制。
    screen.blit(ball,ballrect)
    pygame.display.update()
    # clock.tick(framerate)
    # 控制帧速度, 即窗口刷新速度, 例如: clock.tick(100)
    # 表示每秒钟100次帧刷新视频中每次展示的静态图像称为帧
    fclock.tick(fps)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_54822781

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值