我是pygame的新手,想要编写一些代码,只需每10秒将图像旋转90度.我的代码看起来像这样:
import pygame
import time
from pygame.locals import *
pygame.init()
display_surf = pygame.display.set_mode((1200, 1200))
image_surf = pygame.image.load("/home/tempuser/Pictures/desktop.png").convert()
imagerect = image_surf.get_rect()
display_surf.blit(image_surf,(640, 480))
pygame.display.flip()
start = time.time()
new = time.time()
while True:
end = time.time()
if end - start > 30:
break
elif end - new > 10:
print "rotating"
new = time.time()
pygame.transform.rotate(image_surf,90)
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,即图像不旋转,尽管每10秒钟在终端中打印"旋转".有人能告诉我我做错了什么吗?
我想围绕中心以外的点旋转矩形.到目前为止我的代码是:
import pygame
pygame.init()
w = 640
h = 480
degree = 45
screen = pygame.display.set_mode((w, h))
surf = pygame.Surface((25, 100))
surf.fill((255, 255, 255))
surf.set_colorkey((255, 0, 0))
bigger = pygame.Rect(0, 0, 25, 100)
pygame.draw.rect(surf, (100, 0, 0), bigger)
rotatedSurf = pygame.transform.rotate(surf, degree)
screen.blit(rotatedSurf, (400, 300))
running = True
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
Run Code Online (Sandbox Code Playgroud)
我可以改变度数以获得不同的旋转但旋转大约是中心.我想将矩形中心以外的点设置为旋转点.
在pygame中,我pygame.draw.rect(screen, color, rectangle)在程序中使用了所有矩形。我希望能够将这些矩形旋转到任意角度。我已经看到以下代码来旋转图像,但是我的问题是RECTANGLES。
pygame.transform.rotate(image, angle)
Run Code Online (Sandbox Code Playgroud)
但是我正在使用矩形,但是我没有可以旋转的图像或“表面”。当我尝试旋转一个矩形
rect = pygame.draw.rect(screen, self.color, self.get_rectang())
rotatedRect = pygame.transform.rotate(rect, self.rotation)
screen.blit(rotatedRect)
Run Code Online (Sandbox Code Playgroud)
这给出TypeError: must be pygame.Surface, not pygame.Rect了.rotate()
我的问题是,如何(x,y,w,h)在pygame中旋转a并显示RECTANGLE 而不是图像。
这是“潜在重复项”的链接文章不是重复项。一个答案解释了旋转矩形的后果,另一个答案使用代码旋转图像。
我想围绕一个支点,这是不是在中心旋转图像表面的pygame的。
枢轴是图像中的绿色十字:
我知道游戏窗口中枢轴的位置。我如何在此时跟踪图像并同时围绕该点旋转它。
image = pygame.image.load("boomerang64.png")
pos = (200, 200)
angle = 0
while True:
# [...]
rotate_rect, rotate_image = ???? # rotate around green cross by angle
surf.blit(rotated_image, rotate_rect)
angle += 1
# [...]
Run Code Online (Sandbox Code Playgroud) 到目前为止,我已经制作了一个矩形,它跟随用户的鼠标并用a键逆时针旋转,用键顺时针旋转d。
目前矩形跟随鼠标,但一旦用户开始旋转矩形,矩形就会变得非常滞后和变形。我需要帮助保持矩形不变和恒定的 FPS。感谢您的帮助!
这是代码:
import pygame as py
# define constants
WIDTH = 500
HEIGHT = 500
FPS = 200
# define colors
BLACK = (0 , 0 , 0)
GREEN = (0 , 255 , 0)
# initialize pygame and create screen
py.init()
screen = py.display.set_mode((WIDTH , HEIGHT))
# for setting FPS
clock = py.time.Clock()
rot = 0
rot_speed = 2
# define a surface (RECTANGLE)
image_orig = py.Surface((1 , 100))
# for making transparent …Run Code Online (Sandbox Code Playgroud) 我已经玩了一段时间了函数pygame.transform.rotate(), _get_rect(),_get_rect().center和_get_size()。
我有一个想要旋转的箭头图像,但我无法理解发生了什么。
我的问题是从哪一点开始(x,y)旋转?
在我的示例中,我有一个大小为 28x182 的图像,并使用screen.blit().
我正在考虑在 pygame 中制作一个 2d 射击游戏,我想让我的玩家(Player_1)指向鼠标方向。我花了几个小时寻找解决方案,并尝试了我能找到的所有解决方案,但没有一个有效,所以你能帮助我吗?这是我的代码:
import pygame, sys, os
from pygame.locals import *
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
#Exit settings
def quit():
pygame.quit()
sys.quit()
def events():
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
quit()
#IDS
CLOCK=pygame.time.Clock()
FPS=120
DS=pygame.display.set_mode((0,0), pygame.FULLSCREEN)
pygame.display.set_caption("Shooting simulator")
W,H=DS.get_size()
P_X=W/2-50
P_Y=H/2-50
#Colors
Red=pygame.Color("#FF0000")
Blue=pygame.Color("#0000FF")
Green=pygame.Color("#00FF00")
Black=pygame.Color("#000000")
White=pygame.Color("#FFFFFF")
#IGT(in game things)
Player_1=pygame.image.load("Img/Player_1.png").convert()
def game_loop():
while True:
events()
DS.fill(White)
DS.blit(Player_1,(P_X,P_Y))
pygame.display.flip()
game_loop()
Run Code Online (Sandbox Code Playgroud)
我将非常感谢所有的帮助。
我只是不明白为什么我的子弹不起作用。我做了一个子弹课,这里是:
class Bullet:
def __init__(self):
self.x = player.x
self.y = player.y
self.height = 7
self.width = 2
self.bullet = pygame.Surface((self.width, self.height))
self.bullet.fill((255, 255, 255))
Run Code Online (Sandbox Code Playgroud)
现在我在我的游戏类中添加了几个函数,这是新代码:
class Game:
def __init__(self):
self.bullets = []
def shoot_bullet(self):
if self.bullets:
for bullet in self.bullets:
rise = mouse.y - player.y
run = mouse.x - player.x
angle = math.atan2(rise, run)
bullet.x += math.cos(angle)
bullet.y += math.sin(angle)
pygame.transform.rotate(bullet.bullet, -math.degrees(angle))
D.blit(bullet.bullet, (bullet.x, bullet.y))
def generate_bullet(self):
if mouse.is_pressed():
self.bullets.append(Bullet())
Run Code Online (Sandbox Code Playgroud)
我期望代码做的是每次按下鼠标按钮时Bullet()都会添加game.bullets一个,然后game.shoot_bullet计算玩家和鼠标之间的角度并相应地朝鼠标的方向发射子弹。然而,结果是一团糟,子弹实际上不旋转也不移动。它们被生成并奇怪地移动到屏幕的左侧。我不确定我是否搞砸了什么,或者我使用的方法是完全错误的。
我加载了一张图像,我希望它绕其中心旋转,同时其比例越来越大。我最初知道如何围绕图像的中心旋转图像,但如果比例变大,我很难计算位置。我尝试过,但图像只是“跳舞”,而不是停留在中心。