I am a beginner at programming with less than 4 months of experience so, please pardon me if I haven’t asked the question correctly according to the standards
FEW DAYS AGO, I have created a project but whenever I tried to run it on Spyder(IDE) and Jupyter notebook, the Pygame window opens up but it is not responding (not working or showing anything on the screen) I have tried everything I can like updating Pygame to version 2.0,updating Spyder, putting one event handler:
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
terminate()
sys.exit()
the Spyder console on the other hand shows:
Restarting kernel...
and
(SpyderKernelApp) WARNING | No such comm: 7657c0fb289911eb9a4124b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: e7112fb1289b11eb90d224b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: b71d4cc1289d11eb906324b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: 8c66b5c828a011eb964024b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: 56eda5f928a411eba08524b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: d7db00b428a411ebac6824b6fd3c7b5a
(SpyderKernelApp) WARNING | No such comm: 3de6c19428a611eb91d124b6fd3c7b5a
and after I try to close the window it shows:
Kernel died, restarting
and always restarts.
I don’t know whether there is any bug or not in my code because I am a beginner and it is not showing on my (IDE) so please also recommend me how to debug any code and please give a detailed answer for my problem. Some of the starting code that I think may be wrong:
def main():
global BASICFONT
BASICFONT=pygame.font.Font(None,50)
window.fill(bgcolor)
def startscreen():
font=BASICFONT.render("Snake Game X",True,dgreen)
fontRect=font.get_rect.center=(250,250)
fontRect.fill(black)
window.blit(font,fontRect)
def keymsg():
font=BASICFONT.render("Please Press A Key To Start",True, red)
fontRect=font.get_rect.midbottom=(0,10)
fontRect.fill(black)
window.blit(font,fontRect)
def keypressed():
keypressed=pygame.key.get_pressed()
if keypressed==0 or keypressed==keypressed(pygame.K_ESCAPE):
terminate()
elif keypressed:
return
else:
pygame.time.wait(500)
terminate()
startscreen.keymsg()
startscreen.keypressed()
while run:
startscreen()
run()
if __name__=="Snake Game X":
main()
def run(score,run):
while run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
terminate()
sys.exit()
def randomlocation():
({"x":random.randint(20,win_w-20),
"y":random.randint(20,win_h-20)})
apple=randomlocation()
assert win_w % cell_s==0
assert win_h % cell_s==0
startX=random.randint(20,win_w-20)
startY=random.randint(20,win_h-20)
appleX=apple('x')*cell_s
appleY=apple('y')*cell_s
def drawgame():
def drawapple():
appleRect=window.rect(appleX,appleY,cell_s,cell_s)
appleRect.fill(red)
def drawgrid():
for x in range(0,win_w,20):
pygame.draw.line(window,dgray,0,win_h,5)
for y in range(0,win_h,20):
pygame.draw.line(window,dgray,0,win_w,5)
def drawsnake(snakeCoords):
snakeX=snakeCoords('x')
snakeY=snakeCoords('y')
snakeRect=window.get_rect(snakeX,snakeY,cell_s,cell_s)
snakeRect.fill(green)
window.blit(snakeRect)
def drawscore(score):
scorefont=BASICFONT.render(('SCORE: ',score),True,white)
scoreRect=scorefont.get_rect.topleft=(20,20)
window.blit(scorefont,scoreRect)
def gameover():
gameFont=BASICFONT.render("Game",True,red)
overFont=BASICFONT.render("Over",True,red)
gameRect=gameFont.get_rect.center=(100,100)
overRect=overFont.get_rect.center(100,100+30)
window.blit(gameFont,gameRect)
window.blit(overFont,overRect)
snakeCoords=({"x":startX,"y":startY},
{"x":startX-1,"y":startY},
{"x":startX-2,"y":startY})
direction=RIGHT
if snakeCoords(head)('x')==win_w-20 or snakeCoords(head)('y')==win_h-20:
gameover()
pygame.time.wait(500)
terminate()
sys.exit()
#apple eating snake
if appleX==snakeCoords(head)('x') or appleY==snakeCoords(head)('y'):
apple=randomlocation()
drawgame.drawApple()
score+=1
if appleX==snakeCoords(head)('x') and direction==RIGHT:
newhead=({'x':startX-3,'y':startY})
snakeCoords+=newhead
if appleX==snakeCoords(head)('x') and direction==LEFT:
newhead=({'x':startX+3,'y':startY})
snakeCoords+=newhead
if appleY==snakeCoords(head)('y') and direction==UP:
newhead=({'x':startX,'y':startY+3})
snakeCoords+=newhead
if appleY==snakeCoords(head)('y') and direction==DOWN:
newhead=({'x':startX,'y':startY-3})
snakeCoords+=newhead
#keys pressed
if event.type==pygame.event.get(pygame.KEYDOWN):
if keypressed==keypressed(pygame.K_RIGHT) and direction!=LEFT:
direction=RIGHT
elif keypressed==keypressed(pygame.K_LEFT) and direction!=RIGHT:
direction=LEFT
elif keypressed==keypressed(pygame.K_UP) and direction!=DOWN:
direction=UP
elif keypressed==keypressed(pygame.K_DOWN) and direction!=UP:
direction=DOWN
elif keypressed==keypressed(pygame.K_ESCAPE) :
terminate()
sys.exit()
else:
print("Invalid Key Pressed")
drawgame()
pygame.display.update()
if score==10:
gameover()
pygame.time.wait(500)
terminate()
and my Python version is 3.8.3 .