我希望玩家图像面对鼠标后的十字准线,我使用了一个函数来旋转图像,在解决了许多问题(不正确的角度,没有正确使用函数等)之后,我得到它来旋转成像器但是当我在玩家图像上使用它不起作用,首先玩家图像甚至没有在屏幕上闪烁,其次游戏滞后令人难以置信,第三在移动诅咒一段时间后它返回这个错误,
所以这是我的脚本:
#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
如您所见,所有数字都是角度,我尝试将角度四舍五入,但没有效果。
任何帮助将不胜感激
谢谢你