I am trying to create a maze interface with two stimuli flashing at right and left side. The code runs, opens the interface and closes immediately. I know it is because the loop is not running properly. Since I am a beginner in Python, I can't really find where the bug is.
Can anyone take a look and help me resolve this error?
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()
This is the snippet of another module called flicky.py which I called in the main code:
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)
This defines the brain module:
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"
Sorry for the mess here. But basically what i am trying to do is to combine the below two packages into one. To have a maze screen in the middle and two flickering boards at left and right. I am able to run the packages one by one successfully but when i combined, it is not getting displayed together. I am just a beginner in coding and that is why the code is getting messed.
This is the first package, where two stimuli flickers:
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()
This is the second package of maze layout.
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()
Can anyone help me figure out how to combine both of these into one? For me a screen with two boards is the only thing coming. I am not able to display the maze screen. Any help would be great. Thanks in Advance!
There is a syntax error in the line:
while not exit and while done == False:
it should probably be:
while not exit and not done:
When drawing the line screen.fill((0,0,0))
, it is referencing screen
which doesnt exist, I think you want to use gameDisplay
instead.
brain
is also never defined and so will throw an error when calling brain.headto()
.
blockImg
also never appears to be defined.
These issues are causing the program to crash immediately after starting. To see any errors that are causing the program to crash, run the code through IDLE or an IDE rather than running the .py file as this will usually provide feedback about the crash.
Also, it would be recommended use another variable name other than exit
as this can have special meanings in python.
Check out if this example works as intended. I couldn't test it with the brain
module and just used the arrow keys instead.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.