我正在使用pygame制作图像编辑器,我想知道是否可以将鼠标光标更改为更适合画笔的东西(例如圆形或矩形).Pygame有一种非常奇怪的方式,我不确定它会非常好用.有没有办法可以写入位图然后使用它?
如果有一种方法可以通常使用Python,我认为这也会有用.
我一直试图在使用中围绕其中心旋转图像,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) 是否可以显示具有可控alpha的PyGame表面?我想采用具有自己的每像素alpha的表面,并以不同的半透明度显示它,而不影响表面数据并保持透明度完好无损,即表面上的物体将保持其形状但其"内容"变得或多或少半透明.
换句话说,我想将来自源图像的每像素alpha与运行时计算的每表面alpha组合.
我之前使用过pygame和python 2.7,但最近我'升级'到python 3.2.我下载并安装了Pygame的最新版本,据说可以使用这个版本的python.但是,我有一个相当令人沮丧的错误,应该是一个简单的代码块.代码是:
import pygame, random
title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
else:
print(event.type)
clock.tick(240)
pygame.quit()
Run Code Online (Sandbox Code Playgroud)
每次我运行它我得到:
17
1
4
Traceback (most recent call last):
File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?
我们有一个小型的网络应用程序,我们想要转换成原生的东西.现在,它有很多移动部件(后端,浏览器等),我们想把它转换成一个紧凑的应用程序.我们决定使用PyGame来做到这一点,除了字体渲染问题之外它一直很好.
具体的代码点是\ u0915\u094b\u091d\u093f\u0915\u094b和\ u0921
现在,这在我的编辑器和浏览器中看起来很好但是当我尝试在PyGame中渲染时,我得到了这个
.基本上,元音符号(\ u093fÇ)应该位于left的左侧,但它看起来在它的右侧(并且在क的左侧),从而完全弄乱了它.这不会发生在浏览器或文本编辑器(具有相同的输入字符串)中,因此我猜测它是PyGame中的渲染器问题.
有一个原始修复只适用于这种特殊情况,即将Ç(\ u093f)置于झ(\ u091d)之前.在这种情况下,它会像这样正确呈现
.这依赖于我对语言的了解并将该逻辑放入代码中.我必须在这里处理多种语言,所以这不可行.
我对unicode没有太多经验,所以我不知道如何解决这个问题.有什么我可以做的来解决这个问题吗?
如果它很重要,我正在使用Debian上的freesans字体,并且它具有必要的字形来呈现它.
更新: 实际呈现此代码的代码如下
# -*- coding: utf-8 -*-
import time
import pygame
# Pygame setup and create root window
pygame.font.init()
screen = pygame.display.set_mode((320, 200))
empty = pygame.Surface((320, 200))
font_file = pygame.font.match_font("freesans") # Select and
font = pygame.font.Font(font_file, 30) # open the font
writing = font.render(u"??????? ???????", True, (0, 0, 0)) # Render text on a surface
screen.fill((255, 255, 255)) # Clear the background
screen.blit(writing, …Run Code Online (Sandbox Code Playgroud) 我试图实现一个简单的Pygame脚本,该脚本应该:
请注意,上述所有事件必须按顺序发生,并且不能出现故障.
我遇到程序首先暂停的问题,然后显示文本仅在屏幕更新到其原始状态之前一瞬间(或根本不显示)出现.
该程序似乎跳过了第2步,并在显示文本之前继续执行步骤3中的暂停.我的代码如下:
import pygame
import sys
from pygame.locals import *
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
wS = pygame.display.set_mode((500, 500), 0, 32)
# Method works as intended by itself
def create_text(x, y, phrase, color):
"""
Create and displays text onto the globally-defined `wS` Surface
(params in docstring omitted)
"""
# Assume that the font file exists and is …Run Code Online (Sandbox Code Playgroud) 我在Pygame中写了一个简单的自上而下的RPG,我发现它很慢......虽然我不希望python或pygame匹配用C/C++或事件Byte Compiled等编译语言制作的游戏的FPS像Java一样,但目前的pygame的FPS仍然是15.我尝试渲染16色Bitmaps而不是PNG或24位图,这略微提高了速度,然后在绝望中,我将所有内容切换为黑白单色位图,使FPS达到35.但不是更多.现在根据我读过的大多数游戏开发书籍,为了让用户对游戏图形完全满意,2d游戏的FPS至少应该是40,那么有什么方法可以提高pygame的速度吗?
每次运行此程序时,都会收到此错误:
ValueError: list.remove(x): x not in list
Run Code Online (Sandbox Code Playgroud)
我试图降低一个外星人的生命值,只要它被一个螺栓击中.如果这个外星人的健康状况也应该被摧毁<= 0.同样,螺栓也会被摧毁.这是我的代码:
def manage_collide(bolts, aliens):
# Check if a bolt collides with any alien(s)
for b in bolts:
for a in aliens:
if b['rect'].colliderect(a['rect']):
for a in aliens:
a['health'] -= 1
bolts.remove(b)
if a['health'] == 0:
aliens.remove(a)
# Return bolts, aliens dictionaries
return bolts, aliens
Run Code Online (Sandbox Code Playgroud)
在ValueError上线情况aliens.remove(a).只是为了澄清,字典aliens和bolts列表都是.
我究竟做错了什么?
我花了相当多的时间找到一个64位的pygame安装用于python 3.3,(这里)现在我正在尝试创建一个窗口.但是,虽然窗口打开很好,但是当它按下x按钮时它不会关闭.事实上,我必须关闭IDLE来关闭窗口.我正在运行Win 7的64位版本.这是我的代码:
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Run Code Online (Sandbox Code Playgroud)
当我追加
time.sleep(5)
pygame.quit()
Run Code Online (Sandbox Code Playgroud)
它仍然没有关闭.我唯一的猜测是pygame.quit可能会进入其中一个循环,但即使已经解决了,我也非常希望能够在我想要的时候关闭窗口.
pygame ×10
python ×10
devanagari ×1
dictionary ×1
draw ×1
frame-rate ×1
list ×1
mouse ×1
mouse-cursor ×1
paint ×1
performance ×1
python-3.x ×1
rotation ×1
transparent ×1
unicode ×1