如何创建一个盾牌来(指向鼠标)覆盖我的玩家?

问题描述 投票:0回答:1

我的代码:

import pygame
pygame.init()
run = True
Screen_Display = [1500, 1000]

screen = pygame.display.set_mode((Screen_Display))
floor = 725
player = pygame.Rect((500, 720, 50, 50))
bottom = pygame.Rect((0, 775, 1500, 250))
gravity = True
colors = False
left_wall = False
right_wall = False
roof = False
while run:
    #Screen Refresh
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (100, 100, 100), bottom)
    
    if player.y > floor:
        gravity = False
    elif player.y <= floor:
        gravity = True
    if player.y < 0:
        roof = True
    elif player.y > 0:
        roof = False
    if player.x < 1:
        left_wall = True
    elif player.x > 1:
        left_wall = False
    if player.x > 1450:
        right_wall = True
    elif player.x < 1450:
        right_wall = False
    print(player.y)
    print(roof)
    key = pygame.key.get_pressed()
    if roof == False:
        if key[pygame.K_w] == True:
            player.move_ip(0, -1)
    if gravity == True:
        if key[pygame.K_w] == False:
            player.move_ip(0, 1)
   
    #Movement
    if colors == False:
        pygame.draw.rect(screen, (255, 0, 0), player)
    elif colors == True:
        pygame.draw.rect(screen, (0, 255, 0), player)
        
    if left_wall == False:
        if key[pygame.K_a] == True:
            player.move_ip(-1, 0)
    if right_wall == False:
        if key[pygame.K_d] == True:
            player.move_ip(1, 0)
    if gravity == True:
        if key[pygame.K_s] == True:
            player.move_ip(0, 1)
    
    #Silly Little Colors
    if key[pygame.K_q] == True:
        colors = True
    elif key[pygame.K_e] == True:
        colors = False
    
    #Close Game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    if colors == False:
        pygame.draw.rect(screen, (255, 0, 0), player)
    elif colors == True:
        pygame.draw.rect(screen, (0, 255, 0), player)

    pygame.display.update()
    
pygame.quit()

你可以运行我的代码来识别代码的每个部分的作用,但为了开门见山,我正在制作一款游戏,你是一个带着喷气背包的小傻瓜,喷气背包有一个盾牌可以阻挡来袭的小行星/对象。我目前对如何创建指向鼠标的盾牌感到困惑。除了制作盾牌之外,我不需要任何帮助。谢谢!另外,如果您有任何提示,请告诉我!!

我尝试在互联网上查找任何教程,但找不到任何教程,我尝试自己创建它,但全部都是错误。我希望屏幕至少能显示出来,但我无法显示。

pygame 2d game-development
1个回答
0
投票

试试这个:

    # Draw the player
    if colors == False:
        pygame.draw.rect(screen, (255, 0, 0), player)
    elif colors == True:
        pygame.draw.rect(screen, (0, 255, 0), player)

    # Calculate the angle between the player and the mouse position
    mouse_x, mouse_y = pygame.mouse.get_pos()
    rel_x, rel_y = mouse_x - (player.x + player.width // 2), mouse_y - (player.y + player.height // 2)
    angle = math.atan2(rel_y, rel_x)  # Calculate angle in radians

    # Create a shield surface
    shield_surface = pygame.Surface((shield_radius * 2, shield_radius * 2), pygame.SRCALPHA)
    pygame.draw.circle(shield_surface, shield_color, (shield_radius, shield_radius), shield_radius, shield_thickness)

    # Rotate the shield to point towards the mouse
    rotated_shield = pygame.transform.rotate(shield_surface, -math.degrees(angle) - 90)

    # Calculate the position to blit the rotated shield
    shield_rect = rotated_shield.get_rect(center=(player.x + player.width // 2, player.y + player.height // 2))
    screen.blit(rotated_shield, shield_rect.topleft)

    # Close Game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

pygame.quit()
© www.soinside.com 2019 - 2024. All rights reserved.