-3
我的代碼示例只導致黑屏,我不知道爲什麼。我在Windows 7上運行python 2.7,pygame和easygui。爲什麼我的代碼示例導致黑屏?
import pygame, easygui, io, time
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (255, 0, 255)
PURPLE = (130, 0, 130)
programMode = 0
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 20)
def printline(self, screen, textString):
textBitmap = self.font.render(textString, True, WHITE)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def printDebugLine(self, screen, textString):
textBitmap = self.font.render(textString, True, RED)
screen.blit(textBitmap, [self.debugX, self.debugY])
self.debugY += self.debugLineHeight
def reset(self):
self.x = 600
self.debugX = 600
self.y = 400
self.debugY = 400
self.line_height = 15
self.debugLineHeight = 13
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
size = [950, 750]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Robot Joystick Control Version 2.0")
done = False
clock = pygame.time.Clock()
pygame.joystick.init()
textPrint = TextPrint()
myfont = pygame.font.SysFont("monospace", 25)
camImg = pygame.image.load('C:\Users\Evan Develop\Desktop\Python Code Examples\Zalophus/camImg.png')
camImgx = 425
camImgy = 20
rovImg = pygame.image.load('C:\Users\Evan Develop\Desktop\Python Code Examples\Zalophus/rovImg.png')
rovImgx = 425
rovImgy = 385
hG = pygame.image.load('C:\Users\Evan Develop\Desktop\Python Code Examples\Zalophus/hG.png')
hGx = 10
hGy = 500
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
JOYBUTTONUP JOYHATMOTION
if event.type == pygame.JOYBUTTONDOWN:
textPrint.printline(screen, "Joystick button pressed.")
screen.fill(BLACK)
textPrint.reset()
joystick_count = pygame.joystick.get_count()
textPrint.printline(screen, "Number of joysticks: {}".format(joystick_count))
textPrint.indent()
if (joystick_count == 0):
textPrint.printline(screen, "No joysticks connected...")
if (joystick_count == 1):
i = 0
joystick = pygame.joystick.Joystick(i)
joystick.init()
textPrint.printDebugLine(screen, "Joystick {}".format(i))
textPrint.indent()
name = joystick.get_name()
textPrint.printDebugLine(screen, "Joystick name: {}".format(name))
axes = joystick.get_numaxes()
textPrint.printDebugLine(screen, "Number of axes: {}".format(axes))
textPrint.indent()
for i in range(axes):
axis = (joystick.get_axis(i) * -100)
textPrint.printDebugLine(screen, "Axis {} value: {:>6.3f}".format(i, axis))
textPrint.indent()
buttons = joystick.get_numbuttons()
textPrint.printline(screen, "Number of buttons: {}".format(buttons))
textPrint.indent()
almostExplode1 = False
almostExplode2 = False
for i in range(buttons):
button = joystick.get_button(i)
textPrint.printline(screen, "Button {:>2} value: {}".format(i,button))
if (i == 8 and button == 1):
almostExplode1 = True
if (i == 9 and button == 1):
almostExplode2 = True
if (almostExplode1 == True and almostExplode2 == True):
print ("\n \n OH NOOOOOO!!! Joystick EXPLODED!!!!\n\n" * 20)
done = True
break
textPrint.unindent()
hats = joystick.get_numhats()
textPrint.printline(screen, " Number of hats: {}".format(hats))
textPrint.indent()
for i in range(hats):
hat = joystick.get_hat(i)
textPrint.printline(screen, "Hat {} value: {}".format(i, str(hat)))
textPrint.indent()
numOfAxes = axes
directionByte = 0
# Axes used for driving, drawing, and direction byte.
yAxis = int(joystick.get_axis(1) * -100)
xAxis = int(joystick.get_axis(0) * -100)
zAxis = int(joystick.get_axis(2) * -100)
wAxis = int(joystick.get_axis(3) * -100)
if (yAxis > 10):
directionByte = 1
elif (yAxis < -10):
directionByte = 2
if (xAxis > 10 and (xAxis > yAxis and xAxis > -yAxis)):
directionByte = 3
elif (xAxis < -10 and (xAxis < yAxis and xAxis < -yAxis)):
directionByte = 4
if (wAxis > 10 and (wAxis > yAxis and wAxis > -yAxis and wAxis > xAxis and wAxis > -xAxis)):
directionByte = 5
elif (wAxis < -10 and (wAxis < yAxis and wAxis < -yAxis and wAxis < xAxis and wAxis < -xAxis)):
directionByte = 6
yAxisDot = int(joystick.get_axis(1) * 250 + 600)
xAxisDot = int(joystick.get_axis(0) * 250 + 350)
zAxisDot = int(joystick.get_axis(2) * 250 + 350)
wAxisDot = int(joystick.get_axis(3) * 250 + 600)
pygame.draw.circle(screen, BLUE, [xAxisDot, 600], 15, 0)
pygame.draw.circle(screen, RED, [350, yAxisDot], 15, 0)
pygame.draw.circle(screen, GREEN, [zAxisDot, 600], 15, 0)
pygame.draw.circle(screen, WHITE, [350, wAxisDot], 15, 0)
label = myfont.render("Cam Feed #1", 1, (255,255,0))
screen.blit(label, (450, 25))
label = myfont.render("Cam Feed #2", 1, (255,255,0))
screen.blit(label, (450, 390))
pygame.display.flip()
clock.tick(25)
pygame.quit()
您需要在'while'循環內的某個地方調用'pygame.display.flip()',否則在您翻轉顯示屏之前,您已經完成/繪製的任何內容都不會顯示。 – Marius
你寫了嗎?它曾經工作過嗎?嘗試刪除代碼,直到它再次開始工作。 –
「你好,我有這個Python代碼」不是一個問題。 – dimo414