Anything wrong with this code?

This is the place for queries that don't fit in any of the other categories.

Anything wrong with this code?

Postby Hissing Cockroach » Fri Sep 20, 2013 10:34 pm

I just started with pygame and
finally made a code. all it does is pop up the command line
and immediatly disappear. I don't see anything
wrong with it. Does anyone else?

here is my code:

Code: Select all
import pygame, sys
from pygame.locals import *

pygame.init()


DSPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)


RED = (255, 0, 0)
BLUE = (0, 0, 255)

DISPLAYSURF.fill(RED)
pygame.draw.polygon(DISPLAYSURF, GREEN,((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

pixObj = pygame.PixelArray(DISPLAYSURF)
pixObj[480][380] = BLACK
pixObj[482][382] = BLACK
pixObj[484][384] = BLACK
pixObj[486][386] = BLACK
pixObj[488][388] = BLACK
del pixObj


while True:
    for event in pygame.event.get()
    if event.type == QUIT:
        pygame.quit()
        sys.exit
        pygame.display.update()
Last edited by Hissing Cockroach on Sat Sep 21, 2013 5:11 am, edited 1 time in total.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Anything wrong with this code?

Postby micseydel » Fri Sep 20, 2013 10:45 pm

Run your code in a terminal to get a traceback, and if you still have questions have that, post the entire traceback here verbatim and in code tags.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Anything wrong with this code?

Postby metulburr » Fri Sep 20, 2013 11:00 pm

There are so many errors here...from indentation, missing colon, mispelling a var (typo), multiple undefined variables.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1130
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Anything wrong with this code?

Postby dboxall123 » Fri Sep 20, 2013 11:07 pm

The first things that I notice is are
Code: Select all
while True:
    for event in pygame.event.get()
    if event.type == QUIT:

should be:
Code: Select all
while True:
    for event in pygame.event.get():
        if event.type == QUIT:

There is no colon after the for loop and the if statement is not indented. There is also a spelling mistake
Code: Select all
DSPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)

and GREEN and BLACK are not yet defined. Fix these and it should run. :D
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Anything wrong with this code?

Postby Hissing Cockroach » Sat Sep 21, 2013 5:18 am

It's not running.
Last edited by Hissing Cockroach on Sat Sep 21, 2013 3:23 pm, edited 2 times in total.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Anything wrong with this code?

Postby micseydel » Sat Sep 21, 2013 6:23 am

This is a technical forum. "It's not running" is nowhere near detailed enough. What are you doing, what is happening, and what do you expect should be happening?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 941
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Anything wrong with this code?

Postby dboxall123 » Sat Sep 21, 2013 2:16 pm

Ok, this will run

Code: Select all
import pygame, sys

pygame.init()

DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)

RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0,255,0)
BLACK = (0,0,0)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    DISPLAYSURF.fill(RED)
    pygame.draw.polygon(DISPLAYSURF, GREEN,((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

    pixObj = pygame.PixelArray(DISPLAYSURF)
    pixObj[480][380] = BLACK
    pixObj[482][382] = BLACK
    pixObj[484][384] = BLACK
    pixObj[486][386] = BLACK
    pixObj[488][388] = BLACK
    del pixObj

    pygame.display.update()

pygame.quit()

I'm assuming you just had a black screen yeh? All drawing to the screen has to be done inside the while loop, otherwise it draws it once and then disappears (far to quick for you to see). I agree with micseydel, you really do need to include more details in your posts. Also, just as a friendly suggestion, might I suggest that you learn the Python basics before learning Pygame? Believe me, I jumped straight into trying to use pygame before I understood the even the basics of python and object oriented programming, and I failed. It wasn't until I done an actual Python tutorial that I started to understand exacly what the hell I was doing. There is an excellent tutorial here http://www.greenteapress.com/thinkpython/ Good luck :)
Last edited by dboxall123 on Sun Sep 22, 2013 1:13 am, edited 1 time in total.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Anything wrong with this code?

Postby Hissing Cockroach » Sat Sep 21, 2013 3:41 pm

Well, I tried this but, instead of one
black screen popping up, it was two.

Sorry about my last comment. I was
in a hurry when I did that.
Anyway, thanks for the website!
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Anything wrong with this code?

Postby dboxall123 » Sat Sep 21, 2013 3:55 pm

Ha, my bad. The reason this isn't working is because I have removed the
Code: Select all
from pygame.locals.import *
statement. This floods the namespace, and is not good. To rectify this, go to your event loop and change
Code: Select all
if event.type == QUIT
to
Code: Select all
if event.type == pygame.QUIT:
. That will (probably) work.
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Anything wrong with this code?

Postby Hissing Cockroach » Sun Sep 22, 2013 1:01 am

I don't know why it's not working. It's simply
bringing up the command line and disappearing again.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Anything wrong with this code?

Postby dboxall123 » Sun Sep 22, 2013 1:11 am

Hmmm. What are you running this program in? Run it in IDLE and then post the traceback message. This is currently working on my computer:
Code: Select all
import pygame, sys

pygame.init()

DISPLAYSURF = pygame.display.set_mode((500, 400), 0, 32)

RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0,255,0)
BLACK = (0,0,0)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    DISPLAYSURF.fill(RED)
    pygame.draw.polygon(DISPLAYSURF, GREEN,((146, 0), (291, 106), (236, 277), (56, 277), (0, 106)))

    pixObj = pygame.PixelArray(DISPLAYSURF)
    pixObj[480][380] = BLACK
    pixObj[482][382] = BLACK
    pixObj[484][384] = BLACK
    pixObj[486][386] = BLACK
    pixObj[488][388] = BLACK
   

    pygame.display.update()

pygame.quit()
Do you definitely have pygame properly installed? Open up idle, and in the shell type
Code: Select all
import pygame
. See if you get an error
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Anything wrong with this code?

Postby Hissing Cockroach » Sun Sep 22, 2013 2:23 pm

Well, I didn't get an error.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Anything wrong with this code?

Postby Hissing Cockroach » Sun Sep 22, 2013 2:28 pm

Alright, I found an answer.
It works with no

Code: Select all
from pygame.locals.import  *


so thanks!
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat and 1 guest