почему я пишу self.image.get_rect(center=(x,y))
а все равно он поворачивается относительно leftup ?!
возможно это из за скроллинга но я не понимаю и меня это расстраивает:(((
import pygame
import random
import math
pygame.init()
W = 600
H = 400
sc = pygame.display.set_mode((W,H))
class Tile(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.image = pygame.image.load("sprites/tiles/tile.png").convert_alpha()
self.rect = self.image.get_rect(center=(self.x, self.y))
def update(self, *args):
self.scrolling_x = args[0]
self.scrolling_y = args[1]
self.image = pygame.image.load("sprites/tiles/tile.png").convert_alpha()
self.image = pygame.transform.rotate(self.image, self.degress)
self.rect = self.image.get_rect(center=(self.x - self.scrolling_x, self.y - self.scrolling_y))
tile = Tile(300,200)
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.image = pygame.image.load("sprites/player/player.png").convert_alpha()
self.rect = self.image.get_rect(center=(x, y))
def update(self, *args):
self.image = pygame.image.load("sprites/player/player.png").convert_alpha()
self.image = pygame.transform.rotate(self.image, self.degress)
player = Player(300,200)
class Position:
x = 0
y = 0
scroll = Position()
class MousePosition:
x = 0
y = 0
mouse = MousePosition()
def player_scrolling():
if player.x - scroll.x != sc.get_width() / 2:
scroll.x += ((player.x - (scroll.x + sc.get_width() / 2)) / 12)
if player.y - scroll.y != sc.get_height() / 2:
scroll.y += ((player.y - (scroll.y + sc.get_height() / 2)) / 12)
def mouse_update():
position = pygame.mouse.get_pos()
mouse.x = position[0] + scroll.x
mouse.y = position[1] + scroll.y
def player_rotating():
rel_x = mouse.x - player.x
rel_y = mouse.y - player.y
degress = math.degrees(math.atan2(rel_y, rel_x)) * -1
player.degress = degress
rel_x = mouse.x - tile.x
rel_y = mouse.y - tile.y
degress = math.degrees(math.atan2(rel_y, rel_x)) * -1
tile.degress = degress
def player_vector():
global line_c, line_y, line_x
line_y = mouse.y - player.y
line_x = mouse.x - player.x
line_c = math.sqrt(line_x ** 2 + line_y ** 2)
cos = line_x / line_c
sin = line_y / line_c
global px, py
px = 100 * cos
py = 100 * sin
WHITE = (255, 255, 255)
GREY = (128, 128, 128)
BLACK = (0, 0, 0)
run = True
clock = pygame.time.Clock()
FPS = 10
speed = 5
pygame.display.update()
while run:
player_scrolling()
player_rotating()
mouse_update()
player_vector()
tile.x = player.x + px
tile.y = player.y + py
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
player.y -= speed
if keys[pygame.K_s]:
player.y += speed
if keys[pygame.K_a]:
player.x -= speed
if keys[pygame.K_d]:
player.x += speed
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
tile.x = mouse.x
tile.y = mouse.y
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEMOTION:
pass
sc.fill(GREY)
#это чтобы визуализировать центр игрока
#pygame.draw.line(sc, WHITE, (player.x - scroll.x, player.y - scroll.y), (tile.x - scroll.x, tile.y - scroll.y), 3)
sc.blit(player.image, (player.x - scroll.x, player.y - scroll.y))
sc.blit(tile.image,tile.rect)
tile.update(scroll.x, scroll.y)
player.update()
pygame.display.update()
clock.tick(FPS)
pygame.quit()