import sys
import pygame
from pygame.locals import *

pygame.init()  # 初始化pygame模块
WIDTH = 1920  # 定义游戏窗口的宽
HEIGHT = 1080  # 定义游戏窗口的高
SCREEN_SIZE = (WIDTH, HEIGHT)  # 定义一个元组
screen = pygame.display.set_mode(SCREEN_SIZE)  # 初始化一个显示画面的窗口
pygame.display.set_caption("MyPygame")  # 设置标题

r=0
g=0
b=0
blue = (r, g, b)  # 蓝色

fish = pygame.image.load("./PNG/Guppy0.png")  # 加载一张图片 ./ 表示当前目录
pos = fish.get_rect()  # 获取图片的坐标
speed = [3, 3]  # 设定速度,分别是x方向和y方向


while True:  # 游戏主循环
    for event in pygame.event.get():  # 获取事件
        if event.type == QUIT:  # 退出
            pygame.quit()  # 退出pygame模块
            sys.exit()  # 退出系统

    pos = pos.move(speed)  # 移动图像
    if pos.top<0 or pos.bottom>HEIGHT:
        speed[1]=-speed[1]
    if pos.left<0 or pos.right > WIDTH:
        speed[0]=-speed[0]
    
    blue = (r, g, b)  # 蓝色
    screen.fill(blue)  # 用纯色填充窗口
    if r<255:
        r=r+0.1
    else :
        r=0
    screen.blit(fish, pos)  # 绘制角色图像

    pygame.display.update()  # 更新窗口画面显示

设置运行速度

import sys
import pygame
from pygame.locals import *

pygame.init()  # 初始化pygame模块
WIDTH = 800  # 定义游戏窗口的宽
HEIGHT = 480  # 定义游戏窗口的高
FPS = 60
SCREEN_SIZE = (WIDTH, HEIGHT)  # 定义一个元组
screen = pygame.display.set_mode(SCREEN_SIZE)  # 初始化一个显示画面的窗口
pygame.display.set_caption("MyPygame")  # 设置标题

blue = (0, 162, 232)  # 蓝色

fish = pygame.image.load("./PNG/Guppy3.png")  # 加载一张图片 ./ 表示当前目录
pos = fish.get_rect()  # 获取图片的坐标
speed = [1, 1]  # 设定速度,分别是x方向和y方向
clock=pygame.time.Clock()

while True:  # 游戏主循环
    for event in pygame.event.get():  # 获取事件
        if event.type == QUIT:  # 退出
            pygame.quit()  # 退出pygame模块
            sys.exit()  # 退出系统

    pos = pos.move(speed)  # 移动图像
    if pos.left < 0 or pos.right > WIDTH:
        speed[0] = -speed[0]
        fish = pygame.transform.flip(fish, True, False)
    if pos.top < 0 or pos.bottom > HEIGHT:
        speed[1] = -speed[1]

    screen.fill(blue)  # 用纯色填充窗口
    screen.blit(fish, pos)  # 绘制图像

    pygame.display.update()  # 更新窗口画面显示
    clock.tick(60)


0 条评论

目前还没有评论...