45. Jump Game II

本文介绍了一种算法,用于解决给定非负整数数组中从起始位置到末尾位置所需的最少跳跃次数的问题。该算法通过遍历数组并记录最远可达位置来确定最少跳跃次数。

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

Note:
You can assume that you can always reach the last index.

class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size()<2)
        {
            return 0;
        }
        int current_max_index = nums[0];
        int pre_max_max_index = nums[0];
        int jump_min = 1;
        for(int i=1; i<nums.size(); i++)
        {
            if(i>current_max_index)
            {
                jump_min++;
                current_max_index = pre_max_max_index;
            }
            if(pre_max_max_index<nums[i]+i)
            {
                pre_max_max_index = nums[i] + i;
            }
        }
        return jump_min;
            
    }
};


import pygame import time class Fighter: def __init__(self, x, y, image_path, attack_image_path): self.x = x self.y = y self.speed = 2 self.image = pygame.image.load(image_path) self.attack_image = pygame.image.load(attack_image_path) self.is_jumping = False self.jump_height = 15 self.jump_count = self.jump_height self.health = 100 self.is_attacking = False self.last_attack_time = 0 self.attack_delay = 0.3 def draw(self, screen): if self.is_attacking: screen.blit(self.attack_image, (self.x, self.y)) else: screen.blit(self.image, (self.x, self.y)) def move(self, keys): pass def attack(self, enemy): current_time = time.time() if current_time - self.last_attack_time >= self.attack_delay: if abs(self.x - enemy.x) <= 50: enemy.health -= 2 if enemy.health < 0: enemy.health = 0 self.is_attacking = True self.last_attack_time = current_time else: self.is_attacking = False class Fighter1(Fighter): def move(self, keys): if keys[pygame.K_a] and self.x > 0: self.x -= self.speed if keys[pygame.K_d] and self.x < 750: self.x += self.speed if keys[pygame.K_k] and not self.is_jumping: self.is_jumping = True if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height if keys[pygame.K_j]: self.attack(fighter2) else: self.is_attacking = False class Fighter2(Fighter): def __init__(self, x, y, image_path, attack_image_path): super().__init__(x, y, image_path, attack_image_path) self.image = pygame.transform.scale(self.image, (int(self.image.get_width() * 0.5), int(self.image.get_height() * 0.5))) self.attack_image = pygame.transform.scale(self.attack_image, (int(self.attack_image.get_width() * 0.5), int(self.attack_image.get_height() * 0.5))) def move(self, keys): if keys[pygame.K_LEFT] and self.x > 0: self.x -= self.speed if keys[pygame.K_RIGHT] and self.x < 750: self.x += self.speed if keys[pygame.K_KP2] and not self.is_jumping: self.is_jumping = True if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height if keys[pygame.K_KP1]: self.attack(fighter1) else: self.is_attacking = False def draw_health_bars(screen, fighter1, fighter2): BAR_LENGTH = 300 BAR_HEIGHT = 20 # 角色 1 的血条 fill1 = (fighter1.health / 100) * BAR_LENGTH outline_rect1 = pygame.Rect(20, 20, BAR_LENGTH, BAR_HEIGHT) fill_rect1 = pygame.Rect(20, 20, fill1, BAR_HEIGHT) pygame.draw.rect(screen, (255, 0, 0), outline_rect1, 2) pygame.draw.rect(screen, (0, 255, 0), fill_rect1) # 角色 2 的血条 fill2 = (fighter2.health / 100) * BAR_LENGTH outline_rect2 = pygame.Rect(480, 20, BAR_LENGTH, BAR_HEIGHT) fill_rect2 = pygame.Rect(480, 20, fill2, BAR_HEIGHT) pygame.draw.rect(screen, (255, 0, 0), outline_rect2, 2) pygame.draw.rect(screen, (0, 255, 0), fill_rect2) pygame.init() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("格斗游戏") background_image = pygame.image.load("background.jpg") background_image = pygame.transform.scale(background_image, (width, height)) fighter1 = Fighter1(100, 500, "webwxgetmsgimg.jpg", "attack1.jpg") fighter2 = Fighter2(600, 500, "_cgi-bin_mmwebwx-bin_webwxgetmsgimg__&MsgID=4151463399841909084&skey=@crypt_3b46ed7a_7efc25287b2eeaad8d2392b1642162ed&mmweb_appid=wx_webfilehelper.jpg", "attack2.jpg") running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() fighter1.move(keys) fighter2.move(keys) screen.blit(background_image, (0, 0)) draw_health_bars(screen, fighter1, fighter2) fighter1.draw(screen) fighter2.draw(screen) pygame.display.flip() pygame.quit() 我要让角色的跳跃高度降低
10-29
在使用 Pygame 开发的格斗游戏中降低角色的跳跃高度,可从调整跳跃高度参数和重力参数两方面入手。 ### 调整跳跃高度参数 在 `Fighter` 类的 `__init__` 方法里,减小 `jump_height` 的值,能直接降低角色的跳跃高度。示例代码如下: ```python class Fighter: def __init__(self, x, y, image_path, attack_image_path): # ... 其他属性 ... self.jump_height = 10 # 减小跳跃高度 self.jump_count = self.jump_height # ... 其他属性 ... ``` ### 调整重力参数 在跳跃过程中,可通过调整重力的影响来降低跳跃高度。增加每次跳跃时 `self.jump_count` 的变化量,示例代码如下: ```python if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg # 增大重力影响 self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height ``` ### 完整示例代码 ```python import pygame import time class Fighter: def __init__(self, x, y, image_path, attack_image_path): self.x = x self.y = y self.speed = 2 self.image = pygame.image.load(image_path) self.attack_image = pygame.image.load(attack_image_path) self.is_jumping = False self.jump_height = 10 # 减小跳跃高度 self.jump_count = self.jump_height self.health = 100 self.is_attacking = False self.last_attack_time = 0 self.attack_delay = 0.3 def draw(self, screen): if self.is_attacking: screen.blit(self.attack_image, (self.x, self.y)) else: screen.blit(self.image, (self.x, self.y)) def move(self, keys): if keys[pygame.K_a] and self.x > 0: self.x -= self.speed if keys[pygame.K_d] and self.x < 750: self.x += self.speed if keys[pygame.K_k] and not self.is_jumping: self.is_jumping = True if self.is_jumping: if self.jump_count >= -self.jump_height: neg = 1 if self.jump_count < 0: neg = -1 self.y -= (self.jump_count ** 2) * 0.5 * neg # 增大重力影响 self.jump_count -= 1 else: self.is_jumping = False self.jump_count = self.jump_height if keys[pygame.K_j]: self.attack(fighter2) else: self.is_attacking = False def attack(self, enemy): current_time = time.time() if current_time - self.last_attack_time >= self.attack_delay: if abs(self.x - enemy.x) <= 50: enemy.health -= 2 if enemy.health < 0: enemy.health = 0 self.is_attacking = True self.last_attack_time = current_time else: self.is_attacking = False pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Fighting Game") fighter1 = Fighter(100, 400, "fighter1.png", "fighter1_attack.png") fighter2 = Fighter(600, 400, "fighter2.png", "fighter2_attack.png") running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() fighter1.move(keys) screen.fill((255, 255, 255)) fighter1.draw(screen) fighter2.draw(screen) pygame.display.flip() pygame.quit() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值