标签: pygame-surface

如何从一个numpy数组的float32创建一个pygame表面?

我有一段可以使用的代码

my_surface = pygame.image.load('some_image.png')
Run Code Online (Sandbox Code Playgroud)

这将返回一个pygame表面.我想在其他地方使用相同的代码,而是传入一个numpy数组.(实际上,我将有一个if语句来确定我们是否有一个数组或一个图像的路径.在任何一种情况下,该函数必须返回相同类型的对象,一个pygame表面.它已经使用上面的工作了如果脚本的使用方式不同,我现在必须添加第二种生成相同对象的方法.)我尝试过使用

my_surface = pygame.pixelcopy.make_surface(my_array)
Run Code Online (Sandbox Code Playgroud)

但问题是这个函数需要一个INTEGER数组.我的数组是float32.我可以通过传递数组来强制它通过

(my_array*10000).astype(int)
Run Code Online (Sandbox Code Playgroud)

但是当我稍后显示它看起来像垃圾(想象我的惊喜).所以我的问题是,我们如何在一系列花车中优雅地创建一个pygame表面?

python pygame pygame-surface

8
推荐指数
1
解决办法
5750
查看次数

如何将numpy数组呈现到pygame表面?

我正在编写一段代码,其中一部分是读取图像源并将其显示在屏幕上供用户进行交互.我还需要锐化的图像数据.我使用以下内容来读取数据并将其显示出来pyGame

def image_and_sharpen_array(file_name):
    #read the image data and return it, with the sharpened image
    image = misc.imread(file_name)

    blurred = ndimage.gaussian_filter(image,3)
    edge = ndimage.gaussian_filter(blurred,1)
    alpha = 20
    out = blurred + alpha*(blurred - edge)
    return image,out

#get image data
scan,sharpen = image_and_sharpen_array('foo.jpg')
w,h,c = scan.shape


#setting up pygame
pygame.init()
screen = pygame.display.set_mode((w,h))

pygame.surfarray.blit_array(screen,scan)
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)

并且图像仅在旋转和反转的屏幕上显示.这是由于之间的差异misc.imreadpyGame?或者这是由于我的代码中出了什么问题?

还有其他办法吗?我读到的大部分解决方案都涉及保存数字然后用"pyGame"读取它.

python pygame numpy pygame-surface

7
推荐指数
2
解决办法
3944
查看次数

Pygame:用颜色填充文本的透明区域

我有几种字体,我想使用它们基本上是字母的轮廓,但内部是透明的。我将如何仅用颜色填充这些字体的内部区域?我怀疑它会使用特殊的 blitting RGBA_BLEND 模式,但我不熟悉它们的功能。

这是我正在使用的字体示例:https : //www.dafont.com/fipps.font?back=bitmap

现在,我只是将字体渲染到表面上,为此我编写了一个辅助函数。理想情况下,我能够将其集成到我的功能中。

def renderText(surface, text, font, color, position):
    x, y = position[0], position[1]
    width, height = font.size(text)
    position = x-width//2, y-height//2
    render = font.render(text, 1, color)
    surface.blit(render, position)
Run Code Online (Sandbox Code Playgroud)

非常感谢您能给我的任何帮助!

pygame python-3.x pygame-surface

7
推荐指数
1
解决办法
927
查看次数

使用区域参数时pygame surface.blits

我试图使用带有area参数的surface.blits来提高我的代码的性能.当我使用blits的area参数时,我遇到以下错误:

SystemError:'pygame.Surface'对象>的''方法'blits'返回带有错误集的结果.

如果我删除区域参数,blits就像我期望的那样工作.关于我可能做错的任何想法?下面是我的用例和错误的示例代码.

import sys
import random

import pygame
pygame.init()

tilemap = pygame.image.load('pattern.jpg')

tilesize = 64
size = 4
w, h = size*64, size*64
screen = pygame.display.set_mode((w, h))

while True:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    blit_list = []
    for i in range(size):
        for j in range(size):
            xi, yi = random.randint(0, size), random.randint(0, size)
            blit_args = (tilemap, (i*tilesize, j*tilesize),
                        (xi*tilesize, yi*tilesize, tilesize, tilesize))

            # calling screen.blit here works correctly
            screen.blit(*blit_args)

            blit_list.append(blit_args)


    # instead of …
Run Code Online (Sandbox Code Playgroud)

python pygame pygame-surface

6
推荐指数
1
解决办法
308
查看次数

pygame.mouse.get_pos 和 Rect.collidepoint 的 Python 表面真实位置坐标

在我的 python prog 中,我有 2 个表面:

  • ScreenSurface : 屏幕
  • FootSurface: 另一个表面闪烁ScreenSurface

我在 上放了一些矩形FootSurface,问题是Rect.collidepoint()给了我链接到 的相对坐标FootSurfacepygame.mouse.get_pos()给出了绝对坐标。

例如 :

pygame.mouse.get_pos() --> (177, 500) 与主表面命名相关 ScreenSurface

Rect.collidepoint()--> 与第二个表面有关,FootSurface其中 rect 被 blitted

那么就行不通了。有没有一种优雅的python方式来做这件事:将鼠标的相对位置放在我的FootSurface或绝对位置上Rect;或者必须将我的代码更改分裂RectScreenSurface

python mouse pygame pygame-surface

5
推荐指数
1
解决办法
3129
查看次数

为什么我的碰撞测试总是返回“true”并且为什么图像矩形的位置总是错误的(0, 0)?

我的 collide_rect 函数无法正常工作。当它不应该返回时,它总是返回 True。我尝试在互联网上查找,但没有任何效果对我有用。我认为碰撞矩形不知何故没有使用两个精灵的实际坐标。有人能帮忙吗?

import pygame
import pygame.sprite
import sys


gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("test_collision")
clock = pygame.time.Clock()
crashed = False


class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect()
        self.x = 280
        self.y = 475
        self.col = False
    def update(self):
        gameDisplay.blit(self.image, (self.x,self.y))
        self.rect = self.image.get_rect()
    def test_collisions(self,sprite):
        self.col = pygame.sprite.collide_rect(self,sprite)
class Obstacle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 1000
        self.y = 483
        self.image = pygame.image.load("obstacle.png")
        self.time = pygame.time.get_ticks()
        self.rect = self.image.get_rect()
    def change_x(self):
        self.time = pygame.time.get_ticks()
        self.x = -(self.time/5) …
Run Code Online (Sandbox Code Playgroud)

python pygame collision-detection pygame-surface

5
推荐指数
1
解决办法
1807
查看次数

如何修复此 DeprecationWarning

弃用警告:需要一个整数(得到类型浮点数)。int不推荐使用隐式转换为整数,并且可能会在 Python 的未来版本中删除。

win.blit(playerStand, (x, y))

弃用警告:需要一个整数(得到类型浮点数)。int不推荐使用隐式转换为整数,并且可能会在 Python 的未来版本中删除。

win.blit(walkLeft[animCount // 5], (x, y))

python pygame python-3.x pygame-surface pygame2

5
推荐指数
1
解决办法
1万
查看次数

Pygame 水物理无法按预期工作

所以,我一直在尝试根据本教程在 pygame 中实现水物理

https://gamedevelopment.tutsplus.com/tutorials/make-a-splash-with-dynamic-2d-water-effects--gamedev-236

问题是,当我在 pygame 中实现这段代码时,水决定它不会产生很好的涟漪效应,而是会发疯并在整个屏幕上摆动,直到最终崩溃。

我环顾了一些不和谐的服务器,发现其他人试图实现同样的事情,并遇到了同样的问题。他们的代码基本上和我的一样,只是组织得更整齐,所以我会发布它而不是我的。

import pygame, random
import math as m

from pygame import *

pygame.init()

WINDOW_SIZE = (854, 480)
 
screen = pygame.display.set_mode(WINDOW_SIZE,0,32) # initiate the window

clock = pygame.time.Clock()

font = pygame.font.SysFont("Arial", 18)

class surface_water_particle():
    def __init__(self, x,y):
        self.x_pos = x
        self.y_pos = y
        self.target_y = y
        self.velocity = 0
        self.k = 0.02
        self.d = 0.02
        self.time = 1

    def update(self):
        x = -(self.target_y - self.y_pos)
        a = -(self.k * x - self.d * self.velocity) …
Run Code Online (Sandbox Code Playgroud)

pygame physics python-3.x pygame-surface

5
推荐指数
1
解决办法
282
查看次数

在 Python Pygame 上显示 SVG(来自字符串)

我有一个以字符串表示的 SVG 图形

svg_string='<svg height="100" width="500"><ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" /><ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" /></svg>'
Run Code Online (Sandbox Code Playgroud)

我想在Python启动的窗口上显示由 表示的图形svg_string,它是2个椭圆。

其伪代码应该是这样的

import pygame


def display_svg_figure(screen, svg_string):
    #   Code that draws the rendered SVG string on
    #   the screen
    pass

background_colour = (255, 255, 255)
(width, height) = (900, 900)

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Display SVG')
screen.fill(background_colour)

pygame.display.flip()

svg_string='<svg height="100" width="500"><ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" /><ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" /></svg>'
display_svg_figure(screen, svg_string)


running = True
while …
Run Code Online (Sandbox Code Playgroud)

python svg pygame python-3.x pygame-surface

5
推荐指数
1
解决办法
3702
查看次数

如何在曲面上绘制圆弧

我想在曲面内画一个半圆。所以我想使用, pygame.draw.arc() 但我不知道如何在表面内绘制圆弧,我已经看到了官方文档,并且正在使用math.radians()将度数转换为弧度,但我看不到圆弧。正在画一个圆...

import pygame
import math

pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

surf = pygame.Surface((100, 100))
surf.fill("green")
rect = surf.get_rect(center=(100, 100))

run = 1
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = 0

    screen.fill((255, 202, 135))
    pygame.draw.circle(surf, (255, 255, 255), (30,30),25)
    pygame.draw.arc(surf, (255, 255, 255), rect, math.radians(270), math.radians(90))
    screen.blit(surf, rect)

    pygame.display.flip()
pygame.quit()

Run Code Online (Sandbox Code Playgroud)

提前致谢

python pygame pygame-surface

5
推荐指数
1
解决办法
89
查看次数