Home > Software engineering >  Pygame freezes on startup
Pygame freezes on startup

Time:11-17

I'm using pygame to try and get better with python but it just doesn't respond. I don't know why, as I have similar code that works just fine.

import pygame
import random
import time
width = 500
height = 500
snake = [[width / 2,height / 2]]
direction = "right"
pygame.init()
move_increment = 0.1
screen = pygame.display.set_mode((width,height))
running = True
pygame.display.set_caption(("Snake for Python"))
icon = pygame.image.load(("download.png"))
pygame.display.set_icon(icon)
def Keys():
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
       direction = "up"
       print("w pressed")
    if keys[pygame.K_s]:
        direction = "down"
        print("s pressed")
    if keys[pygame.K_d]:
       direction = "right"
       print("d pressed")
    if keys[pygame.K_a]:
      direction = "left"
      print("a pressed")
  
while running:
 for x in snake:
    pygame.draw.rect(screen, (255,255,255), [x[0], x[1], 15, 15])
    if direction == "up":
     x[1] -= move_increment
    if direction == "down":
     x[1]  = move_increment
    if direction == "left":
      x[0] -= move_increment
    if direction == "right":
      x[0]  = move_increment
pygame.draw.rect(screen,(0,0,0),[0,0,width,height])
pygame.display.flip()
Keys()
  
for event in pygame.event.get():
   if event.type == pygame.QUIT:
    running = False

No errors, no prompts stopping execution, this just makes NO SENSE.

CodePudding user response:

pygame.draw.rect(screen,(0,0,0),[0,0,width,height])
pygame.display.flip()
Keys()
  
for event in pygame.event.get():
   if event.type == pygame.QUIT:
    running = False

This code needs to be indented more. It's currently outside the while loop, and therefore never being run

  • Related