我正在尝试在更大的屏幕中运行此代码。我需要那个跟随球的人不要分心,并开始在监视器中看到另一个东西。要做到这一点,我需要我已经拥有的程序,在黑色背景的另一个屏幕上运行。里面的代码必须有1366x768(已经设置好了)。
这是.py代码:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty,\
ObjectProperty
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.config import Config
import select
import v4l2capture
import time
import os
import threading
import datetime
count = 0
aux = 0
aux_2 = 0
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongGame(Widget):
ball = ObjectProperty(None)
def serve_ball(self, vel=(0, 4)):
self.ball.center = self.initial_pos
self.ball.velocity = vel
def update(self, dt):
self.ball.move()
global count, aux, aux_2
# bounce ball off bottom or top or left or right
if self.ball.top > self.top and aux == 0: #bounce top
self.ball.velocity_y = -2
self.ball.velocity_x = -4
aux += 1
elif self.ball.x < 0 and count == 0: #bounce left first time
self.ball.velocity_x = 4
self.ball.velocity_y = 0
count += 1
elif self.ball.x < 0 and count == 1: #bounce left second time
self.ball.velocity_x = 4
self.ball.velocity_y = 2
elif self.ball.x + self.ball.width > self.right: #bounce right
self.ball.velocity_x = -3.5
self.ball.velocity_y = -2
elif self.ball.y < 0 and aux_2 == 0: #bounce bottom first time
self.ball.velocity_x = -3.5
self.ball.velocity_y = 2
aux_2 += 1
elif self.ball.y < 0 and aux_2 == 1:
self.ball.velocity_x = 2
self.ball.velocity_y = 4
aux_2 = 0
elif self.ball.top > self.top and aux == 1: #bounce top second time
self.ball.velocity_x = 2
self.ball.velocity_y = -4
# print("Bola - X =%s \n" % self.ball.x)
# print("Bola - Y = %s \n" % self.ball.y)
class PongApp(App):
def build(self):
game = PongGame()
return game
def on_start(self):
self.root.serve_ball()
Clock.schedule_interval(self.root.update, 1.0 / 60.0)
def on_stop(self):
return
class captureThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
pass
# video = v4l2capture.Video_device("/dev/video0")
# #size_x, size_y = video.set_format(640, 480, fourcc='H264')
# video.create_buffers(30)
# video.queue_all_buffers()
# video.start()
# stop_time = time.time() + 30.0
# with open('video.raw', 'wb') as f:
# while stop_time >= time.time():
# # Wait for the device to fill the buffer.
# select.select((video,), (), ())
# # The rest is easy :-)
# image_data = video.read_and_queue()
# f.write(image_data)
# video.close()
# print "OK"
# os.system('ffmpeg -f h264 -i video.raw -vcodec copy out.mp4 -y')
# print "Convertido"
# ffmpeg_extract_subclip("out.mp4", 0, 100, targetname="test.mp4")
class gameThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
PongApp().run()
if __name__ == '__main__':
thread1 = gameThread(1, "Thread-game")
#thread2 = captureThread(2, "Thread-Capture")
thread1.start()
#thread2.start()
print(datetime.datetime.now())
stop_time = time.time() + 30.0
while time.time() < stop_time:
pass
PongApp().stop()
print(datetime.datetime.now())
.kv文件:
<PongBall>:
size: 50, 50
canvas:
Ellipse:
pos: self.pos
size: self.size
<PongGame>:
ball: pong_ball
initial_pos: [self.center_x , pong_ball.height/2]
PongBall:
id: pong_ball
感谢您的任何提示!