簡體   English   中英

如何在pygame中從玩家畫線

[英]how to draw line in pygame from player

嘿,我正在進行自上而下的近戰戰斗(劍盒魔法物品龍......),我現在不知道如何從玩家和鼠標角度畫劍(也許更容易)

import pygame
import sys

max_tps=20.0

pygame.init()
color=(255,255,255)
screen=pygame.display.set_mode((1550,800))
box=pygame.Rect(100,100,50,50)
boxai=pygame.Rect(0,0,50,50)
clock=pygame.time.Clock()
delta=0.0

while True:
    #wyjdź
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit(0)
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            sys.exit(0)

    #delta
    delta += clock.tick()/1000.0
    while delta > 1/max_tps:
        delta-=1/max_tps

        #input
        if pygame.key.get_pressed() [pygame.K_a]:
            box.x -=10
        if pygame.key.get_pressed() [pygame.K_d]:
            box.x +=10
        if pygame.key.get_pressed() [pygame.K_w]:
            box.y -=10
        if pygame.key.get_pressed() [pygame.K_s]:
            box.y +=10
        if event.type == pygame.MOUSEBUTTONDOWN:
            pygame.draw.line(screen,(255,0,0),box,box+10,10)
        #gracz
        screen.fill(color)
        pygame.draw.rect(screen, (0,0,0),box)
            # ai
        pygame.draw.rect(screen, (255, 0, 0), boxai)
        if boxai.x < box.x:
            boxai.x += 5
        if boxai.x > box.x:
            boxai.x -= 5
        if boxai.y < box.y:
            boxai.y += 5
        if boxai.y > box.y:
            boxai.y -= 5
        if boxai.x and boxai.y == box.x and box.y:
            print("a")
        pygame.display.flip()

為什么不用精靈? 您甚至可以創建自己的精靈。 創建一個pygame.Surface並使用pygame.draw模塊在其上繪圖。 例如pygame.draw.polygon

import pygame

pygame.init()
window = pygame.display.set_mode((250, 250))
clock = pygame.time.Clock()

sword = pygame.Surface((60, 100), pygame.SRCALPHA)
points = [(30, 0), (40, 10), (40, 70), (60, 75), (40, 80), (35, 80), (35, 100), 
          (25, 100), (25, 80), (20, 80), (0, 75), (20, 70), (20, 10)]
pygame.draw.polygon(sword, (255, 255, 0), points)
angle = 0

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (128, 128, 128), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles] 

def blitRotate(surf, image, origin, pivot, angle):
    image_rect = image.get_rect(topleft = (origin[0] - pivot[0], origin[1]-pivot[1]))
    offset_center_to_pivot = pygame.math.Vector2(origin) - image_rect.center
    rotated_offset = offset_center_to_pivot.rotate(-angle)
    rotated_image_center = (origin[0] - rotated_offset.x, origin[1] - rotated_offset.y)
    rotated_image = pygame.transform.rotate(image, angle)
    rotated_image_rect = rotated_image.get_rect(center = rotated_image_center)
    surf.blit(rotated_image, rotated_image_rect)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.MOUSEBUTTONDOWN:
            target = event.pos
        
    window.blit(background, (0, 0))
    blitRotate(window, sword, window.get_rect().center, (30, 75), angle)
    pygame.display.flip()
    clock.tick(100)
    angle += 1

pygame.quit()
exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM