最近我决定开始我的第三场pygame游戏 . 在那场比赛中,玩家应该通过用鼠标指向飞机从屏幕底部的大炮向飞机射击 . 我将代码放入更多模块(more.py文件)为了更容易理解 . 我开始尝试让大炮向鼠标当前位置旋转 . 所以我们走了 .
main.py
import pygame,sys
import screen
import constants
from pygame.locals import *
from loop import *
screen.screen = pygame.display.set_mode((800,600))
main_loop()
constatns.py
import pygame
pygame.init()
scr = pygame.display.list_modes()
resolution = (scr[0][0],scr[0][1])
angle = 0
barell = pygame.image.load("barell.png")
tux = pygame.image.load("tux.png")
tux2 = pygame.transform.scale(tux,(100,100))
loop.py
import pygame,event
import screen
import constants
import math
import random
import groups
from faster_barell import *
faster_barell = faster_barell()
faster_barell.add_to_group()
def main_loop():
while True:
mx,my = pygame.mouse.get_pos()
event.Event()
screen.screen.fill([0,0,0])
groups.barell_group.draw(screen.screen)
for t in groups.barell_group:
dx = mx - t.rect.x
dy = my - t.rect.y
angle = math.atan2(dx,dy)
angle2 = math.degrees(angle)
constants.angle = angle2
t.image = pygame.transform.rotate(constants.barell,angle2)
pygame.display.flip()
screen.py
import pygame
import constants
pygame.init()
screen = None
event.py
import pygame,sys
from pygame.locals import *
class Event(object):
def __init__(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
faster_barell.py
import pygame
import constants
import groups
class faster_barell(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = constants.barell
self.rect = self.image.get_rect()
self.rect.x = 750
self.rect.y = 550
def add_to_group(self):
groups.barell_group.add(self)
groups.py
import pygame
barell_group = pygame.sprite.Group()
现在不是旋转pygame(我可以't really explain how)scales the barell image.The barell image is just a blank(white) 10x30 image).Now here comes even more strange part.When in t.image = pygame.transform.rotate(constants.barell,angle2) I change constants.barell to constants.tux2(which is just a tux image(just for testing it won'在游戏中)一切正常!这是我使用http://upload.wikimedia.org/wikipedia/commons/3/3e/Tux-G2.png的晚礼服图片 . 我试图通过将math.atan2中的dx和dy更改为其他内容来解决问题(dy,dx,-dy,dx,-dx,-dy等)请帮助我'm trying to solve this for about 6 hours(I never ask on stackoverflow unless I really can' t做任何事情让代码工作)
2 回答
以下是一些有用的链接:docs
来自rotate函数doc:
“除非以90度的增量旋转,否则 the image will be padded larger to hold the new size . ”
解决方案是在每次旋转后裁剪图像 . 您可以通过编写将创建新曲面的小函数进行裁剪,并将中间部分blit到新曲面上 . 这个问题在这里已经解决了:so
谢谢你的答案,我尝试了所有这些,但没有一个对我有用 . 相反,我这样修理它
pygame.transform.rotozoom(constants.barell,angle2,1)而不是pygame.transform.scale .
谢谢大家:D