小编مهن*_*ليل的帖子

如何知道两点之间的角度?

我正在用pygame制作小游戏,我制作了一支围绕其中心旋转的枪.我的问题是我希望枪自己向敌方方向旋转,但是我不能这样做因为我找不到枪和敌人之间的角度让枪旋转到它我已经搜索过了我发现我必须使用atan2但我没有找到任何正常工作的代码所以我希望有人可以帮助我.

这是我的代码:

import pygame
from pygame.locals import*
pygame.init()
height=650
width=650
screen=pygame.display.set_mode((height,width))
clock=pygame.time.Clock()
gun=pygame.image.load("m2.png").convert_alpha() 
gun=pygame.transform.smoothscale(gun,(200,200)).convert_alpha()
angle=0
angle_change=0
RED=(255,0,0)
x=525
y=155
while True :
    screen.fill((150,150,150))
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            quit()
        if event.type==KEYDOWN:
            if event.key==K_a:
                angle_change=+1
            if event.key==K_d:
                angle_change=-1
        elif event.type==KEYUP:
            angle_change=0
    angle+=angle_change
    if angle>360:
        angle=0
    if angle<0:
        angle=360
    pygame.draw.rect(screen,RED,(x,y,64,64))
    position = (height/2,width/2)
    gun_rotate=pygame.transform.rotate(gun,angle) 
    rotate_rect = gun_rotate.get_rect()
    rotate_rect.center = position
    screen.blit(gun_rotate, rotate_rect)
    pygame.display.update()
    clock.tick(60) 
Run Code Online (Sandbox Code Playgroud)

这是一张试图说清楚的图片:

在此输入图像描述

谢谢.

python math pygame angle

10
推荐指数
3
解决办法
3万
查看次数

如何使用 pygame.surface.scroll()?

pygame.surface.scroll()我刚刚从 pygame 文档中发现和理解的内容scroll()是用于移动表面,而不需要再次重建背景以覆盖旧表面,就像pygame.rect.move_ip()表面一样。

无论如何,我不知道如何使用它,而且 pygame 文档中的示例对我来说很难理解,只要我是初学者,经过很长时间的搜索,我找不到任何有用的东西来理解如何使用它。

这是我的代码。

import pygame
from pygame.locals import*

screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
while True :
    screen.fill((0,0,0))
    for event in pygame.event.get():
        if event.type==pygame.QUIT :
            pygame.quit()
            quit()
    image.scroll(10,10)
    screen.blit(image,(boxx,boxy))
    pygame.display.update()
    clock.tick(60)
Run Code Online (Sandbox Code Playgroud)

python pygame

5
推荐指数
1
解决办法
7976
查看次数

标签 统计

pygame ×2

python ×2

angle ×1

math ×1