• Python:pygame游戏编程之旅二(自由移动的小球)


    本节实现一个在窗口中自由运动的小球程序,做了详细注释,不多做解释了。

     代码:

    1. # -*- coding:utf-8 -*-  
    2.   
    3. import sys  
    4.   
    5. import pygame  
    6. from pygame.locals import *  
    7.   
    8. def play_ball():  
    9.       
    10.     pygame.init()  
    11.       
    12.     #窗口大小  
    13.     window_size = (width, height) =(700500)  
    14.       
    15.     #小球运行偏移量[水平,垂直],值越大,移动越快  
    16.     speed = [11]  
    17.       
    18.     #窗口背景色RGB值  
    19.     color_black = (00139)  
    20.       
    21.     #设置窗口模式  
    22.     screen = pygame.display.set_mode(window_size)  
    23.       
    24.     #设置窗口标题  
    25.     pygame.display.set_caption('运动的小球')  
    26.       
    27.     #加载小球图片  
    28.     ball_image = pygame.image.load('ball.gif')  
    29.       
    30.     #获取小球图片的区域开状  
    31.     ball_rect = ball_image.get_rect()  
    32.       
    33.     while True:  
    34.           
    35.         #退出事件处理  
    36.         for event in pygame.event.get():  
    37.             if event.type == pygame.QUIT:  
    38.                 pygame.quit()  
    39.                 sys.exit()  
    40.           
    41.         #使小球移动,速度由speed变量控制  
    42.         ball_rect = ball_rect.move(speed)  
    43.           
    44.         #当小球运动出窗口时,重新设置偏移量  
    45.         if (ball_rect.left < 0or (ball_rect.right > width):  
    46.             speed[0] =- speed[0]  
    47.         if (ball_rect.top < 0or (ball_rect.bottom > height):  
    48.             speed[1] =- speed[1]  
    49.           
    50.         #填充窗口背景  
    51.         screen.fill(color_black)  
    52.           
    53.         #在背景Surface上绘制 小球  
    54.         screen.blit(ball_image, ball_rect)  
    55.           
    56.         #更新窗口内容  
    57.         pygame.display.update()  
    58.           
    59. if __name__ == '__main__':  
    60.     play_ball()  

    测试:

      动画程序,抓几张不同时刻的图片。

    1、

     

    2、


    3、



    PS:

    有朋友说球速度还是太快了,此时可以加个定时器控制一下,如下:

    1. # -*- coding:utf-8 -*-  
    2.   
    3. import sys  
    4.   
    5. import pygame  
    6. from pygame.locals import *  
    7.   
    8. def play_ball():  
    9.       
    10.     pygame.init()  
    11.       
    12.     #窗口大小  
    13.     window_size = (width, height) =(700500)  
    14.       
    15.     #小球运行偏移量[水平,垂直],值越大,移动越快  
    16.     speed = [11]  
    17.       
    18.     #窗口背景色RGB值  
    19.     color_black = (00139)  
    20.       
    21.     #设置窗口模式  
    22.     screen = pygame.display.set_mode(window_size)  
    23.       
    24.     #设置窗口标题  
    25.     pygame.display.set_caption('运动的小球')  
    26.       
    27.     #加载小球图片  
    28.     ball_image = pygame.image.load('ball.gif')  
    29.       
    30.     #获取小球图片的区域开状  
    31.     ball_rect = ball_image.get_rect()  
    32.       
    33.     frames_per_sec = 10  
    34.     fps_clock = pygame.time.Clock()  
    35.       
    36.     while True:  
    37.           
    38.         #退出事件处理  
    39.         for event in pygame.event.get():  
    40.             if event.type == pygame.QUIT:  
    41.                 pygame.quit()  
    42.                 sys.exit()  
    43.           
    44.         #使小球移动,速度由speed变量控制  
    45.         ball_rect = ball_rect.move(speed)  
    46.           
    47.         #当小球运动出窗口时,重新设置偏移量  
    48.         if (ball_rect.left < 0or (ball_rect.right > width):  
    49.             speed[0] =- speed[0]  
    50.         if (ball_rect.top < 0or (ball_rect.bottom > height):  
    51.             speed[1] =- speed[1]  
    52.           
    53.         #填充窗口背景  
    54.         screen.fill(color_black)  
    55.           
    56.         #在背景Surface上绘制 小球  
    57.         screen.blit(ball_image, ball_rect)  
    58.           
    59.         #更新窗口内容  
    60.         pygame.display.update()  
    61.           
    62.         fps_clock.tick(frames_per_sec)  
    63.           
    64. if __name__ == '__main__':  
    65.     play_ball()  
  • 相关阅读:
    【BZOJ】4636: 蒟蒻的数列
    BZOJ1878 [SDOI2009]HH的项链
    【网络流24题----02】太空飞行计划
    【网络流24题----03】Air Raid最小路径覆盖
    【网络流24题----01】飞行员配对方案问题
    素数判定(米勒测试定理-费马小定理+快速乘)
    一堆模板(丑陋0.0)------数据结构
    丑数(USACO)
    NOI[2001]食物链
    关于Tarjan(2)
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959460.html
Copyright © 2020-2023  润新知