【问题标题】:my picture should show in the direction of my mouse but it does not work [duplicate]我的图片应该显示在我的鼠标方向,但它不起作用[重复]
【发布时间】:2020-08-22 22:07:03
【问题描述】:

我的图片应该指向鼠标的方向,但它不起作用,因为我正在放大我的屏幕(因为我制作像素艺术)。 我认为坐标受屏幕放大率的影响

你能帮我解决这个问题吗?

这是我的代码:

import pygame
import sys
import random
import math

from pygame.locals import *
pygame.init()

clock = pygame.time.Clock()

screen_width = 1440
screen_height = 900
screen = pygame.display.set_mode((screen_width,screen_height))
display = pygame.Surface((int(1440/2),int(900/2)))

player = pygame.Rect(50,50,50,50)

bow_path = "data/bow_1.png"
bow = pygame.image.load(bow_path).convert()
bow.set_colorkey((255,255,255))

#---movement---#
right = False
left = False
down = False
up = False

movement = [0,0]
while True:
    display.fill((0,0,0))
    press_timer = 0
    mx,my = pygame.mouse.get_pos()

    movement = [0,0]
    if right == True:
        movement[0] += 2.5
        
    if left == True:
        movement[0] -= 2.5
       
    if up == True:
        movement[1] -= 2.5
        
    if down == True:
        movement[1] += 2.5

    player.x += int(movement[0])
    player.y += int(movement[1])

    bow_rect = bow.get_rect()     
    angle = math.degrees(math.atan2(player.centery - my, mx - player.centerx)) + 180
    rotated_bow = pygame.transform.rotate(bow,angle)
    
    pygame.draw.rect(display,(255,255,255),player)
    display.blit(rotated_bow,(int(player.x+25)-int(rotated_bow.get_width()/2),int(player.y+25)-int(rotated_bow.get_height()/2)))

在这里我改变我的屏幕尺寸:

    screen.blit(pygame.transform.scale(display,(screen_width,screen_height)),(0,0))
    pygame.display.update()
    clock.tick(60)

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    鼠标坐标总是相对于原始分辨率。当您将表面缩放到屏幕上时,您必须对鼠标坐标进行缩放以匹配表面。

    这是更新后的代码:

    bow_rect = bow.get_rect()
    # reverse scale mouse coordinates (1/2x)
    angle = math.degrees(math.atan2(player.centery - my/2, mx/2 - player.centerx)) + 180
    rotated_bow = pygame.transform.rotate(bow,angle)
    
    pygame.draw.rect(display,(255,255,255),player)
    display.blit(rotated_bow, (int(player.x+25)-int(rotated_bow.get_width()/2),
                 int(player.y+25)-int(rotated_bow.get_height()/2)))
    # scale surface to screen (2x)
    screen.blit(pygame.transform.scale(display,(screen_width,screen_height)),(0,0))
    pygame.display.update()
    clock.tick(60)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多