2017-09-10 7 views
1

タイトルには、W、S、A、Dで制御できる画像があります。しかし、私は、移動可能なプレーヤーの画像をマウスの位置に向けるようにしたい。ここでreleventコードです:私は本当にWTFイムがやって知っているが、ここでの事は、これまで YouTubeのビデオをどのように動作するかだいないので移動可能な画像をマウスに従うように回転させる方法

import pygame 
import random 
import time 
import math 
import sys 

pygame.init() 

#The size of the game window 
display_width = 1280 
display_height = 800 

#Colors available 
black = (0, 0, 0) #colours defined by RGB, 
white = (255, 255, 255) 
red = (200, 0, 0) 
green = (0, 150, 0) 
bright_red = (255, 0, 0) 
bright_green =(0, 255, 0) 

#This code opens up the Game window 
gameDisplay = pygame.display.set_mode((display_width, display_height)) 
pygame.display.set_caption("Blockslayer") 
clock = pygame.time.Clock() 
pygame.mouse.set_visible(True) 

#player character info 
slayerImg = pygame.image.load('squareslayer.png').convert_alpha() 
slayerWidth = 84 
slayerHeight = 84 


#mouse info 
mouse_c = pygame.image.load("crosshair.png ").convert_alpha() 

def crosshair(mousex,mousey): 
    mousex, mousey = pygame.mouse.get_pos() 
    small_ch = pygame.transform.scale(mouse_c, (20, 20)) 
    gameDisplay.blit(small_ch, (mousex, mousey,)) 

    print(mousex,mousey) 

#player character 
def slayer(x,y,): 
    #small_slayer = pygame.transform.scale(slayerImg, (120, 80,)) 
    pos = pygame.mouse.get_pos() 
    angle = 360 - math.atan2(pos[1] - 84, pos[0] - 84) * 180/math.pi 
    rotimage = pygame.transform.rotate((slayerImg), angle,) 
    rect = rotimage.get_rect(center=(x, y)) 
    gameDisplay.blit(rotimage, rect,) 
    pygame.display.update() 

#Game Logic 
def block_game_loop(): 
    x = (display_width * 0.45) 
    y = (display_height * 0.8) 
    pos = pygame.mouse.get_pos() 
    angle = 360 - math.atan2(pos[1] + x - 84, pos[0] + y - 84) * 180/math.pi 
    rotimage = pygame.transform.rotate((slayerImg), angle,) 

    mousex, mousey = pygame.mouse.get_pos() 


    #def blockguy(blockguyX, blockguyY, blockguyW, blockguyH,): 
    #blockguyX = random.randrange(0, 785) 
    #blockguyY = random.randrange (0, 600) 
    #blockguyW = 166 
    #blockguyH = 110 
    #blockguy_speed = 5 


    #Event handler 
    exit_game = False 

    while not exit_game: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

     pressed = pygame.key.get_pressed() 

     if pressed[pygame.K_s]: y += 7 
     if pressed[pygame.K_w]: y -= 7 
     if pressed[pygame.K_a]: x -= 7 
     if pressed[pygame.K_d]: x += 7 





     gameDisplay.fill(white) 
     slayer(x, y,) 

     #Boundaries 
     if x > display_width: 
      x = 1275 

     if x < 0: 
      x = 5 

     if y > display_height: 
      y = 795 

     if y < 0: 
      y = 5 

     crosshair(mousex,mousey) 
     #blockguy(blockguyX, blockguyY, blockguyW, blockguyH,) 
     pygame.display.update() 
     clock.tick(60) 


block_game_loop() 
pygame.quit() 
quit() 

コードはかなり一緒にjankedさ:https://www.youtube.com/watch?v=zShWAm4pSx8&feature=youtu.be

+3

質問がありますか? [mcve]の可能性はありますか? [ask]をお読みください。 – wwii

答えて

1

これを見てみましょうrotate機能(コメントを読む)。

import math 
import pygame 


pygame.init() 

gray = (30, 30, 30) 

display_width, display_height = (1280, 800) 
gameDisplay = pygame.display.set_mode((display_width, display_height)) 

clock = pygame.time.Clock() 

slayerImg = pygame.Surface((104, 84), pygame.SRCALPHA) 
pygame.draw.polygon(slayerImg, (0, 120, 250), [(1, 1), (103, 42), (1, 83)]) 


def rotate(x, y, mouse_pos, image): 
    # Calculate x and y distances to the mouse pos. 
    run, rise = (mouse_pos[0]-x, mouse_pos[1]-y) 
    # Pass the rise and run to atan2 (in this order) 
    # and convert the angle to degrees. 
    angle = math.degrees(math.atan2(rise, run)) 
    # Rotate the image (use the negative angle). 
    rotimage = pygame.transform.rotate(image, -angle) 
    rect = rotimage.get_rect(center=(x, y)) 
    return rotimage, rect 


def block_game_loop(): 
    x = display_width * 0.45 
    y = display_height * 0.8 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       return 

     pressed = pygame.key.get_pressed() 
     if pressed[pygame.K_s]: y += 7 
     if pressed[pygame.K_w]: y -= 7 
     if pressed[pygame.K_a]: x -= 7 
     if pressed[pygame.K_d]: x += 7 

     mousex, mousey = pygame.mouse.get_pos() 
     # Boundaries 
     if x > display_width: 
      x = 1275 
     if x < 0: 
      x = 5 
     if y > display_height: 
      y = 795 
     if y < 0: 
      y = 5 

     gameDisplay.fill(gray) 
     rotimage, rect = rotate(x, y, (mousex, mousey), slayerImg) 
     gameDisplay.blit(rotimage, rect) 

     pygame.display.update() 
     clock.tick(60) 


block_game_loop() 
pygame.quit() 

補遺:ここでは、回転弾丸を撃つことができる方法を示します例です。 math.cosmath.sinでxとyの速度を計算する必要があるので、基本的な三角法の知識が必要です。それはあなたが望むスピードにスケールする必要がある単位ベクトル(長さ1)を与えます。今度はvelocityリストをrect、余分な位置リスト、回転したイメージとともに箇条書きオブジェクトを表すリストに入れなければなりません。 rect位置を更新するには、最初にposをベロシティに追加してからposをrect中心に割り当てなければなりません(rectはxとyの位置としてintを持つことができるので、そうする必要があります)。

リンク解答のようにリストの代わりにpygame.math.Vector2とpygameスプライトとスプライトグループを使用することをお勧めします。なぜなら、それは読んだ方がはるかにいいからです。箇条書きを削除するコードを追加する必要がありますが、スプライトとスプライトグループで実装する方が簡単です。

import math 
import pygame as pg 
from pygame.math import Vector2 


pg.init() 
screen = pg.display.set_mode((640, 480)) 
FONT = pg.font.Font(None, 24) 
BLACK = pg.Color('black') 
BULLET_IMAGE = pg.Surface((20, 11), pg.SRCALPHA) 
pg.draw.polygon(BULLET_IMAGE, pg.Color('grey11'), [(0, 0), (20, 5), (0, 11)]) 


def update_bullets(bullets): 
    """Add the velocity to the pos then assign pos to the rect center.""" 
    for bullet_rect, pos, velocity, _ in bullets: 
     pos[0] += velocity[0] 
     pos[1] += velocity[1] 
     bullet_rect.center = pos 


def draw_bullets(bullets, screen): 
    for bullet_rect, pos, _, image in bullets: 
     screen.blit(image, bullet_rect) 
     pg.draw.rect(screen, (200, 140, 0), bullet_rect, 1) 


def main(): 
    clock = pg.time.Clock() 
    # The cannon image and rect. 
    cannon_img = pg.Surface((60, 22), pg.SRCALPHA) 
    pg.draw.rect(cannon_img, pg.Color('grey19'), [0, 0, 35, 22]) 
    pg.draw.rect(cannon_img, pg.Color('grey19'), [35, 6, 35, 10]) 
    orig_cannon_img = cannon_img # Store orig image to preserve quality. 
    cannon = cannon_img.get_rect(center=(320, 240)) 
    angle = 0 # Angle of the cannon. 

    # Add bullets to this list. Bullets will also be lists 
    # consisting of a pygame.Rect, the velocity and the image. 
    bullets = [] 
    bullet_speed = 5 

    playing = True 
    while playing: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       playing = False 
      elif event.type == pg.MOUSEBUTTONDOWN: 
       # Left button fires a bullet from cannon center with 
       # current angle. Add the bullet to the bullets list. 
       if event.button == 1: 
        # Use cosine and sine to calculate the x and y 
        # velocity. Scale them by the desired speed. 
        velocity = (math.cos(math.radians(angle)) * bullet_speed, 
           math.sin(math.radians(angle)) * bullet_speed) 
        img = pg.transform.rotate(BULLET_IMAGE, -angle) 
        bullet_rect = img.get_rect(center=cannon.center) 
        # The extra pos list is needed because the pygame.Rect 
        # can only have ints as the x and y value. We still 
        # need the rect for collision detection. 
        pos = list(bullet_rect.center) 
        bullet = [bullet_rect, pos, velocity, img] 
        bullets.append(bullet) 

     update_bullets(bullets) 
     # Find angle to target (mouse pos). 
     x, y = Vector2(pg.mouse.get_pos()) - cannon.center 
     angle = math.degrees(math.atan2(y, x)) 
     # Rotate the cannon image. 
     cannon_img = pg.transform.rotate(orig_cannon_img, -angle) 
     cannon = cannon_img.get_rect(center=cannon.center) 

     # Draw 
     screen.fill(pg.Color('darkseagreen4')) 
     draw_bullets(bullets, screen) 
     screen.blit(cannon_img, cannon) 
     txt = FONT.render('angle {:.1f}'.format(angle), True, BLACK) 
     screen.blit(txt, (10, 10)) 
     pg.draw.line(
      screen, pg.Color(150, 60, 20), 
      cannon.center, pg.mouse.get_pos(), 2) 
     pg.display.update() 

     clock.tick(30) 


if __name__ == '__main__': 
    main() 
    pg.quit() 
+0

あなたは絶対的な伝説です。ありがとう、トン! –

+0

どのように私はキャラクターからマウスの位置に弾丸を発射しますか、私はいくつか試しました。 –

+0

ベクターの仕組みを知っていますか?次にこの[回答](https://stackoverflow.com/a/43953216/6220679)を見てください。 – skrx

関連する問題