Pygame_Python - 屏幕立即打开和关闭

时间:2017-09-25 19:26:37

标签: python pygame

我正在尝试创建一个迷宫界面,其中两个刺激在左右两侧闪烁。代码运行,打开界面并立即关闭。我知道这是因为循环运行不正常。由于我是Python的初学者,我无法真正找到bug的位置。

任何人都可以看看并帮我解决此错误吗?

import sys; sys.path.append('..') 
import pygame
from flicky import FlickyManager
import open_bci_v3 as bci
import os
import logging
import time
from pygame.locals import *
import numpy
from collections import *
import math
import pandas as pd
import brain

pygame.init()

display_width = 800
display_height = 800

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Cursor Control by SSVEP')

done = False
clock=pygame.time.Clock()
flickymanager = FlickyManager(screen)

# 7.5 Hz
flickymanager.add('left',8.33)
# 12. 5 Hz
flickymanager.add('right',5) 

exit = False

playerImg = pygame.image.load('player.bmp')

def player(x,y):
    gameDisplay.blit(playerImg, (x,y))

x = 250
y = 90
x_change = 0

def maze():
    M = 10
    N = 9
    maze = [ 1,1,1,1,1,1,1,1,1,1,
             1,0,0,0,0,0,0,0,0,1,
             1,1,1,1,1,1,1,1,0,1,
             1,0,0,0,0,0,0,1,0,1,
             1,0,1,1,1,1,0,1,0,1,
             1,0,1,0,0,0,0,1,0,1,
             1,0,1,1,1,1,1,1,0,1,
             1,0,0,0,0,0,0,0,0,1,
             1,1,1,1,1,1,1,1,1,1 ]
    bx = 0
    by = 0
    for i in range(0, M * N):
        if maze[ bx + (by * M) ] == 1:
            gameDisplay.blit(blockImg,( 200 + bx * 40 , 40 + by * 40 ))
        bx = bx + 1
        if bx == M:
            bx = 0 
            by = by + 1

while not exit and while done == False:
    for event in pygame.event.get():
        if (event.type == pygame.KEYUP) or (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_ESCAPE):
                done = True

        if event.type == pygame.QUIT:
            exit = True
            done = True

    screen.fill((0,0,0))
    clock.tick(60) # 16 ms between frames ~ 60FPS
    flickymanager.process()
    flickymanager.draw()
    pygame.display.flip()

    if brain.headto() == 'right':
        x_change = 20
    elif brain.headto() == 'left':
        x_change = -20
    elif brain.headto() == 'stay':
        x_change = 0

    x += x_change

    player(x,y)
    maze()

    pygame.display.update()



pygame.quit()

这是另一个名为flicky.py的模块的片段,我在主代码中调用了它:

class FlickyManager:
    def __init__(self,screen):
        self.flickies = []
        self.screen = screen
    def addFlicky(self,f):
        self.flickies.append(f)
    def add(self,location,frames):
        w, h = self.screen.get_size()
        if location == 'left':
            x = 0; y = h / 2 - A/2;
        elif location == 'right':
            x = w - A; y = h / 2 - A/2;
        elif location == 'top':
            y = 0; x = w / 2 - A/2;
        elif location == 'bottom':
            y = h-A; x = w / 2 - A/2;
        else:
            raise ValueError("location %s unknown" % location)
        f = Flicky(x,y,frames)
        self.flickies.append(f)
    def process(self):
        for f in self.flickies:
            f.process()
    def draw(self):
        for f in self.flickies:
            f.draw(self.screen)

这定义了大脑模块:

def headto( ):
    board.start_streaming(getsample, 1.5)
    average7_5 = numpy.mean(e7_5Output)
    average12_5 = numpy.mean(e12_5Output)
    print average7_5
    print average12_5
    average = (average7_5 + average12_5)/2
    print average
    if  10000 < average < 30000:
       return "right"
    elif 30000 <= average < 60000:
       return "left"
    else:
       return "stay"

对不起这里的烂摊子。但基本上我要做的是将以下两个包合二为一。在中间有一个迷宫屏幕,在左边和右边有两个闪烁的板。我能够成功地逐个运行包,但是当我合并时,它不能一起显示。我只是编码的初学者,这就是代码混乱的原因。

这是第一个包,两个刺激闪烁:

import pygame #@UnusedImport
from flicky import FlickyManager

pygame.init()
screen=pygame.display.set_mode([800,200])
pygame.display.set_caption("Stimulus")

done=False
clock=pygame.time.Clock()
flickymanager = FlickyManager(screen)

# 7.5 Hz
flickymanager.add('left',8.33)
# 12. 5 Hz
flickymanager.add('right',5) 


while done==False:
    for event in pygame.event.get():
        if (event.type == pygame.KEYUP) or (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_ESCAPE):
                done = True
        if event.type == pygame.QUIT:
            done = True
    screen.fill((0,0,0))
    clock.tick(60) # 16 ms between frames ~ 60FPS
    flickymanager.process()
    flickymanager.draw()
    pygame.display.flip()

pygame.quit()

这是迷宫布局的第二个包。

import sys; sys.path.append('..') 
import pygame
import open_bci_v3 as bci
import os
import logging
import time
from pygame.locals import *
import numpy
from collections import *
import math
import pandas as pd
import brain

pygame.init()

display_width = 800
display_height = 800

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Maze Game by SSVEP')

white = (255,255,255)

exit = False
playerImg = pygame.image.load('player.bmp')
blockImg = pygame.image.load('block.bmp')

def player(x,y):
    gameDisplay.blit(playerImg, (x,y))

x = 250
y = 90
x_change = 0

def maze():
    M = 10
    N = 9
    maze = [ 1,1,1,1,1,1,1,1,1,1,
             1,0,0,0,0,0,0,0,0,1,
             1,1,1,1,1,1,1,1,0,1,
             1,0,0,0,0,0,0,1,0,1,
             1,0,1,1,1,1,0,1,0,1,
             1,0,1,0,0,0,0,1,0,1,
             1,0,1,1,1,1,1,1,0,1,
             1,0,0,0,0,0,0,0,0,1,
             1,1,1,1,1,1,1,1,1,1 ]
    bx = 0
    by = 0
    for i in range(0, M * N):
        if maze[ bx + (by * M) ] == 1:
            gameDisplay.blit(blockImg,( 200 + bx * 40 , 40 + by * 40 ))
        bx = bx + 1
        if bx == M:
            bx = 0 
            by = by + 1

while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True

    if brain.headto() == 'right':
        x_change = 20
    elif brain.headto() == 'left':
        x_change = -20
    elif brain.headto() == 'stay':
        x_change = 0

    x += x_change

    gameDisplay.fill(white)
    player(x,y)
    maze()

    pygame.display.update()

pygame.quit()
quit()

任何人都可以帮我弄清楚如何将这两者合二为一吗?对我来说,只有两块板的屏幕是唯一的。我无法显示迷宫画面。任何帮助都会很棒。在此先感谢!

2 个答案:

答案 0 :(得分:1)

该行中存在语法错误:

while (given > 0){ remainder = given % base; if (remainder >= 10){ switch(remainder){ case 10:output = "A" + output;break; case 11:output = "B" + output;break; case 12:output = "C" + output;break; case 13:output = "D" + output;break; case 14:output = "E" + output;break; case 15:output = "F" + output;break; } } else { output = remainder + output; } given /= base; } System.out.println(output);

应该是:

while not exit and while done == False:

在绘制第while not exit and not done:行时,它引用的screen.fill((0,0,0))不存在,我认为您想要使用screen

gameDisplay也从未定义过,因此在调用brain时会抛出错误。

brain.headto()似乎也从未被定义过。

这些问题导致程序在启动后立即崩溃。 要查看导致程序崩溃的任何错误,请通过IDLE或IDE运行代码,而不是运行.py文件,因为这通常会提供有关崩溃的反馈。

此外,建议使用除blockImg以外的其他变量名,因为这在python中具有特殊含义。

答案 1 :(得分:0)

检查此示例是否按预期工作。我无法使用brain模块对其进行测试,只是使用了箭头键。

import pygame as pg

import brain
from flicky import FlickyManager


def maze():
    M = 10
    N = 9
    maze = [
        1,1,1,1,1,1,1,1,1,1,
        1,0,0,0,0,0,0,0,0,1,
        1,1,1,1,1,1,1,1,0,1,
        1,0,0,0,0,0,0,1,0,1,
        1,0,1,1,1,1,0,1,0,1,
        1,0,1,0,0,0,0,1,0,1,
        1,0,1,1,1,1,1,1,0,1,
        1,0,0,0,0,0,0,0,0,1,
        1,1,1,1,1,1,1,1,1,1,
        ]

    bx = 0
    by = 0
    for i in range(0, M * N):
        if maze[ bx + (by * M) ] == 1:
            screen.blit(blockImg,( 200 + bx * 40 , 40 + by * 40 ))
        bx = bx + 1
        if bx == M:
            bx = 0
            by = by + 1


pg.init()

screen = pg.display.set_mode((800, 800))
clock = pg.time.Clock()

white = (255, 255, 255)

playerImg = pg.image.load('player.bmp').convert()
blockImg = pg.image.load('block.bmp').convert()

flickymanager = FlickyManager(screen)
flickymanager.add('left',8.33)  # 7.5 Hz
flickymanager.add('right',5)  # 12. 5 Hz

x = 250
y = 90
x_change = 0

done = False

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    if brain.headto() == 'right':
        x_change = 20
    elif brain.headto() == 'left':
        x_change = -20
    elif brain.headto() == 'stay':
        x_change = 0

    x += x_change
    flickymanager.process()

    # Draw everything.
    screen.fill(white)
    maze()
    screen.blit(playerImg, (x, y))
    flickymanager.draw()

    pg.display.update()
    clock.tick(30)

pg.quit()