我希望玩家的图像能够面对跟随鼠标的十字准线,我使用了一个功能来旋转图像并解决了许多问题(不正确的角度,没有正确使用功能等等)我得到了旋转成像器但是当我在玩家图像上使用它时它不起作用,首先玩家的图像甚至不会对屏幕造成影响,其次游戏滞后不可思议,第三,将诅咒移动一段时间后它会返回错误,
所以这是我的剧本:
#set up
import pygame, sys, random, time, math
from pygame.locals import *
from math import atan2, degrees, pi
pygame.init()
#variables end----------------------------------------------------------------
#imagers
grass = "grass_shit.png" #grass image
player_img = "shithead.png" #player name
ali_img = "shit_head2.png" #alien image
dead_screen = "dead_shit.png"
cross_hair = "crosshair.png"
#screen
screen = pygame.display.set_mode((850, 640),0,32) #set screen
background = pygame.image.load(grass).convert() #load image to screen
#character variables
char = pygame.image.load(player_img).convert_alpha() #covert player image
player_x, player_y = 0, 0 #character position
move_player_x, move_player_y = 0, 0 #how far the character will move, #x is left and right, y is up and down
#alien variables
ali = pygame.image.load(ali_img).convert_alpha() #covert alien image
ali_x, ali_y = random.randint(10, 480), random.randint(10, 500)
move_ali_x, move_ali_y = 0, 0
#mouse things
crosshair = pygame.image.load(cross_hair).convert_alpha()
#other
blit_everything = True
dead = False
#variables end----------------------------------------------------------------
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_a:
move_player_x=-3
elif event.key==K_d:
move_player_x=+3
elif event.key==K_w:
move_player_y=-3
elif event.key==K_s:
move_player_y=+3
if event.type==KEYUP:
if event.key==K_a:
move_player_x=0
elif event.key==K_d:
move_player_x=0
elif event.key==K_w:
move_player_y=0
elif event.key==K_s:
move_player_y=0
if event.type==KEYDOWN:
if event.key== K_LEFT:
move_player_x=-3
elif event.key== K_RIGHT:
move_player_x=+3
elif event.key==K_UP:
move_player_y=-3
elif event.key== K_DOWN:
move_player_y=+3
if event.type==KEYUP:
if event.key==K_LEFT:
move_player_x=0
elif event.key==K_RIGHT:
move_player_x=0
elif event.key==K_UP:
move_player_y=0
elif event.key==K_DOWN:
move_player_y=0
if ali_x < player_x:
move_ali_x =+ 0.5
elif ali_x > player_x:
move_ali_x =- 0.5
if ali_y < player_y:
move_ali_y =+ 0.5
elif ali_y > player_y:
move_ali_y =- 0.5
ali_y += move_ali_y
ali_x += move_ali_x
ali_rect = pygame.Rect(ali_x, ali_y, 35, 35)
player_x += move_player_x
player_y += move_player_y
char_rect = pygame.Rect(player_x, player_y, 35, 35)
if char_rect.colliderect(ali_rect):
background = pygame.image.load(dead_screen).convert()
blit_everything = False
dead = True
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_x -= crosshair.get_width() / 2
mouse_y -= crosshair.get_height() / 2
dx = player_x - mouse_x
dy = player_y - mouse_y
player_atan = atan2(-dy, dx)
player_atan %= 2 * pi
player_angle = degrees(player_atan)
char = pygame.transform.rotate(char, player_angle)
print player_angle
screen.blit(background,(0,0))
#blit everything
if blit_everything == True:
screen.blit(char,(player_x,player_y))
screen.blit(ali,(ali_x, ali_y))
screen.blit(crosshair,(mouse_x, mouse_y))
pygame.display.update()
这是错误:
Traceback (most recent call last):
File "/home/claude/Dropbox/BigKahunaBurger/BigKahunaBurger LOOP.py", line 160, in <module>
char = pygame.transform.rotate(char, player_angle)
error: Width or height is too large
>>>
在第161行,我让程序打印角度以确保它们是角度,这就是我得到的:
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
162.829790545
159.757500243
149.225963899
126.609372987
152.009531506
114.018423659
因为你可以看到所有数字都是角度,我试着将角度四舍五入但是没有效果。
任何帮助将不胜感激
谢谢
答案 0 :(得分:0)
为什么不使用不同的相机进行HUD的事情?
btw尝试追踪你的错误。 - 我们没有看到这里的行号,所以我们可以以某种方式区分该区域。 - 我的猜测是你的一个或多个变量在每帧之后越来越大 - 直到太大而无法转换为整数值(例如像素位置) - 为您的代码添加计数器,并在每个帧或键击后增加它 - 如果该计数器大于某个阈值,则添加断点 - 在一些帧/键盘/鼠标移动后,观察变量的明显错误值...
PS。错误也可能是盒子之外的东西 - 内存泄漏或未初始化的变量或其他东西
PPS。你的角度可以是正确的,但它们会创建一个带有更多参数的变换矩阵,问题可能就在那里
答案 1 :(得分:0)
当你转动时:
char = pygame.transform.rotate(char, player_angle)
如果旋转图像,则下一个旋转的图像将再次稍大。如果你继续这样做,最终图像将变得太大而Pygame无法处理,并且你的程序将崩溃并显示错误消息pygame.error:宽度或高度太大。
唯一的例外是如果您将图像旋转90度的倍数,例如0度,90度,180度,270度或360度。在这种情况下,像素可以旋转而没有任何失真。
答案 2 :(得分:0)
为避免失真,您可以使用每次旋转原稿而不是通过旋转旋转已扭曲的图像。这就像在复印机中不断复制副本一样:最终你的副本太扭曲了。 通过使用它,您不必使角度倍数为90。