使用Python和pygame中的线程进行Promblem

时间:2017-07-22 15:28:57

标签: python multithreading pygame

控制台表示画线但不在屏幕上。 屏幕是填充并正确翻转。 我的Python是3.6 这是pygame的问题还是我的代码? 附:我现在认为这个鳕鱼写的不好,但我是编码新手。谢谢你的帮助。

import random
import time as timer
import pygame
from pygame import *
import threading
timeAndNum = dict()
screen = pygame.display.set_mode((900,450))
ranges = [1000,2000,3000,4000,5000,6000,7000,8000]
ip = 0
begintime = 0
endtime = 0
pygame.init()
def bubble_sort(arr):
    for i in range(len(arr)):
        for j in range(len(arr) - 1, i, -1):
            if arr[j] < arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return arr
def Create(Numbers):
    global ranges
    global ip
    for i in range(0,ranges[ip]):
        Number = random.randint(0,1000000)
        Numbers.append(Number)
    ip += 1
ip2 = 0
def Createxy(ranges,timeAndNum):
    Sps = []
    taml = len(timeAndNum)
    for rang in ranges[0:taml]:
        x = rang // 20
        y = int(timeAndNum[rang]) * 2
        cordinats = (x,768 - y)
        crd = [cordinats]
        if not crd in Sps:
            Sps.append(cordinats)
        crd = []
    return Sps
def drawlines(Sps,screen):
    for cordinats in Sps:
        if Sps.index(cordinats) == 0:
            x0,y0 = 0,768
        else:
            x0,y0 = Sps[Sps.index(cordinats) - 1]
        x1,y1 = cordinats
        pygame.draw.line(screen,(255,255,255),(x0,y0),(x1,y1))
def Creation():
    global timeAndNum
    global ranges
    global endtime
    global ip2
    for i in range(len(ranges)):
        times = timer.clock()
        begintime = endtime
        Numbers = []
        Create(Numbers)
        bubble_sort(Numbers)
        times = timer.clock()
        endtime = times
        print(times)
        timeAndNum[ranges[ip2]] = endtime
        ip2 += 1
def Drawing():
    global screen
    print('Printing Something...')
    print('Printing Something...1')
    screen.fill((255,0,0))
    print('Printing Something...2')
    Q = True
    while Q == True:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    Q = False
            elif event.type == QUIT:
                    Q = False
        timer.sleep(1)
        Sps = Createxy(ranges,timeAndNum)
        drawlines(Sps,screen)
        pygame.display.flip()
        print(Sps)
    print(timeAndNum)
thread = threading.Thread(target=Creation)
thread1 = threading.Thread(target=Drawing)
thread.start()
thread1.start()

0 个答案:

没有答案