• 周三. 4 月 22nd, 2026

物嫩软件资讯网

软件资讯来物嫩

射击游戏demo6

admin@wunen

5 月 30, 2025

给角色添加重力和跳跃动作

import pygame
import sys
import os
# 初始化Pygame
pygame.init()

# 屏幕宽度
SCREEN_WIDTH = 1200
# 屏幕高度,为屏幕宽度的0.5倍
SCREEN_HEIGHT = int(SCREEN_WIDTH * 0.5)

# 创建一个指定尺寸的显示表面对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置窗口标题
pygame.display.set_caption('射击游戏')

# 创建一个新的时钟对象来跟踪时间
clock = pygame.time.Clock()
# FPS(每秒帧数):帧率是指一秒内出现的帧数。
# 频率,即连续图像被捕获或显示的频率。
# 设置帧率
FPS = 60

# 定义游戏变量
# 重力值
GRAVITY = 0.75

# 定义玩家动作变量
# 是否向左移动
moving_left = False
# 是否向右移动
moving_right = False

# 定义颜色
# 背景颜色
BG = (144, 201, 120)
# 红色
RED = (255, 0, 0)
# 白色
WHITE = (255, 255, 255)
# 绿色
GREEN = (0, 255, 0)
# 黑色
BLACK = (0, 0, 0)
# 粉色
PINK = (235, 65, 54)

def draw_bg():
    # 用背景颜色填充屏幕
    screen.fill(BG)
    pygame.draw.line(screen,RED,(0,300),(SCREEN_WIDTH,300))

class Soldier(pygame.sprite.Sprite):
    def __init__(self,char_type, x,y,scale,speed):
        super().__init__()
        self.char_type=char_type
        self.speed = speed  # 移动速度
        self.direction = 1  # 方向(1为右,-1为左)
        self.vel_y = 0  # 垂直速度
        self.jump = False  # 跳跃状态
        self.in_air = True  # 是否在空中
        self.flip = False  # 是否翻转图像(左右方向)
        self.animation_list = []  # 动画帧列表
        self.frame_index = 0  # 当前动画帧索引
        self.action = 0  # 当前动作(0:闲置, 1:奔跑, 2:跳跃, 3:死亡)
        self.update_time = pygame.time.get_ticks()  # 上次更新时间

        # 加载角色所有动画
        animation_types = ['Idle', 'Run', 'Jump', 'Death']
        for animation in animation_types:
            temp_list = []  # 临时存储当前动作的帧
            # 计算动画帧数
            num_of_frames = len(os.listdir(f'img/{self.char_type}/{animation}'))
            for i in range(num_of_frames):
                # 加载并缩放图像
                img = pygame.image.load(f'img/{self.char_type}/{animation}/{i}.png').convert_alpha()
                img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
                temp_list.append(img)
            self.animation_list.append(temp_list)
        
        # 设置初始图像和碰撞矩形
        self.image = self.animation_list[self.action][self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.width = self.image.get_width()
        self.height = self.image.get_height()

        
    def draw(self):
    # 绘制角色到屏幕
        flipped_image = pygame.transform.flip(self.image, self.flip, False)
        screen.blit(flipped_image, self.rect)

    def move(self, moving_left, moving_right):
            # 初始化移动变量
            screen_scroll = 0
            dx = 0
            dy = 0
            
            # 处理水平移动
            if moving_left:
                dx = -self.speed
                self.flip = True
                self.direction = -1
            if moving_right:
                dx = self.speed
                self.flip = False
                self.direction = 1

            # 处理跳跃
            if self.jump and not self.in_air:
                self.vel_y = -11
                self.jump = False
                self.in_air = True
            
            # 应用重力
            self.vel_y += GRAVITY
            if self.vel_y > 10:  # 限制最大下落速度
                self.vel_y = 10
            dy += self.vel_y

            # 检测是否掉落出地图
            if self.rect.bottom +dy>300:
                dy=300-self.rect.bottom
                self.in_air=False

            # 更新位置
            self.rect.x += dx
            self.rect.y += dy

player =Soldier("player",200,200,3,5)
player2 =Soldier("enemy",400,200,3,5)


run = True
while run:  # 游戏主循环
    clock.tick(FPS)  # 设置帧率
    draw_bg()
    player.move(moving_left,moving_right)
    player.draw()
    player2.draw()
    
    
    for event in pygame.event.get():
        # 处理退出游戏事件
        if event.type == pygame.QUIT:
                run = False
                sys.exit()
        # 处理键盘按下事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:  # 按下A键或左键 => 向左移动
                moving_left = True
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:  # 按下D键或右键 => 向右移动
                moving_right = True
            if (event.key == pygame.K_w or event.key == pygame.K_UP) and player.alive:  # 按下W键或上键且玩家存活 => 跳跃
                player.jump = True
                
            if event.key == pygame.K_ESCAPE:  # 按下ESC键 => 关闭窗口
                run = False
                # 处理键盘释放事件
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                moving_left = False
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                moving_right = False
            if event.key == pygame.K_SPACE:
                shoot = False
            if event.key == pygame.K_q or event.key == pygame.K_g:
                grenade = False
                grenade_thrown = False

    pygame.display.update()  # 更新屏幕显示

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注