def run(): pygame.init() ai = Settings() screen = pygame.display.set_mode((ai.width,ai.height)) pygame.display.set_caption("Paint") paint = False space = False data = [] brush = Draw(screen) while True: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: paint = True if event.type == pygame.MOUSEBUTTONUP: paint = False if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: sys.exit() if event.key == pygame.K_SPACE: space = True #print("space True") if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: space = False screen.fill(ai.bg_color) if paint: x,y = pygame.mouse.get_pos() data.append([x,y]) for i in data: brush.draw(i[0],i[1]) if space: data = [] screen.fill(ai.bg_color) pygame.display.flip()
class Game: START_X = 1 START_Y = 3 def start(self): self.__snake = Snake(self.START_X, self.START_Y) self.__draw = Draw() self.__candy = CandyBall() self.__pongPallet = PongPallet() self.__startTime = time.perf_counter() self.__startTimeBall = time.perf_counter() self.__startTimePallet = time.perf_counter() self.__startTimeColorChange = time.perf_counter() self.__madePoint = False self.__colorChange = False self.__score = 0 def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_UP]: self.__snake.changeDirection(Direction.UP) if keys[pygame.K_DOWN]: self.__snake.changeDirection(Direction.DOWN) if keys[pygame.K_LEFT]: self.__snake.changeDirection(Direction.LEFT) if keys[pygame.K_RIGHT]: self.__snake.changeDirection(Direction.RIGHT) if time.perf_counter() - self.__startTime > 0.1: self.__snake.move() if (not self.__candyEaten()): self.__snake.removeBack() else: self.__candy = CandyBall() if (self.__snake.collision() or self.__snake.collisionWalls() or self.collisionPallet()): pygame.event.post(pygame.event.Event(GAMEOVER)) self.__startTime = time.perf_counter() self.__draw.draw(self.__snake, self.__candy, self.__pongPallet, self.__colorChange) if self.__madePoint: self.__candy = CandyBall() self.__score += 1 self.__madePoint = False if time.perf_counter() - self.__startTimeColorChange > 0.6: self.__colorChange = False if time.perf_counter() - self.__startTimePallet > 0.1: self.__pongPallet.move(self.__candy.coordinate()) self.__startTimePallet = time.perf_counter() if time.perf_counter() - self.__startTimeBall > 0.05: self.__candy.move() self.bounceCandy() self.__startTimeBall = time.perf_counter() def getScore(self): return self.__score def __candyEaten(self): (xSnake, ySnake) = self.__snake.get().front() (xCandy, yCandy) = self.__candy.coordinate() if round(xCandy) == xSnake and round(yCandy) == ySnake: return True return False def collisionPallet(self): (x, y) = self.__snake.get().front() yPallet = self.__pongPallet.get() if x == 0: if y < (yPallet + Draw.PALLET_LENGTH) and y > yPallet: return True return False def bounceCandy(self): (x, y) = self.__candy.coordinate() yPallet = self.__pongPallet.get() if x < 0.3: # Value found by trail and error self.__madePoint = True self.__colorChange = True self.__startTimeColorChange = time.perf_counter() elif round(x) < 2: if y < (yPallet + Draw.PALLET_LENGTH) and y > yPallet: angle = self.__candy.getAngle() angle = 360 - angle if self.__pongPallet.getDirection() == Direction.UP: angle += 20 elif self.__pongPallet.getDirection() == Direction.DOWN: angle -= 20 self.__candy.setAngle(angle) else: _Node = self.__snake.get().getFrontNode() coordinates = [] while _Node.next() is not None: coordinates.append(_Node.get()) _Node = _Node.next() if self.__snake.getDirection( ) == Direction.UP or self.__snake.getDirection() == Direction.DOWN: self.testForCollisionInX(coordinates) elif self.__snake.getDirection( ) == Direction.LEFT or self.__snake.getDirection( ) == Direction.RIGHT: self.testForCollisionInY(coordinates) def testForCollisionInX(self, coordinates): (x, y) = self.__candy.coordinate() x = round(x) y = round(y) if coordinates.count((x + 1, y)) >= 1 or coordinates.count((x - 1, y)): angle = self.__candy.getAngle() angle = 360 - angle self.__candy.setAngle(angle) def testForCollisionInY(self, coordinates): (x, y) = self.__candy.coordinate() x = round(x) y = round(y) if coordinates.count((x, y + 1)) >= 1 or coordinates.count((x, y - 1)): angle = self.__candy.getAngle() angle = 180 - angle self.__candy.setAngle(angle)
from draw import Draw url = 'https://view.inews.qq.com/g2/getOnsInfo?name=wuwei_ww_cn_day_counts' response = requests.get(url=url) responseJson = response.json() data = json.loads(responseJson['data']) # map函数 CovidModel是CovidModel初始化方法的简写 covidModels = map(CovidModel, data) # 通过时间戳进行排序 covidModels = sorted(covidModels,key = lambda model:model.timeStamp) #获取增加模型数组 addCovidModels = AddCovidModel.createAddCovidModels(covidModels = covidModels) #获取日期和增加病例 dates, confirms = AddCovidModel.creatDatesAndConfirms(addCovidModels = addCovidModels) #绘图 Draw.draw(dates, confirms) # 当前工作目录 current working directory path = os.getcwd() # 完成后打开文件夹 os.system(r"open {}".format(path))
import config as cfg from entity_parameters import EntityParameters as EP from position.position import Position from logical_world import LogicalWorld from draw import Draw head = Head( parameters={ EP.POSITION: Position( left=0, top=0, right=cfg.CANVAS_WIDTH, bottom=cfg.CANVAS_HEIGHT), EP.TYPE: 'woman' }) print('head', head) class MiniLogicalWorld(LogicalWorld): def __init__(self): pass logical_world = MiniLogicalWorld() logical_world.entities = [head] print(logical_world) logical_world._create_parts_of_entities() draw = Draw(logical_world) draw.draw(save=True)
# detector = Detector("models/net_Adam_garbage_old_stack_cls.pth") image_array = os.listdir(cfg.IMAGE_PATH) count = 1 for image_name in image_array: # 处理多张图片 image = cv2.imread(os.path.join(cfg.IMAGE_PATH, image_name)) image = image[:, :, ::-1] img = Image.fromarray(image, "RGB") # image = Image.open(os.path.join(cfg.IMAGE_PATH, image_name)) start_time = time.time() box = detector.detect(img, 0.61, cfg.ANCHORS_GROUP) print(box) end_time = time.time() print(end_time - start_time) # print(box) draw.draw(img, box, None, False, count) count += 1 # 处理视频 # cap = cv2.VideoCapture(r"F:\Project\Yolo V3\data\video\jj.mp4") # # fps = cap.get(cv2.CAP_PROP_FPS) # # print(fps) # while True: # ret, frame = cap.read() # if ret: # start_time = time.time() # # 将每一帧通道为BGR转换成RGB,用于后面将每一帧转换成图片 # frames = frame[:, :, ::-1] # image = Image.fromarray(frames, 'RGB') # width, high = image.size # x_w = width / 416
if __name__ == '__main__': while True: print('Write your sentence:') sentence_txt = input('') if sentence_txt.strip() == '': continue try: sentence = Sentence(sentence_txt, cfg.NLP) # print('Text: {}'.format(sentence)) grammatical_objects = sentence.get_grammatical_objects() subjects = sentence.get_subjects() grammatical_object_pairs = sentence.get_grammatical_object_pairs() attributes = sentence.get_attributes() grammatical_world = GrammaticalWorld( subjects=subjects, objects=grammatical_objects, objects_pairs=grammatical_object_pairs, attributes=attributes) print('Grammatical world: {}'.format(grammatical_world)) logical_world = LogicalWorld(grammatical_world) # print('Logical world: {}'.format(logical_world)) draw = Draw(logical_world) img = draw.draw(save=True, display=False) except Exception as e: print(e)
def main(portrait=False): d = Draw(portrait) width = d.size[0] height = d.size[1] # Center pixel d.pixel(width // 2, height // 2, 0) # 3-by-3 vertical lines for i in range(height): d.pixel(width // 3, i, 0) d.pixel(2 * width // 3, i, 0) # 3-by-3 horizontal lines for i in range(width): d.pixel(i, height // 3, 0) d.pixel(i, 2 * height // 3, 0) # Diagonals d.line(0, 0, width - 1, height - 1, 0) d.line(0, height - 1, width - 1, 0, 0) # Center lines d.hline(0, height // 2, width, 0) d.vline(width // 2, 0, height, 0) # Sixths box using all the lines functions # Horizontal and Vertical lines using the hline and vline functions d.hline(width // 6, height // 6, 4 * width // 6, 0) d.vline(width // 6, height // 6, 4 * height // 6, 0) # Horizontal and Vertical lines using the line functions d.line(width // 6, 5 * height // 6, 5 * width // 6, 5 * height // 6, 0) d.line(5 * width // 6, height // 6, 5 * width // 6, 5 * height // 6, 0) # Quarter box using rect function d.rect(width // 4, height // 4, 3 * width // 4, 3 * height // 4, 0) # Third box using rect function with fill d.rect(width // 3, height // 3, 2 * width // 3, 2 * height // 3, 0, True) # Circle radius = min(height // 3, width // 3) d.circle(width // 2, height // 2, radius, 0) # Circle with fill radius = min(height // 6, width // 6) d.circle(width // 6, height // 2, radius, 0, True) # Write "this is only a text" at the origin of the display d.text('this is only a text', 0, 0, 0) # Write every available character on the screen. all_chars = sorted(font.keys()) y = height x = 0 for n, letter in enumerate(all_chars): if (n * 8) % width == 0: y -= 8 x = 0 else: x += 8 d.text(letter, x, y, 0) d.draw()