pygame - Broken calling function in Python -
pygame - Broken calling function in Python -
when programing basic ufo game in python 2.7 , pygame came accross error, didn't come in consol. happend was:
i programing made little alter in code the menu screen worked fine , go play game it keeps restarting. i edited few more bits of code it'd in while loop until phone call command correctly. and still wouldent phone call command.if want have images can download them , sorcecode here: http://dl.dropbox.com/u/33487921/a%20problem.zip
here code:
import sys, pygame, random pygame.locals import * 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 in range(self.amount): if == 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 , ymove >= 1: xmove *= 3 ymove *= 3 if ymove < 1 , xmove >= 1: xmove *= 3 ymove *= 3 if ymove < 1 , xmove <1: xmove *= 3 ymove *= 3 self.array[i] = [xmove,ymove,xintercept,yintercept,false,false, type, "image"] def move(self, px, py, ix, iy): 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 , 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) , 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) , 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): in range(self.amount): self.array.append([]) check = true if self.array[i][4] == false , 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): 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): 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" 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 live def hit(self, damage=1): self.health -= harm 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 >= height - (self.iy/2): = height - self.iy elif <= self.iy/2: = 0 else: = - (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 > self.y: if my-self.y > self.maxspeed: self.y += self.maxspeed else: self.y = if < self.y: if self.y-my > self.maxspeed: self.y -= self.maxspeed else: self.y = def blit(self): screen.blit(self.image, (self.x, self.y)) def forblit(self): homecoming self.x, self.y, self. ix, self.iy def toolbar(self): pygame.draw.rect(screen, [127,127,127], pygame.rect(0, height, width, height2)) 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): homecoming 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=[] 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) 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) , mousey in range(self.y,self.y+self.size[3]+1,1): homecoming true else: homecoming 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 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 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 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): homecoming 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: homecoming 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: 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" , 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 2 bits of code have broke. (i set class abouve def's know 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 live 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 utilize code in idle know dousent phone call self.player.new() because shell dousn't print "starting" or give error me putting print alive, continuosly loops while loop.
sorry miles of code, :( helps, because without of wouldent understand it. thankyou taking time read , seek reply this. , sorry grammer , spalling mistakes, can't spell.
your issues while true in game.new method, have found other issues create game not run.
below diff 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()
python pygame
Comments
Post a Comment