第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在表面上繪制并使用 pygame 旋轉(zhuǎn)

在表面上繪制并使用 pygame 旋轉(zhuǎn)

慕無忌1623718 2021-07-28 06:52:13
我試圖在 Python 中使用命令 context.rotate(angle) 在 JS 中模擬一些可用的行為。我有以下代碼: import pygame import math import numpy as nppygame.init()CLOCK = pygame.time.Clock()RED = pygame.color.THECOLORS['red']WHITE = pygame.color.THECOLORS['white']screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)screen_width, screen_height = screen.get_size()surface = pygame.Surface((50, 50), pygame.SRCALPHA)surface.fill((0, 0, 0))rotated_surface = surfacerect = surface.get_rect()ax = int(screen_width / 2)ay = int(screen_height / 2)angle = 0print("Size of the screen ({}, {})".format(screen_width, screen_height))print("Center of the screen ({}, {})".format(ax, ay))myfont = pygame.font.SysFont("monospace", 12)pygame.display.set_caption("Test rotate")main_loop = Trueamplifier = 200def calculate_angle(mouse_position):    dx = mouse_position[0] - ax    dy = mouse_position[1] - ay    return np.arctan2(dy,dx)while main_loop:    for event in pygame.event.get():        keys = pygame.key.get_pressed()        if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:            main_loop = False    pos = pygame.mouse.get_pos()    angle = (calculate_angle(pos) * 180)/math.pi    screen.fill((255,255,255))    rotated_surface = pygame.transform.rotate(surface, -angle)    rect = rotated_surface.get_rect(center = (ax, ay))    screen.blit(rotated_surface, (rect.x, rect.y))    pygame.draw.line(rotated_surface, WHITE, (ax,ay), (ax+20, ay), 1)    pygame.draw.line(rotated_surface, WHITE, (ax+10,ay-10), (ax+20, ay), 1)    pygame.draw.line(rotated_surface, WHITE, (ax+10,ay+10), (ax+20, ay), 1)    pygame.display.update()    CLOCK.tick(30)pygame.quit()我正在繪制一個(gè)箭頭并希望根據(jù)鼠標(biāo)在屏幕上的位置旋轉(zhuǎn)它。我當(dāng)然可以在每次進(jìn)行一些正弦、余弦計(jì)算時(shí)重新繪制線條,但這很痛苦。我認(rèn)為表面可以在這里幫助我,事實(shí)上,它適用于完美旋轉(zhuǎn)的矩形。但是將我的線條畫到表面上是行不通的。所以,我想我誤解了表面的用途,或者我編碼錯(cuò)誤,有更好的方法來做到這一點(diǎn)。請(qǐng)注意,如果我在我的 draw.line 指令中用 screen 替換了 Rodated_surface,箭頭確實(shí)會(huì)在屏幕上繪制,但永遠(yuǎn)不會(huì)旋轉(zhuǎn)。有什么想法(除了使用圖像/精靈;))?
查看完整描述

1 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

問題是您在表面積外繪制箭頭線。每個(gè)表面的左上角坐標(biāo)為 (0, 0),因此它們都有自己的(本地)坐標(biāo)系。由于您使用屏幕的中心坐標(biāo)來繪制線條,因此不會(huì)在可見表面區(qū)域中繪制箭頭。


我會(huì)使用局部坐標(biāo)在 while 循環(huán)之前的原始表面上繪制線條,然后旋轉(zhuǎn)它并在循環(huán)內(nèi)獲得一個(gè)新的矩形。


import math

import pygame



pygame.init()


CLOCK = pygame.time.Clock()

GRAY = pygame.color.THECOLORS['gray50']

WHITE = pygame.color.THECOLORS['white']

screen = pygame.display.set_mode((800, 600))

screen_rect = screen.get_rect()  # A rect with the size of the screen.

surface = pygame.Surface((50, 50), pygame.SRCALPHA)

# surface.fill((0, 50, 150))  # Fill the surface to make it visible.

# Use the local coordinate system of the surface to draw the lines.

pygame.draw.line(surface, WHITE, (0, 0), (25, 0), 1)

pygame.draw.line(surface, WHITE, (0, 0), (25, 25), 1)

pygame.draw.line(surface, WHITE, (0, 0), (0, 25), 1)

# I rotate it so that the arrow is pointing to the right (0° is right).

surface = pygame.transform.rotate(surface, -135)

rect = surface.get_rect()

angle = 0

# If you're using Python 3.6+, you can use f-strings.

print(f"Size of the screen {screen.get_size()}")

print(f"Center of the screen {screen_rect.center}")


def calculate_angle(mouse_position):

    dx = mouse_position[0] - screen_rect.centerx

    dy = mouse_position[1] - screen_rect.centery

    return math.atan2(dy, dx)


main_loop = True

while main_loop:

    for event in pygame.event.get():

        # You shouldn't use `pygame.key.get_pressed` in the event loop.

        if (event.type == pygame.QUIT

            or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):

            main_loop = False


    pos = pygame.mouse.get_pos()

    angle = math.degrees(calculate_angle(pos))

    screen.fill(GRAY)

    # Now just rotate the original surface and get a new rect.

    rotated_surface = pygame.transform.rotate(surface, -angle)

    rect = rotated_surface.get_rect(center=(screen_rect.center))

    screen.blit(rotated_surface, rect)

    pygame.display.update()

    CLOCK.tick(30)



pygame.quit()

我還嘗試改進(jìn)了一些其他內(nèi)容(查看評(píng)論)。


查看完整回答
反對(duì) 回復(fù) 2021-08-03
  • 1 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)