user1078204
user1078204

Reputation:

Broken calling function in Python

When I was programing a basic UFO game in python 2.7 and with pygame I came accross an error, However it didn't come up in the consol. What happend was:

  1. I was programing
  2. Made a little change in the code
  3. The Menu Screen worked fine and I go to play the game
  4. It then keeps restarting.
  5. I edited a few more bits of code so it'd be in a while loop until it would call a command correctly.
  6. And It still wouldent call the command.

If you want to have the Images you can download them and the Sorcecode here: http://dl.dropbox.com/u/33487921/A%20Problem.zip

Here is the code:

import sys, pygame, random
from pygame.locals import *
from time import gmtime, strftime
pygame.init()

width = int(500*2)
height = int(300*2)
width2 = width + (width/5)
height2 = height + ((height/3)/2)
screen = pygame.display.set_mode((width, height2))
pygame.display.set_caption("MoeTM's UFO Game")

class FlyingObject():
    def Restart(self):
        self.amount = 0
        self.array = []
    def New(self, FPS):
        self.amount += 1
        for i in range(self.amount):
            if i == self.amount-1:
                self.array.append([])
                Strength = random.randrange(1, 101, 1)
                if Strength in range(1, 51, 1):
                    Type = 1
                if Strength in range(51, 76, 1):
                    Type = 2
                if Strength in range(76, 88, 1):
                    Type = 3
                if Strength in range(88, 94, 1):
                    Type = 4
                if Strength in range(94, 101, 1):
                    Type = 5
                X = random.randrange(0,1,1)
                if X == 0:
                    XMove = random.randrange(1,6,1)
                if X == 1:
                    XMove = random.randrange(1,6,1)
                    XMove /= 5.0
                Y = random.randrange(0,2,1)
                if Y == 0:
                    YMove = random.randrange(1,6,1)
                if Y == 1:
                    YMove = random.randrange(1,6,1)
                    YMove /= 5.0
                XMove *= 10
                YMove *= 10
                XMove *= FPS
                YMove *= FPS
                TLBR = random.randrange(0,4,1)
                if TLBR == 0:#top
                    XIntercept = random.randrange(0,width+1,1)
                    YIntercept = -5
                if TLBR == 1:#left
                    XIntercept = -5
                    YIntercept = random.randrange(0,height+1,1)
                if TLBR == 2:#bottom
                    XIntercept = random.randrange(0,width+1,1)
                    YIntercept = height + 5
                if TLBR == 3:#right
                    XIntercept = width + 5
                    YIntercept = random.randrange(0,height+1,1)
                if XIntercept in range((width/4)*3,width+1,1):
                    XMove = -XMove
                if YIntercept in range((height/4)*3,height+1,1):
                    YMove = -YMove
                if XIntercept in range((width/4),(width/4)*3, 1):
                    chance = random.randrange(0,2,1)
                    if chance == 1:
                        XMove=-XMove
                if YIntercept in range((height/4),(height/4)*3, 1):
                    chance = random.randrange(0,2,1)
                    if chance == 1:
                        YMove=-YMove
                if XMove < 1 and YMove >= 1:
                    XMove *= 3
                    YMove *= 3
                if YMove < 1 and XMove >= 1:
                    XMove *= 3
                    YMove *= 3
                if YMove < 1 and XMove <1:
                    XMove *= 3
                    YMove *= 3
                self.array[i] = [XMove,YMove,XIntercept,YIntercept,False,False, Type, "image"]
    def Move(self, PX, PY, IX, IY):
        for i in range(self.amount):
            self.array.append([])
            if self.array[i][2] > width +10 or self.array[i][2] < -10:
                self.array[i][4] = False
                self.array[i][5] = True
            if self.array[i][3] > height +10 or self.array[i][3] < -10:
                self.array[i][4] = False
                self.array[i][5] = True
            Check = True
            if self.array[i][4] == False and self.array[i][5] == True:
                Check = False
            if Check == True:
                self.array[i][2] += self.array[i][0]#XIntercept+=XMove
                self.array[i][3] += self.array[i][1]#YIntercept+=Ymove
                if self.array[i][2] in range(0,width+1,1) and self.array[i][3] in range(0,height+1,1):
                    self.array[i][4] = True
                    self.array[i][5] = True
                else:
                    self.array[i][4] = False
                if int(self.array[i][2]+5) in range(PX, PX+IX, 1) and int(self.array[i][3]+5) in range(PY, PY+IY, 1):
                    self.colide(self.array[i][6])
                    self.array[i][4] = False
                    self.array[i][5] = True
    def Blit(self):
        for i in range(self.amount):
            self.array.append([])
            Check = True
            if self.array[i][4] == False and self.array[i][5] == True:
                Check = False
            if Check == True:
                screen.blit(self.array[i][7], (int(self.array[i][2]), int(self.array[i][3])))
class Bullits(FlyingObject):
    def colide(self, damage=1):
        Play.Game.Player.Hit(damage)
    def Convert(self):
        for i in range(self.amount):
            self.array.append([])
            self.array[i][7]=self.Image = pygame.image.load("images\Bullit"+str(self.array[i][6])+".png")
class Orbs(FlyingObject):
    def colide(self, ammount=1):
        Play.Game.Player.GotPoint(ammount)
    def Convert(self):
        for i in range(self.amount):
            self.array.append([])
            self.array[i][7]=self.Image = pygame.image.load("images\Orb"+str(self.array[i][6])+".png")
class UFO():
    def __init__(self):
        self.health = 10
        self.maxhealth = 50
        self.startinghealth = 10
        self.maxspeed = 20
        self.Image = pygame.image.load("images\UFO.png")
        self.Heart0 = pygame.transform.scale(pygame.image.load("images\Heart0.png"), (width/50,height/30))
        self.Heart1 = pygame.transform.scale(pygame.image.load("images\Heart1.png"), (width/50,height/30))
        self.Heart2 = pygame.transform.scale(pygame.image.load("images\Heart2.png"), (width/50,height/30))
        self.Heart3 = pygame.transform.scale(pygame.image.load("images\Heart3.png"), (width/50,height/30))
        self.Heart4 = pygame.transform.scale(pygame.image.load("images\Heart4.png"), (width/50,height/30))
        self.Heart5 =pygame.transform.scale( pygame.image.load("images\Heart5.png"), (width/50,height/30))

        self.ix = 100
        self.iy = 40
        self.points = 0
        self.collection = 0
        self.x = (width-self.ix)/2
        self.y = (height-self.iy)/2
        self.HeartCol= []
        self.DA = "New"
        for i in range(0,10,1):
            self.HeartCol.append([])
            dif = int((width/2.5)/10)
            higt = int(((height2-height)/2)+height)
            self.HeartCol[i] = [dif*i, higt]
    def New(self):
        print "Starting"
        self.health = self.startinghealth
        self.x = (width-self.ix)/2
        self.y = (height-self.iy)/2
        self.DA = "Alive"
        print Alive
    def Hit(self, damage=1):
        self.health -= damage
        if self.health <= 0:
            self.DA = "Dead"
    def GotPoint(self, amount=1):
        self.points += amount
        if self.points >= 10:
            self.points -= 10
            self.collection += 1
            self.health += 1
            if self.health >= self.maxhealth:
                self.health = self.maxhealth
    def Move(self,mx,my):
        if mx >= width - (self.ix/2):
            mx = width - self.ix
        elif mx <= self.ix/2:
            mx = 0
        else:
            mx = mx - (self.ix/2)
        if my >= height - (self.iy/2):
            my = height - self.iy
        elif my <= self.iy/2:
            my = 0
        else:
            my = my - (self.iy/2)
        if mx > self.x:
            if mx-self.x > self.maxspeed:
                self.x += self.maxspeed
            else:
                self.x = mx
        if mx < self.x:
            if self.x-mx > self.maxspeed:
                self.x -= self.maxspeed
            else:
                self.x = mx
        if my > self.y:
            if my-self.y > self.maxspeed:
                self.y += self.maxspeed
            else:
                self.y = my
        if my < self.y:
            if self.y-my > self.maxspeed:
                self.y -= self.maxspeed
            else:
                self.y = my
    def Blit(self):
        screen.blit(self.Image, (self.x, self.y))
    def ForBlit(self):
        return self.x, self.y, self. ix, self.iy
    def Toolbar(self):
        pygame.draw.rect(screen, [127,127,127], pygame.Rect(0, height, width, height2))
        for i in range(0,10,1):
            if self.health >= 41+i:
                screen.blit(self.Heart5, (self.HeartCol[i][0], self.HeartCol[i][1]))
            elif self.health >= 31+i:
                screen.blit(self.Heart4, (self.HeartCol[i][0], self.HeartCol[i][1]))
            elif self.health >= 21+i:
                screen.blit(self.Heart3, (self.HeartCol[i][0], self.HeartCol[i][1]))
            elif self.health >= 11+i:
                screen.blit(self.Heart2, (self.HeartCol[i][0], self.HeartCol[i][1]))
            elif self.health >= 1+i:
                screen.blit(self.Heart1, (self.HeartCol[i][0], self.HeartCol[i][1]))
            else:
                screen.blit(self.Heart0, (self.HeartCol[i][0], self.HeartCol[i][1]))
    def DOA(self):
        return self.DA
    def New(self):
        pass
class Background():
    def __init__(self):
        self.sr = 5
        self.bg = [0,0,0]
    def New(self):
        self.amountOfStars = random.randrange(10,51,1)
        self.stars=[]
        for i in range(self.amountOfStars):
            x = random.randrange(self.sr*2,width-(self.sr*2)+1,1)
            y = random.randrange(self.sr*2,height-(self.sr*2)+1,1)
            coulerr = random.randrange(200,256,1)
            coulerg = random.randrange(200,256,1)
            coulerb = random.randrange(0,56,1)
            size = random.randrange(5, 10, 1)
            self.stars.append([])
            self.stars[i] = [[x,y],[coulerr,coulerg,coulerb],size]
    def Blit(self):
        screen.fill(self.bg)
        for i in range(self.amountOfStars):
            self.stars.append([])
            pygame.draw.circle(screen,self.stars[i][1],self.stars[i][0],self.stars[i][2])

class Text():
    def Text(self, Text, TextSize, Couler):
        self.Couler = Couler
        self.TextSize = int(TextSize)
        self.Text = str(Text)
        self.font = pygame.font.Font(None, self.TextSize)
        self.text = self.font.render(self.Text, 1, self.Couler)
        self.Size = self.text.get_rect()
        self.ctext = self.text
        self.cCouler = self.Couler
    def Position(self, X, Y):
        self.X = X
        self.Y = Y
        self.Position = self.X,self.Y
    def Blit(self):
        screen.blit(self.ctext, [self.X,self.Y])
    def IsItClicked(self, MouseX, MouseY):
        if MouseX in range(self.X,self.X+self.Size[2]+1,1) and MouseY in range(self.Y,self.Y+self.Size[3]+1,1):
            return True
        else:
            return False
    def Hover(self, MouseX=0, MouseY=0, couler=-1):
        if couler == -1:
            couler = self.Couler
        hover = self.IsItClicked(MouseX, MouseY)
        if hover == True:
            self.cCouler = couler
            self.ctext = self.font.render(self.Text, 1, self.cCouler)
        else:
            self.cCouler = self.Couler
            self.ctext = self.font.render(self.Text, 1, self.cCouler)


class Menu():
    def __init__(self):
        self.Move = (height2/4)/2
        self.move = self.Move*2
        self.Menu = Text()
        self.Menu.Text("MoeTM's UFO Game", 50, [255,255,0])
        self.Menu.Position((width - self.Menu.Size[2])/2,self.Menu.Size[0])
        self.Play = Text()
        self.Play.Text("Play The Game!", 30, [255,255,0])
        self.Play.Position(self.Play.Size[0]+20,self.Play.Size[0]+self.Move)
        self.Shop = Text()
        self.Shop.Text("Enter the Shop, Why?", 30, [255,255,0])
        self.Shop.Position(self.Shop.Size[0]+20,self.Shop.Size[0]+self.move*2-self.Move)
        self.Saves = Text()
        self.Saves.Text("Save the game, ;P", 30, [255,255,0])
        self.Saves.Position(self.Saves.Size[0]+20,self.Saves.Size[0]+self.move*3-self.Move)
        self.Options = Text()
        self.Options.Text("Options...", 30, [255,255,0])
        self.Options.Position(self.Options.Size[0]+20,self.Options.Size[0]+self.move*4-self.Move)

        self.Area = "Menu"
    def Blit(self, MouseX=0, MouseY=0):
        if self.Area == "Menu":
            self.Play.Hover(MouseX, MouseY, [192,255,0])
            self.Shop.Hover(MouseX, MouseY, [192,255,0])
            self.Saves.Hover(MouseX, MouseY, [192,255,0])
            self.Options.Hover(MouseX, MouseY, [192,255,0])
            self.Play.Blit()
            self.Shop.Blit()
            self.Saves.Blit()
            self.Options.Blit()
        if self.Area == "Shop":
            pass
        if self.Area == "Saves":
            pass
        if self.Area == "Options":
            pass
        if self.Area != "Play":
            self.Menu.Hover(MouseX, MouseY, [192,255,0])
            self.Menu.Blit()
    def IsClicked(self, MouseX, MouseY):
        if self.Area == "Menu":
            play = self.Play.IsItClicked(MouseX, MouseY)
            if play == True:
                self.Area = "Play"
            Shop = self.Shop.IsItClicked(MouseX, MouseY)
            if Shop == True:
                self.Area = "Shop"
            Saves = self.Saves.IsItClicked(MouseX, MouseY)
            if Saves == True:
                self.Area = "Saves"
            Options = self.Options.IsItClicked(MouseX, MouseY)
            if Options == True:
                self.Area = "Options"
        menu = self.Menu.IsItClicked(MouseX, MouseY)
        if menu == True:
            self.Area = "Menu"
    def getArea(self):
        return self.Area
class Game():
    def __init__(self):
        self.Player = UFO()
        self.Background = Background()
        self.Orbs = Orbs()
        self.Bullits = Bullits()
        self.Death = pygame.image.load("images\Death.png")
        self.Death = pygame.transform.scale(self.Death, (width2, height2))
        self.death = 0
    def New(self):
        while True:
            self.Player.New()
            Place = self.Player.DOA()
            if Place != "New":
                self.TotalTime = 0
                self.Background.New()
                self.Bullits.Restart()
                self.Orbs.Restart()
    def Blit(self):
        self.Bullits.Convert()
        self.Orbs.Convert()
        self.Background.Blit()
        self.Bullits.Blit()
        self.Orbs.Blit()
        self.Player.Blit()
    def Move(self, MX, MY):
        self.Player.Move(MX, MY)
        PX, PY, IX, IY = self.Player.ForBlit()
        self.Bullits.Move(PX, PY, IX, IY)
        self.Orbs.Move(PX, PY, IX, IY)
    def Updater(self, time):
        self.TotalTime += time
    def Death(self):
        if self.death >= 1: return 0
        self.death += 1
        time = float(self.TotalTime) * 10
        time = int(time)
        time /= 10.0
        time = str(time)
        self.DeadText = Text()
        text = "You died in "+time+" seconds"
        self.DeadText.Text(text,50,[0,0,0])
        self.DeadText.Position((width-self.DeadText.Size[2])/2,(height2-self.DeadText.Size[3])/2)
    def DeadBlit(self):
        screen.blit(self.Death, (0, 0))
        self.DeadText.Blit()
class Main():
    def __init__(self):
        self.Game = Game()
        self.Menu = Menu()
        self.DA= ""
        self.seconds = 0
        self.clock = pygame.time.Clock()
        self.FPS = 0.1
        self.QUIT = False
        self.Place = ["","New"]
        self.difficalty1 = 10
        self.difficalty2 = 5
    def Main(self):
        while True:
            for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        self.QUIT = True
                    if event.type == KEYDOWN:
                        if event.key == K_p:
                            time = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
                            pygame.image.save(screen, time+".png")
                        #if event.key == K_DOWN:

                        #if event.key == K_UP:
                    if event.type == MOUSEBUTTONDOWN:
                        self.Menu.IsClicked(MouseX, MouseY)
            if self.QUIT == True:
                    pygame.quit()
                    break
            self.seconds += self.clock.tick()/1000.0
            if self.seconds >= self.FPS:
                MouseX,MouseY = pygame.mouse.get_pos()
                self.Place[0] = self.Menu.getArea()
                if self.Place[0] == "Play" and self.Place[1] == "New":
                    self.Game.New()
                screen.fill([0,0,0])
                if self.Place[0] != "Play":
                    self.Menu.Blit(MouseX, MouseY)
                if self.Place[0] == "Play":
                    if self.Place[1] != "Dead":
                        self.Game.Move(MouseX,MouseY)
                        self.Game.Updater(self.seconds)

                        randomChance = random.randrange(0,self.difficalty1,1)
                        if randomChance in range(0, self.difficalty2, 1):
                            self.Game.Orbs.New(self.FPS)
                        randomChance = random.randrange(0,self.difficalty1,1)
                        if randomChance in range(0, self.difficalty2, 1):
                            self.Game.Bullits.New(self.FPS)

                    self.Game.Blit()
                    self.Place[1] = self.Game.Player.DOA()
                    if self.Place[1] == "Dead":
                        self.Game.Death()
                        self.Game.DeadBlit()
                pygame.display.flip()
                self.seconds -= self.FPS


Play = Main()
Play.Main()
pygame.quit()

here is the two bits of code that have broke. (I put the class abouve the def's so you know what class there in)

class UFO():
    def New(self):
        print "Starting"
        self.health = self.startinghealth
        self.x = (width-self.ix)/2
        self.y = (height-self.iy)/2
        self.DA = "Alive"
        print Alive
class Game():
    def New(self):
        while True:
            self.Player.New()
            Place = self.Player.DOA()
            if Place != "New":
                self.TotalTime = 0
                self.Background.New()
                self.Bullits.Restart()
                self.Orbs.Restart()

if you use the code in IDLE then you will know that it dousent even call self.Player.New() because the Shell dousn't print "Starting" or give an error for me putting print Alive, it just continuosly loops the while loop.

Sorry for the Miles of code, :( But Hopefully it helps, mostly because without some of it you wouldent understand it. Also Thankyou for taking the time to read and try to answer this. And Sorry for all the grammer and spalling mistakes, I can't spell.

Upvotes: 2

Views: 309

Answers (1)

Lucas S.
Lucas S.

Reputation: 13541

Your issues is the While True in your Game.New method, also i have found other issues that make the game to not run.

Below is the diff with the fixed code:

--- ../A Problem-1/Game.pyw 2012-01-25 13:32:00.000000000 -0300
+++ Game.pyw    2012-01-25 11:55:56.000000000 -0300
@@ -11,6 +11,9 @@
 pygame.display.set_caption("MoeTM's UFO Game")

 class FlyingObject():
+    def __init__(self):
+        self.amount = 0
+        self.array = []
     def Restart(self):
         self.amount = 0
         self.array = []
@@ -233,6 +236,7 @@
     def __init__(self):
         self.sr = 5
         self.bg = [0,0,0]
+        self.amountOfStars = 0
     def New(self):
         self.amountOfStars = random.randrange(10,51,1)
         self.stars=[]
@@ -352,15 +356,15 @@
         self.Death = pygame.image.load("images\Death.png")
         self.Death = pygame.transform.scale(self.Death, (width2, height2))
         self.death = 0
+        self.TotalTime = 0
     def New(self):
-        while True:
-            self.Player.New()
-            Place = self.Player.DOA()
-            if Place != "New":
-                self.TotalTime = 0
-                self.Background.New()
-                self.Bullits.Restart()
-                self.Orbs.Restart()
+        self.Player.New()
+        Place = self.Player.DOA()
+        if Place != "New":
+            self.TotalTime = 0
+            self.Background.New()
+            self.Bullits.Restart()
+            self.Orbs.Restart()
     def Blit(self):
         self.Bullits.Convert()
         self.Orbs.Convert()

Upvotes: 1

Related Questions