我一直试图在使用中围绕其中心旋转图像,pygame.transform.rotate()但它不起作用.特别是挂起的部分是rot_image = rot_image.subsurface(rot_rect).copy().我得到了例外:
ValueError: subsurface rectangle outside surface area
以下是用于旋转图像的代码:
def rot_center(image, angle):
"""rotate an image while keeping its center and size"""
orig_rect = image.get_rect()
rot_image = pygame.transform.rotate(image, angle)
rot_rect = orig_rect.copy()
rot_rect.center = rot_image.get_rect().center
rot_image = rot_image.subsurface(rot_rect).copy()
return rot_image
Run Code Online (Sandbox Code Playgroud) 我正在用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)
这是一张试图说清楚的图片:
谢谢.
我知道有几个主题,但我仍然无法弄清楚如何让我的船射击子弹..我想添加我的MOUSEBUTTONDOWN子弹从船上拍摄,因为声音效果播放.谢谢您的帮助!
import sys, pygame, pygame.mixer
from pygame.locals import *
pygame.init()
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
background = pygame.image.load("bg.png")
ship = pygame.image.load("ship.png")
ship = pygame.transform.scale(ship,(64,64))
shot = pygame.mixer.Sound("shot.wav")
soundin = pygame.mixer.Sound("sound.wav")
soundin.play()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
shot.play()
clock.tick(60)
mx,my = pygame.mouse.get_pos()
screen.blit(background,(0,0))
screen.blit(ship,(mx-32,500))
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)