PyGame运动

版权声明:本文为博主原创文章,转载请注明出处: https://blog.csdn.net/Hubz131/article/details/86757000

一、直线运动

下面这个程序让“hello world”程序中的鱼动起来。

# -*- coding: utf-8 -*- 
# Time : 2019/2/3 12:07 
# Author : hubozhi
import pygame
from pygame.locals import *
from sys import exit
import time
background_image = './image/sushiplate.jpg'
sprite_image = './image/fugu.png'

if __name__=="__main__":
    pygame.init()

    screen = pygame.display.set_mode((640, 480), 0, 32)
    background = pygame.image.load(background_image).convert()
    sprite = pygame.image.load(sprite_image)
    width =sprite.get_width()
    # 大小:get_size 高:get_height  宽:get_width
    # sprite的起始坐标
    x = -width

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:exit()

        screen.blit(background, (0, 0))
        screen.blit(sprite, (x, 100))

        x += 1
        #消失多少,出现多少
        if x>640-width:
            screen.blit(sprite,(-(640-x),100))
            
        if x>640:x=0
        time.sleep(0.005)
        pygame.display.update()

二、时间

在上面的程序中,帧率是很高的。而且电脑的性能不同,鱼的速度就会不同,如果动画的的元素很多,速度就会下降。

为了解决这个问题,可以使用pygame的时间模块。

clock = pygame.time.Clock()

time_passed = clock.tick()

time_passed = clock.tick(30)

第一行初始化了一个Clock对象。第二行返回了距上一次调用这个函数,过去了多长时间(注意,得到的值是以毫秒为单位的)。第三行,在函数中添加了framerate参数,这个函数会延时使得游戏的帧率不会超过给定值。

给定的值仅仅是最大帧率,当动作复杂或机器性能不足时,实际帧率无法达到这个值,需要一种手段控制动画效果。比如给吾提一个恒定的速度,再通过时间,计算出移动的距离(代码如下)。

# -*- coding: utf-8 -*-
# Time : 2019/2/3 12:07
# Author : hubozhi
import pygame
from pygame.locals import *
from sys import exit
import time
background_image = './image/sushiplate.jpg'
sprite_image = './image/fugu.png'

if __name__=="__main__":
    pygame.init()

    screen = pygame.display.set_mode((640, 480), 0, 32)
    background = pygame.image.load(background_image).convert()
    sprite = pygame.image.load(sprite_image)
    width =sprite.get_width()
    click = pygame.time.Clock()
    # 大小:get_size 高:get_height  宽:get_width
    clock = pygame.time.Clock()
    speed = 250
    # sprite的起始坐标
    x = -width

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:exit()

        screen.blit(background, (0, 0))
        screen.blit(sprite, (x, 100))
        time_passed = clock.tick()
        time_passed_seconds = time_passed/1000
        distance_moved = time_passed_seconds * speed
        x += distance_moved
        #消失多少,出现多少
        if x>640-width:
            screen.blit(sprite,(-(640-x),100))

        if x>640:x=0

        pygame.display.update()

三、斜线运动

接下来这个程序,使得物体斜线运动并且触边反弹。

# -*- coding: utf-8 -*- 
# Time : 2019/2/3 13:36 
# Author : hubozhi
import pygame
from pygame.locals import *
from sys import exit

background_image = './image/sushiplate.jpg'
sprite_image = './image/fugu.png'

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode((640, 480), 0, 32)
    background = pygame.image.load(background_image).convert()
    sprite = pygame.image.load(sprite_image)
    clock = pygame.time.Clock()
    x, y = 100, 100
    speed_x, speed_y = 133, 170

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        screen.blit(background, (0, 0))



        time_passed = clock.tick(30)
        time_passed_seconds = time_passed / 1000
        x += int(speed_x * time_passed_seconds)
        y += int(speed_y * time_passed_seconds)
        
        screen.blit(sprite, (x, y))

        # 到达边界后速度反向
        if x > 640 - sprite.get_width():
            speed_x = -speed_x
            x = 640 - sprite.get_width()
        elif x < 0:
            speed_x = -speed_x

        if y > 480 - sprite.get_height():
            speed_y = -speed_y
            y = 480 - sprite.get_height()
        elif y < 0:
            speed_y = -speed_y
            y = 0
        pygame.display.update()

四、向量

下面这个例子,使用向量代替之前的x和y的计算,实现了鱼在鼠标周围游动的效果。

使用向量类来存储和计算向量。

猜你喜欢

转载自blog.csdn.net/Hubz131/article/details/86757000