我想在图像周围绘制红色边框,但不知道如何做。
我尝试填充Hero精灵的图像并将 colorkey 设置为 red self.image.set_colorkey(red),但这会使图像不可见。
在图像上绘制一个红色矩形,将其完全填充:pygame.draw.rect(self.image, red, [0, 0, width, height])。
我只想要一个红色边框,以帮助将来进行碰撞检测。
main.py中的代码:
import pygame
from pygame import *
import sprites
from sprites import *
pygame.init()
width = 640
height = 480
color = (255, 255, 255) #white
x = 0
y = 0
speed = 3
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Mushroom")
icon = pygame.image.load('icon.bmp')
pygame.display.set_icon(icon)
sprites_list = pygame.sprite.Group()
hero = Hero('mushroom.png', 48, 48)
hero.rect.x = 200;
hero.rect.y = 300;
sprites_list.add(hero)
running = …Run Code Online (Sandbox Code Playgroud) 我想围绕中心以外的点旋转矩形.到目前为止我的代码是:
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)
我可以改变度数以获得不同的旋转但旋转大约是中心.我想将矩形中心以外的点设置为旋转点.
几秒钟后(大约5秒),这段简单的代码崩溃(窗口没有响应).
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 24)
#clock = pygame.time.Clock()
#font = pygame.font.Font(None, 32)
cycles = 0
while True:
screen.fill(0)
# text = font.render('Cycles : %d' % cycles, True, (255, 255, 255))
# screen.blit(text, (100, 100))
cycles += 1
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
如果我取消注释注释行,我可以清楚地看到程序在显示47到50之间的值时失控.
我使用python 2.7和pygame 1.9.2,Windows 8(64位)和Eclipse + PyDev.
好的,基本上我正在开发这个 pygame 游戏,并且我正在使用混音器子模块来处理声音。但是,在 pygame 中,如果要停止声音,则声音必须完成其循环,直到可以停止为止。对于我所有的声音,这是可以的,因为它们可以是循环的 1 秒声音。但是,我使用了需要在一个循环中不断播放的下落声音,但是循环播放只有 1 秒长的下落声音听起来不是很有效。所以,最终我想知道是否有一种方法可以立即停止 7 秒的声音循环而不完成一个循环。
所以这个问题对我来说有点奇怪。我写了这段代码来看看pygame是否正常工作。
import pygame,sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400,300))
pygame.display.set_caption("Hello World")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
我pygame.py在输入时保存了这个文件:
-python pygame.py
Run Code Online (Sandbox Code Playgroud)
在cmd上它说:
ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package
Run Code Online (Sandbox Code Playgroud)
如果我-python在 shell 中输入然后输入import pygame它就像一个魅力。
所以总而言之:如果我想执行pygame.py,它看不到模块,但在输入python和后它会看到模块import pygame(工作没有错误)。
操作系统是Windows。
尝试使用 pip 安装 pygame 时出现以下错误:
Command "python setup.py egg_info" failed with error code 1 in
C:\Users\danie\AppData\Local\Temp\pip-install-8zxzuqef\pygame\
Run Code Online (Sandbox Code Playgroud)
我不知道这意味着什么,当我在其他地方查找时告诉我修复我的环境变量。但它仍然不起作用。
我正在 Godot 3.0 中制作 2D 平台游戏,我想让玩家使用鼠标瞄准来投掷/射击物品(类似于泰拉瑞亚中的弓和枪)。我该怎么做呢?我正在使用 gdscript。
我在网上看到使用HWSURFACE|DOUBLEBUF|RESIZABLE来调整窗口大小。
它的工作原理,但我不知道是什么的HWSURFACE和DOUBLEBUF实际做。
我知道它代表硬件表面和双缓冲区,但是它们对我的实际帮助我一无所知。
如何使用 Pygame 的sprite模块来检测具有透明背景的两个精灵的碰撞,以便True仅在实际精灵而不是透明背景发生碰撞时才返回?
我正在与一些带有pygame的射弹一起工作,并发现即使只有200行代码,游戏的运行速度也低于50 fps.(没有一个大的循环,除了运行循环,我的PC是相当新的)
那么,这是因为pygame使用SDL吗?
如果是这样,使用像OpenGL这样的GPU会提高性能吗?
Main.py
#Eemport
import pygame as pyg,sys,background, player, wall
from pygame.locals import *
#Screen
screen_size_width = 1280
screen_size_height = 720
screen_size = (screen_size_width,screen_size_height)
#Initialization
pyg.init()
screen = pyg.display.set_mode(screen_size)
pyg.display.set_caption("Collision and better physics")
#Set clock
Clock = pyg.time.Clock()
#Set Background
Background = background.background("beach.jpg",screen_size)
#Set Player
player_size_width = 128
player_size_height = 128
player_location_x = 640
player_location_y = 360
player_size = (player_size_width,player_size_height)
player_location = (player_location_x,player_location_y)
player_speed = 5
Player = player.player("crab.png",player_size,player_location)
#Set input
keys = {'right': False, 'left': False, 'up': …Run Code Online (Sandbox Code Playgroud) pygame ×9
python ×9
python-2.7 ×2
python-3.x ×2
sprite ×2
2d ×1
audio ×1
gdscript ×1
godot ×1
image ×1
installation ×1
mouse ×1
pip ×1
rotation ×1
surface ×1