zoukankan      html  css  js  c++  java
  • 【pygame游戏编程】第二篇-----移动图像

     Learning From Here

    import pygame
    import sys
    
    pygame.init()
    
    screen_width = 640
    screen_high = 480
    
    screen = pygame.display.set_mode((screen_width, screen_high))
    screen_rect = screen.get_rect()
    
    pygame.display.set_caption("Moving Fish")
    
    background_image_filename = 'sushiplate.png'
    mouse_image_fliename = 'fugu.png'
    
    background_image = pygame.image.load(background_image_filename).convert()
    mouse_image = pygame.image.load(mouse_image_fliename).convert_alpha()
    
    mouse_image_rect = mouse_image.get_rect()
    
    clock = pygame.time.Clock()
    
    while True:
    
        #限制每秒循环100次,防止大量占用cpu
        clock.tick(100)
    
        for event in pygame.event.get():
    
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
    
        screen.blit(background_image, (0, 0))
    
        #获取鼠标位置
        x, y = pygame.mouse.get_pos()
    
        mouse_image_rect.center = (x, y)
    
        #设置图片移动边界
        if mouse_image_rect.left < 0:
            mouse_image_rect.left = 0
        if mouse_image_rect.right > screen_rect.right:
            mouse_image_rect.right = screen_rect.right
        if mouse_image_rect.top < 0:
            mouse_image_rect.top = 0
        if mouse_image_rect.bottom > screen_rect.bottom:
            mouse_image_rect.bottom = screen_rect.bottom
    
        screen.blit(mouse_image, mouse_image_rect)
    
        pygame.display.update()

    运行截图:

     

    本次使用的两张图片资源:

    背景:sushiplate.jpg

    光标:fugu.png

    蒹葭苍苍,白露为霜; 所谓伊人,在水一方。
  • 相关阅读:
    Jmeter运行badboy录制的脚本
    Bugfree安装与使用
    JMeter录制脚本
    第六天-linux系统优化初步讲解
    第五天-linux基础命令
    第四天-secureCRT-ssh客户端使用详解
    第三天-linux版本选择及安装
    第二天-计算机硬件基本知识和linux发展简史
    第一天-学习linux运维
    ubuntu15.04 无线上网问题
  • 原文地址:https://www.cnblogs.com/huwt/p/10349869.html
Copyright © 2011-2022 走看看