0

I am using Python 3.4 with Pygame 1.9.2 on a windows 8 hp computer. I am working on a simple snake game where the worm eats the apple and gets longer. The code seems to be fine as far as I know, and the game works for what I have so far (worm doesn't grow yet). But the problem I'm having is that while I am testing the game it will occasionally, temporarily freeze the keys. The game will still run, but I can't use the keys for a few seconds or so, causing game over as the worm runs into the edge of the window. If I leave the curser within the screen and click, the functionality will return sooner, but you shouldn't have to deal with this problem while playing a snake game. Every second counts! Any suggestions?

    import pygame
    import time
    import random
    pygame.init()

    white = (255,255,255)
    black = (0,0,0)
    red = (255,0,0)
    green = (0,255,0)

    display_width = 800
    display_height = 600

    gameDisplay = pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption("Game Experiment")

    clock = pygame.time.Clock()

    framePsecond = 10
    block_size = 10
    font = pygame.font.SysFont(None, 25)

    def message_to_screen(msg,color):
        screen_text = font.render(msg, True, color)
        gameDisplay.blit(screen_text, [display_width/2 - 175,display_height/2])

    def gameLoop():
        gameExit = False
        gameOver = False

        lead_x = display_width/2
        lead_y = display_height/2

        lead_x_change = 0   
        lead_y_change = 0

        randApplex = round(random.randrange(0, display_width-block_size)/10.0)*10.0
        randAppley = round(random.randrange(0, display_height-block_size)/10.0)*10.0

        while not gameExit:

            while gameOver == True:
                gameDisplay.fill(black)
                message_to_screen("Game over! Press 'p' to play again or 'q' to quit",white)
                pygame.display.update()

                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            gameExit = True
                            gameOver = False
                        if event.key == pygame.K_p:
                            gameLoop()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True 
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        lead_x_change = -block_size
                        lead_y_change = 0
                    elif event.key == pygame.K_RIGHT:
                        lead_x_change = block_size
                        lead_y_change = 0
                    elif event.key == pygame.K_UP:
                        lead_y_change = -block_size
                        lead_x_change = 0
                    elif event.key == pygame.K_DOWN:
                        lead_y_change = block_size
                        lead_x_change = 0

            if lead_x >= display_width or lead_x <= 0 or lead_y >= display_height or lead_y < 0:
                gameOver = True

            lead_x += lead_x_change
            lead_y += lead_y_change

            gameDisplay.fill(green)
            pygame.draw.rect(gameDisplay, red, [randApplex,randAppley,block_size,block_size])
            pygame.draw.rect(gameDisplay, red, [lead_x,lead_y,block_size,block_size])
            pygame.display.update()

             if lead_x == randApplex and lead_y == randAppley:
                randApplex = round(random.randrange(0, display_width-block_size)/10.0)*10.0
                randAppley = round(random.randrange(0, display_height-block_size)/10.0)*10.0

            clock.tick(framePsecond)

        pygame.quit()
        quit()


    gameLoop()
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 2
    This could well be a problem with the code, but it's impossible to tell unless you post it. Have you done anything to isolate the behaviour? – michaelrccurtis Oct 07 '15 at 03:10
  • Pygame only has support for python 3.1 and 3.2 so i would guess that is the problem. There isn't any fix for it hasn't been updated since 2009. – Paradizigmania Oct 07 '15 at 05:39
  • Please post your code, otherwise nobody can help you. – sloth Oct 07 '15 at 08:48
  • @Paradizigmania Pygame works fine with python 3.4 – sloth Oct 07 '15 at 08:49
  • @sloth sorry thought it might not work because there is no version for 3.4 – Paradizigmania Oct 07 '15 at 08:50
  • @Paradizigmania You'll have to install it with `pip`/`wheel`. The pygame homepage is usually horribly out-of-date. All the action happens directly at bitbucket. – sloth Oct 07 '15 at 08:59
  • @sloth thanks now I can finally update it myself – Paradizigmania Oct 07 '15 at 11:22
  • @michaelrccurtis I don't think it has to do with the code, but I'll post it all the same. It is basically doing the same thing it would do if you clicked your mouse outside the window, but I haven't clicked anything. It does it on it's own. – James Scott Oct 08 '15 at 20:14

0 Answers0