无法初始化pygame

时间:2018-06-09 16:04:00

标签: python raspberry-pi pygame

我正在尝试使用我的覆盆子pi在PiTFT 2.8“显示器上创建一个Button。如果我运行没有sudo的代码,会弹出一个窗口,它看起来没问题。我用sudo运行它应该出现在piTFT显示,但它在pygame.init()函数中冻结。

import pygame
from pygame.locals import *
import os

class Button:
def create_button(self, surface, color, x, y, length, height, width, text, text_color):
    surface = self.draw_button(surface, color, length, height, x, y, width)
    surface = self.write_text(surface, text, text_color, length, height, x, y)
    self.rect = pygame.Rect(x,y, length, height)
    return surface

def write_text(self, surface, text, text_color, length, height, x, y):
    font_size = int(length//len(text))
    myFont = pygame.font.SysFont("Calibri", font_size)
    myText = myFont.render(text, 1, text_color)
    surface.blit(myText, ((x+length/2) - myText.get_width()/2, (y+height/2) - myText.get_height()/2))
    return surface

def draw_button(self, surface, color, length, height, x, y, width):           
    for i in range(1,10):
        s = pygame.Surface((length+(i*2),height+(i*2)))
        s.fill(color)
        alpha = (255/(i+2))
        if alpha <= 0:
            alpha = 1
        s.set_alpha(alpha)
        pygame.draw.rect(s, color, (x-i,y-i,length+i,height+i), width)
        surface.blit(s, (x-i,y-i))
    pygame.draw.rect(surface, color, (x,y,length,height), 0)
    pygame.draw.rect(surface, (190,190,190), (x,y,length,height), 1)  
    return surface

def pressed(self, mouse):
    if mouse[0] > self.rect.topleft[0]:
        if mouse[1] > self.rect.topleft[1]:
            if mouse[0] < self.rect.bottomright[0]:
                if mouse[1] < self.rect.bottomright[1]:
                    print "Some button was pressed!"
                    return True
                else: return False
            else: return False
        else: return False
    else: return False


os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

pygame.init()
print "got here"
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((320, 240))
screen.fill((0,0,0))
pygame.display.update()
font_big = pygame.font.Font(None, 20)
print "here"
pygame.display.set_caption("Buttons.py - example")
button = Button()
screen.fill((30,144,255))
#Parameters:               surface,      color,       x,   y,           length, height, width,    text,      text_color
button.create_button(screen, (107,142,35), 100, 135, 100,    50,    0,            "Example", (255,255,255))
pygame.display.flip()
pygame.display.update()

0 个答案:

没有答案