python - pygame crash right after taking an action -
im new @ python, problem @ code silly one. right after image of player moving, window of pygame crashing without error message @ idle.
im using python 2.7. here's code:
import pygame,sys pygame.locals import * pygame.init() dis=pygame.display.set_mode((1084,638),0,32) pygame.display.set_caption('ledders , snakes') fps=30 fpsclock=pygame.time.clock() white=(255,255,255) img=pygame.image.load('smal.gif') bg = pygame.image.load("under.gif") cax=150 cay=150 di='right' flag=true while flag: dis.blit(bg, (0, 0)) if di=='right': cax+=10 cay-=10 if cax==280: di='down' elif di=='down': cay+=10 cax+=10 if cax==410: flag=false dis.blit(img,(cax,cay)) event in pygame.event.get(): if event.type==quit: pygame.quit() sys.exit() pygame.display.update() fpsclock.tick(fps)
i through program , issue set flag = false
when if cax == 410
. why program quits, because condition becomes true after couple of seconds. there many things should consider, made changes (not in program in names):
import pygame import sys pygame.locals import * pygame.init() size = width, height = 1084, 638 # have reference width , height. screen = pygame.display.set_mode(size, 0, 32) # 'screen' convention , describes variable better 'dis'. pygame.display.set_caption('ledders , snakes') fps = 30 clock = pygame.time.clock() # use lowercase variables. white = (255, 255, 255) # use caps constants. img = pygame.surface((32, 32)) # used these 2 lines because don't have image. img.fill((0, 0, 255)) bg = pygame.surface((32, 32)) # used these 2 lines because don't have image. bg.fill((255, 0, 0)) cax = 150 # since don't know program can't tell if these describable variables or not. cay = 150 direction = 'right' # 'di' isn't describable enough. running = true # 'flag' seems indescribable variable. while running: screen.blit(bg, (0, 0)) if direction == 'right': cax += 10 cay -= 10 if cax == 280: direction = 'down' elif direction == 'down': cay += 10 cax += 10 if cax == 410: running = false screen.blit(img, (cax, cay)) event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() pygame.display.update() clock.tick(fps)
also, put spaces between operators ´==´ or after commas such ´(0, 0)´instead of (0,0). , use variable names lowercase, words separated underscore. try follow pep8, long doesn't make sense break it.
Comments
Post a Comment