• Python:pygame游戏编程之旅三(玩家控制的小球)


    上一节实现了小球自由移动,本节在上节基础上增加通过方向键控制小球运动,并为游戏增加了背景图片。

    一、实现:

    1. # -*- coding:utf-8 -*-  
    2.   
    3. import os  
    4. import sys  
    5.   
    6. import pygame  
    7. from pygame.locals import *  
    8.   
    9.   
    10. def load_image(pic_name):  
    11.     ''''' 
    12.     Function:图片加载函数 
    13.     Input:NONE 
    14.     Output: NONE 
    15.     author: socrates 
    16.     blog:http://blog.csdn.net/dyx1024 
    17.     date:2012-04-15 
    18.     '''  
    19.     #获取当前脚本文件所在目录的绝对路径  
    20.     current_dir = os.path.split(os.path.abspath(__file__))[0]  
    21.       
    22.     #指定图片目录  
    23.     path = os.path.join(current_dir, 'image', pic_name)  
    24.       
    25.     #加载图片  
    26.     return pygame.image.load(path).convert()  
    27.      
    28. def control_ball(event):  
    29.     ''''' 
    30.     Function:控制小球运动 
    31.     Input:NONE 
    32.     Output: NONE 
    33.     author: socrates 
    34.     blog:http://blog.csdn.net/dyx1024 
    35.     date:2012-04-15 
    36.     '''      
    37.     #相对偏移坐标  
    38.     speed = [x, y] = [00]  
    39.       
    40.     #速度  
    41.     speed_offset = 1  
    42.       
    43.     #当方向键按下时,进行位置计算  
    44.     if event.type == pygame.KEYDOWN:  
    45.         if event.key == pygame.K_LEFT:  
    46.             speed[0] -= speed_offset  
    47.         if event.key == pygame.K_RIGHT:  
    48.             speed[0] = speed_offset  
    49.         if event.key == pygame.K_UP:  
    50.             speed[1] -= speed_offset  
    51.         if event.key == pygame.K_DOWN:  
    52.             speed[1] = speed_offset  
    53.       
    54.     #当方向键释放时,相对偏移为0,即不移动  
    55.     if event.type in (pygame.KEYUP, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_DOWN) :  
    56.         speed = [00]  
    57.               
    58.     return speed  
    59.               
    60. def play_ball():  
    61.     ''''' 
    62.     Function:主函数 
    63.     Input:NONE 
    64.     Output: NONE 
    65.     author: socrates 
    66.     blog:http://blog.csdn.net/dyx1024 
    67.     date:2012-04-15 
    68.     '''      
    69.     pygame.init()  
    70.       
    71.     #窗口大小  
    72.     window_size = Rect(00700500)  
    73.       
    74.     #设置窗口模式  
    75.     screen = pygame.display.set_mode(window_size.size)  
    76.       
    77.     #设置窗口标题  
    78.     pygame.display.set_caption('运动的小球(2)-通过方向键控制小球移动')  
    79.       
    80.     #加载小球图片  
    81.     ball_image = load_image('ball.gif')  
    82.       
    83.     #加载窗口背景图片  
    84.     back_image = load_image('back_image.jpg')  
    85.       
    86.     #获取小球图片的区域开状  
    87.     ball_rect = ball_image.get_rect()  
    88.       
    89.     while True:  
    90.           
    91.         #退出事件处理  
    92.         for event in pygame.event.get():  
    93.             if event.type == pygame.QUIT:  
    94.                 pygame.quit()  
    95.                 sys.exit()  
    96.           
    97.         #使小球移动,速度由speed变量控制  
    98.         cur_speed = control_ball(event)  
    99.           
    100.         #Rect的clamp方法使用移动范围限制在窗口内  
    101.         ball_rect = ball_rect.move(cur_speed).clamp(window_size)  
    102.           
    103.         #设置窗口背景  
    104.         screen.blit(back_image, (00))  
    105.                
    106.         #在背景Surface上绘制 小球  
    107.         screen.blit(ball_image, ball_rect)  
    108.           
    109.         #更新窗口内容  
    110.         pygame.display.flip()  
    111.           
    112. if __name__ == '__main__':  
    113.     play_ball()  

    二、测试

    1、开始运行,小球位于窗口(0, 0)坐标处。


    2、按下向右方向键,使用小球向右移动


    3、按下向下方向键,使用小球向下移动。



  • 相关阅读:
    个人博客作业Week2
    个人作业
    软件工程——个人博客作业三
    软工个人博客二
    软件工程——个人博客作业一
    Linux服务器搭建常用环境(一)
    Swift学习之语言基础
    Python——函数的参数
    Python——函数的调用
    Python 导语
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959458.html
Copyright © 2020-2023  润新知