pygameSurfaceget_rect()方法返回值是一个Rect对象,该对象有一个move()方法可以用于移动矩形。move(x, y)函数有两个参数,第一个参数是 X 轴移动的距离,第二个参数是 Y 轴移动的距离。窗口的左上角是(0, 0),如果是move(100, 50)就是左移100下移50。
我再win上测试没有问题,
import cv2
cap = cv2VideoCapture(0)
while 1:
ret, frame = capread()
k = cv2waitKey(1)
if k == 27:
break
cv2imshow("capture", frame)
caprelease()
cv2destroyAllWindows()
实在不行试试cv+pygame
代码如下:
import pygame
import cv2
cap = cv2VideoCapture(0)
ret, img = capread()
if not ret:
print("Can't read stream")
img = cv2transpose(img)
print('shape:', imgshape)
pygameinit()
screen = pygamedisplayset_mode((imgshape[0],imgshape[1]))
surface = pygamesurfaceSurface((imgshape[0], imgshape[1]))
running = True
while running:
for event in pygameeventget():
if eventtype == pygameQUIT:
running = False
ret, img = capread()
if not ret:
running = False
break
else:
img = cv2transpose(img)
pygamesurfarrayblit_array(surface, img)
screenblit(surface, (0,0))
pygamedisplayflip()
pygamequit()
把开发过程中常用的一些内容片段记录起来,下边内容是关于python通过PyGame绘制图像并保存为文件的内容,希望对大伙有较大好处。
''' pg_draw_circle_save101py
draw a blue solid circle on a white background
save the drawing to an image file
tested with Python 27 and PyGame 192 by vegaseat 16may2013
'''
import pygame as pg
# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)
width = 300
height = 300
# create the display window
win = pgdisplayset_mode((width, height))
# optional title bar caption
pgdisplayset_caption("Pygame draw circle and save")
# default background is black, so make it white
winfill(white)
# draw a blue circle
# center coordinates (x, y)
radius = min(center)
# width of 0 (default) fills the circle
# otherwise it is thickness of outline
width = 0
# drawcircle(Surface, color, pos, radius, width)
pgdrawcircle(win, blue, center, radius, width)
# now save the drawing
# can save as bmp tga png or jpg
fname = "circle_bluepng"
pgimagesave(win, fname)
print("file {} has been saved"format(fname))
# update the display window to show the drawing
pgdisplayflip()
# event loop and exit conditions
# (press escape key or click window title bar x to exit)
while True:
for event in pgeventget():
if eventtype == pgQUIT:
# most reliable exit on x click
pgquit()
raise SystemExit
elif eventtype == pgKEYDOWN:
# optional exit with escape key
if eventkey == pgK_ESCAPE:
pgquit()
raise SystemExit
以上就是关于python pygame 中如何让角色移动到地图中间 在按下 方向键 向右后 地图向左移动全部的内容,包括:python pygame 中如何让角色移动到地图中间 在按下 方向键 向右后 地图向左移动、mac本使用opencv打开摄像头导致python意外退出、python通过PyGame绘制图像并保存为图片文件的代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)