Pygame Photo Printing Bug???

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

Pygame Photo Printing Bug???

Postby BlueBeachMonster » Fri Apr 04, 2014 9:38 pm

For some reason, if you collect the highest lantern first, it loads underneath the user, in all other cases the lantern loads on top.
(Arrow keys to move, to collect the other lantern one drop off the lantern on the pink square.)
Link to images needed: http://imgur.com/a/fAfQP

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

ONED = (0)
TWOD = (0)
Score = (0)
Lant1Done = (0)
Lant2Done = (0)
Lant1DoneH = (0)
Lant2DoneH = (0)
LantP = (0)

GS = (100,100,100)
HB = (255,102,255)
GY = (102,51,0)
WHITE = (255,255,255)

DISPLAYSURF = pygame.display.set_mode((260,260))
DISPLAYSURF.fill (WHITE)

PuppyImg = pygame.image.load('Puppy.png')
Puppyx = 160
Puppyy = 210

Lant1Img = pygame.image.load('Lant.png')
Lant1x = 210
Lant1y = 60

Lant2Img = pygame.image.load('Lant.png')
Lant2x = 10
Lant2y = 210


A = pygame.Rect(10,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, A)
B = pygame.Rect(60,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, B)
C = pygame.Rect(110,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, C)
D = pygame.Rect(160,10,40,40)
pygame.draw.rect(DISPLAYSURF, GS, D)
E = pygame.Rect(210,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, E)
F = pygame.Rect(10,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, F)
G = pygame.Rect(60,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, G)
H = pygame.Rect(110,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, H)
I = pygame.Rect(160,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, I)
J = pygame.Rect(210,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, J)
K = pygame.Rect(10,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, K)
L = pygame.Rect(60,110,40,40)
pygame.draw.rect(DISPLAYSURF, GS, L)
M = pygame.Rect(110,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, M)
N = pygame.Rect(160,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, N)
O = pygame.Rect(210,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, O)
P = pygame.Rect(10,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, P)
Q = pygame.Rect(60,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, Q)
R = pygame.Rect(110,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, R)
S = pygame.Rect(160,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, S)
T = pygame.Rect(210,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, T)
U = pygame.Rect(10,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, U)
V = pygame.Rect(60,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, V)
W = pygame.Rect(110,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, W)
X = pygame.Rect(160,210,40,40)
pygame.draw.rect(DISPLAYSURF, HB, X)
Y = pygame.Rect(210,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, Y)







pygame.display.set_caption('Halloween Horror')






while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                rightPressed = True
            if event.key == K_LEFT:
                leftPressed = True
            if event.key == K_UP:
                upPressed = True
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                if Puppyx < 200:
                    Puppyx+=50
                    print ("x = ", Puppyx)
                    rightPressed = False
                if Puppyx == 210:
                    if event.key == K_RIGHT:
                        print ('Invalid Input')
            if event.key == K_LEFT:
                leftPressed = False
                if Puppyx > 10:
                    Puppyx-=50
                    print ("x = ", Puppyx)
                if Puppyx == 10:
                    if event.key == K_LEFT:
                        print ('Invalid Input')
            if event.key == K_UP:
                if Puppyy > 10:
                    Puppyy-=50
                    print ("y = ", Puppyy)
                    upPressed = False
                if Puppyy == 10:
                    if event.key == K_UP:
                        print ('Invalid Input')
            elif event.key == K_DOWN:
                if Puppyy < 200:
                    Puppyy+=50
                    print ("y = ", Puppyy)
                    downPressed = False
                if Puppyy == 210:
                    if event.key == K_DOWN:
                        print ('Invalid Input')

    if LantP == (0):
        if Lant1Done == 0:
            if Puppyx == 210:
                if Puppyy == 60:
                    Lant1Done = (1)
                    LantP = (1)
        if Lant2Done == 0:
            if Puppyx == 10:
                if Puppyy == 210:
                    Lant2Done = (1)
                    LantP = (1)











    DISPLAYSURF.fill (WHITE)
    pygame.draw.rect(DISPLAYSURF, GY, A)
    pygame.draw.rect(DISPLAYSURF, GY, B)
    pygame.draw.rect(DISPLAYSURF, GY, C)
    pygame.draw.rect(DISPLAYSURF, GS, D)
    pygame.draw.rect(DISPLAYSURF, GY, E)
    pygame.draw.rect(DISPLAYSURF, GY, F)
    pygame.draw.rect(DISPLAYSURF, GY, G)
    pygame.draw.rect(DISPLAYSURF, GY, H)
    pygame.draw.rect(DISPLAYSURF, GY, I)
    pygame.draw.rect(DISPLAYSURF, GY, J)
    pygame.draw.rect(DISPLAYSURF, GY, K)
    pygame.draw.rect(DISPLAYSURF, GS, L)
    pygame.draw.rect(DISPLAYSURF, GY, M)
    pygame.draw.rect(DISPLAYSURF, GY, N)
    pygame.draw.rect(DISPLAYSURF, GY, O)
    pygame.draw.rect(DISPLAYSURF, GY, P)
    pygame.draw.rect(DISPLAYSURF, GY, Q)
    pygame.draw.rect(DISPLAYSURF, GY, R)
    pygame.draw.rect(DISPLAYSURF, GY, S)
    pygame.draw.rect(DISPLAYSURF, GY, T)
    pygame.draw.rect(DISPLAYSURF, GY, U)
    pygame.draw.rect(DISPLAYSURF, GY, V)
    pygame.draw.rect(DISPLAYSURF, GY, W)
    pygame.draw.rect(DISPLAYSURF, HB, X)
    pygame.draw.rect(DISPLAYSURF, GY, Y)
    DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))

    if Puppyx == 160:
        if Puppyy == 210:
            if Lant1Done == 1:
                Lant1DoneH = (1)
                if ONED == (0):
                    Score = (Score+1)
                    print ('Your Score so Far is',Score)
                    ONED = (1)
                    LantP = (0)
    if Puppyx == 160:
        if Puppyy == 210:
            if Lant2Done == 1:
                Lant2DoneH = (1)
                if TWOD == (0):
                    Score = (Score+1)
                    print ('Your Score so Far is',Score)
                    TWOD = (1)
                    LantP = (0)
    if Lant1DoneH == (0):
        if Lant1Done == 0:
            DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))
            DISPLAYSURF.blit(Lant1Img, (Lant1x,Lant1y))
        if Lant1Done == 1:
            DISPLAYSURF.blit(Lant1Img, (Puppyx,Puppyy))
    if Lant2DoneH == (0):
        if Lant2Done == 0:
            DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))
            DISPLAYSURF.blit(Lant2Img, (Lant2x,Lant2y))
        if Lant2Done == 1:
            DISPLAYSURF.blit(Lant2Img, (Puppyx,Puppyy))
   
    pygame.display.update()
#HELP MEH
Last edited by micseydel on Fri Apr 04, 2014 10:26 pm, edited 1 time in total.
Reason: Locked OP, code tags.
BlueBeachMonster
 
Posts: 5
Joined: Fri Apr 04, 2014 8:33 pm

Re: Pygame Photo Printing Bug???

Postby metulburr » Fri Apr 04, 2014 11:09 pm

You really need to learn classes, if not, at least functions. The way it is currently written is rediculous to debug.

It also looks like you try to break every single PEP guideline also. Magic numbers, massive if structures, improper exiting, tabbed indentation, capitalized variables, star imports, unnecessary if comparisons:
Code: Select all
if Lant2Done == 0:

would be
Code: Select all
if not Lant2Done:

etc.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1415
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Pygame Photo Printing Bug???

Postby Mekire » Sat Apr 05, 2014 12:26 pm

I'm going to agree with Mettulburr and say, yes, that code is ruthlessly broken.

But here is your fix. You always draw the dog, so always draw it first:
Code: Select all
import pygame, sys
from pygame.locals import *
pygame.init()

ONED = (0)
TWOD = (0)
Score = (0)
Lant1Done = (0)
Lant2Done = (0)
Lant1DoneH = (0)
Lant2DoneH = (0)
LantP = (0)

GS = (100,100,100)
HB = (255,102,255)
GY = (102,51,0)
WHITE = (255,255,255)

DISPLAYSURF = pygame.display.set_mode((260,260))
DISPLAYSURF.fill (WHITE)
clock = pygame.time.Clock()

PuppyImg = pygame.image.load('Puppy.png')
Puppyx = 160
Puppyy = 210

Lant1Img = pygame.image.load('Lant.png')
Lant1x = 210
Lant1y = 60

Lant2Img = pygame.image.load('Lant.png')
Lant2x = 10
Lant2y = 210


A = pygame.Rect(10,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, A)
B = pygame.Rect(60,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, B)
C = pygame.Rect(110,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, C)
D = pygame.Rect(160,10,40,40)
pygame.draw.rect(DISPLAYSURF, GS, D)
E = pygame.Rect(210,10,40,40)
pygame.draw.rect(DISPLAYSURF, GY, E)
F = pygame.Rect(10,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, F)
G = pygame.Rect(60,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, G)
H = pygame.Rect(110,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, H)
I = pygame.Rect(160,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, I)
J = pygame.Rect(210,60,40,40)
pygame.draw.rect(DISPLAYSURF, GY, J)
K = pygame.Rect(10,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, K)
L = pygame.Rect(60,110,40,40)
pygame.draw.rect(DISPLAYSURF, GS, L)
M = pygame.Rect(110,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, M)
N = pygame.Rect(160,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, N)
O = pygame.Rect(210,110,40,40)
pygame.draw.rect(DISPLAYSURF, GY, O)
P = pygame.Rect(10,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, P)
Q = pygame.Rect(60,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, Q)
R = pygame.Rect(110,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, R)
S = pygame.Rect(160,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, S)
T = pygame.Rect(210,160,40,40)
pygame.draw.rect(DISPLAYSURF, GY, T)
U = pygame.Rect(10,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, U)
V = pygame.Rect(60,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, V)
W = pygame.Rect(110,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, W)
X = pygame.Rect(160,210,40,40)
pygame.draw.rect(DISPLAYSURF, HB, X)
Y = pygame.Rect(210,210,40,40)
pygame.draw.rect(DISPLAYSURF, GY, Y)







pygame.display.set_caption('Halloween Horror')






while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                rightPressed = True
            if event.key == K_LEFT:
                leftPressed = True
            if event.key == K_UP:
                upPressed = True
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                if Puppyx < 200:
                    Puppyx+=50
                    print ("x = ", Puppyx)
                    rightPressed = False
                if Puppyx == 210:
                    if event.key == K_RIGHT:
                        print ('Invalid Input')
            if event.key == K_LEFT:
                leftPressed = False
                if Puppyx > 10:
                    Puppyx-=50
                    print ("x = ", Puppyx)
                if Puppyx == 10:
                    if event.key == K_LEFT:
                        print ('Invalid Input')
            if event.key == K_UP:
                if Puppyy > 10:
                    Puppyy-=50
                    print ("y = ", Puppyy)
                    upPressed = False
                if Puppyy == 10:
                    if event.key == K_UP:
                        print ('Invalid Input')
            elif event.key == K_DOWN:
                if Puppyy < 200:
                    Puppyy+=50
                    print ("y = ", Puppyy)
                    downPressed = False
                if Puppyy == 210:
                    if event.key == K_DOWN:
                        print ('Invalid Input')

    if LantP == (0):
        if Lant1Done == 0:
            if Puppyx == 210:
                if Puppyy == 60:
                    Lant1Done = (1)
                    LantP = (1)
        if Lant2Done == 0:
            if Puppyx == 10:
                if Puppyy == 210:
                    Lant2Done = (1)
                    LantP = (1)











    DISPLAYSURF.fill (WHITE)
    pygame.draw.rect(DISPLAYSURF, GY, A)
    pygame.draw.rect(DISPLAYSURF, GY, B)
    pygame.draw.rect(DISPLAYSURF, GY, C)
    pygame.draw.rect(DISPLAYSURF, GS, D)
    pygame.draw.rect(DISPLAYSURF, GY, E)
    pygame.draw.rect(DISPLAYSURF, GY, F)
    pygame.draw.rect(DISPLAYSURF, GY, G)
    pygame.draw.rect(DISPLAYSURF, GY, H)
    pygame.draw.rect(DISPLAYSURF, GY, I)
    pygame.draw.rect(DISPLAYSURF, GY, J)
    pygame.draw.rect(DISPLAYSURF, GY, K)
    pygame.draw.rect(DISPLAYSURF, GS, L)
    pygame.draw.rect(DISPLAYSURF, GY, M)
    pygame.draw.rect(DISPLAYSURF, GY, N)
    pygame.draw.rect(DISPLAYSURF, GY, O)
    pygame.draw.rect(DISPLAYSURF, GY, P)
    pygame.draw.rect(DISPLAYSURF, GY, Q)
    pygame.draw.rect(DISPLAYSURF, GY, R)
    pygame.draw.rect(DISPLAYSURF, GY, S)
    pygame.draw.rect(DISPLAYSURF, GY, T)
    pygame.draw.rect(DISPLAYSURF, GY, U)
    pygame.draw.rect(DISPLAYSURF, GY, V)
    pygame.draw.rect(DISPLAYSURF, GY, W)
    pygame.draw.rect(DISPLAYSURF, HB, X)
    pygame.draw.rect(DISPLAYSURF, GY, Y)
    DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))

    if Puppyx == 160:
        if Puppyy == 210:
            if Lant1Done == 1:
                Lant1DoneH = (1)
                if ONED == (0):
                    Score = (Score+1)
                    print ('Your Score so Far is',Score)
                    ONED = (1)
                    LantP = (0)
    if Puppyx == 160:
        if Puppyy == 210:
            if Lant2Done == 1:
                Lant2DoneH = (1)
                if TWOD == (0):
                    Score = (Score+1)
                    print ('Your Score so Far is',Score)
                    TWOD = (1)
                    LantP = (0)
    DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))
    if not Lant1DoneH:
        pos = (Puppyx,Puppyy) if Lant1Done else (Lant1x,Lant1y)
        DISPLAYSURF.blit(Lant1Img, pos)
    if not Lant2DoneH:
        pos = (Puppyx,Puppyy) if Lant2Done else (Lant2x,Lant2y)
        DISPLAYSURF.blit(Lant2Img, pos)

    pygame.display.update()
    clock.tick(60)
#HELP MEH

This is the portion that is changed:
Code: Select all
DISPLAYSURF.blit(PuppyImg, (Puppyx,Puppyy))
if not Lant1DoneH:
    pos = (Puppyx,Puppyy) if Lant1Done else (Lant1x,Lant1y)
    DISPLAYSURF.blit(Lant1Img, pos)
if not Lant2DoneH:
    pos = (Puppyx,Puppyy) if Lant2Done else (Lant2x,Lant2y)
    DISPLAYSURF.blit(Lant2Img, pos)

Also added a clock.
-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: 7stud, Baldyr, Google [Bot] and 2 guests