内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

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

时间:2015-02-22 23:37:31 来源: 作者: 人气: 评论:

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

 代码:

 
  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、

 [URL]be8621fe87559cb73f38bd48755e93ed.jpg

2、

[URL]47346424f02dee061ca0a3e78ddb6c50.jpg

3、

[URL]8871c95f534336df77553a0950455daa.jpg

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()  
  66.  
分享给小伙伴们:
本文标签:Python游戏  

相关文章

    无相关信息

发表评论共有条评论

友情链接1 百悠游站点 |

友情链接2: 帝国CMS |

百悠游站点,电脑学习,游戏网赚分享,游戏工作室门户网站。

Copyright (C) buyzd.cn, All Rights Reserved.

百悠游站点 版权所有