python - I'm creating Space Invaders but it's not working like it should -


for programming class i'm working on space invaders game... code isn't perfect it's working. mostly...

i won't go lot of detail (i'll include link codeskulptor page can see yourself) 1 thing that's bugging me enemies slowing down, should speed up!

i'm printing horizontal velocity (vx) console , can see it's increasing. speeding works until point slow down again, while vx still increasing?! easy spot when there 4 enemies left...

all enemy/alien objects stored in list, when hit laser, deleted list function called 'kill'.

heres function:

def kill(enemy):     global points     points += 1     enemies in enemylist_1:         if enemies.vx > 0:             enemies.vx += 1.0/4/60         elif enemies.vx < 0:             enemies.vx -= 1.0/4/60     enemylist_1.remove(enemy) 

as can see enemy objects in enemylist_1 list , removed when function called (the function called following function in enemies class):

    def laserhit(self, laser):         global wait, friendlaserlist         if abs(self.x - laser.x) < self.size[0] / 2.0 , abs(self.y - laser.y) < self.size[1] / 2.0:             laser.x = width             laser.y = height             laser.pos = [width, height]             laser.vy = 0             wait = 0.0             friendlaserlist = []             kill(self) 

the wait variable put in create cooldown between laser firing. don't bother looking @ it

my code looks trainwreck part it's working , i'm happy own work, it's first time i've ever done this...

the link game (not finishes of course, comments in dutch, teacher etc. variables in english: http://www.codeskulptor.org/#user38_acl7aifiuqohq0n_29.py

the first couple of times try run it, not work because simplegui module loading images. (our teacher accepts immages stored in google drive)

my burning question: how come enemies slowing down when more killed on time, while code explicitly states should speed up?

ps: i've tried, instead of deleting enemies list, place them outside canvas, gave me trouble laser - enemy collision checking. i'd rather delete them since makes game run more smoothly. i've tried .pop() , 'del' same problem

i apoligise n00b code , english mistakes...

no don't freak out on how bad code right now, lot of stuff wasn't workin , had change values etc. once working should clean etc. right need fixed , confused wether it's did or if it's bug...

my entire code people don't want go codeskulptor (warning: it's mess!):

import simplegui import random  # frame opties width = 450.0 height = 600.0 border = (width / 16, height / 12, 1, 'white')  # vijand opties # grootte van de vijanden kan je aanpassen, ook het aantal. # aantal/grootte. natuurlijk niet te groot maken... enemy_size = 20.0 enemy_centrepoint = enemy_size / 2.0 enemy_rows = 3 enemy_row_amount = 13 enemy_amount = enemy_row_amount * enemy_rows  # score level = 1 points = 0  # speler lives = 3 player_size = 22.0  # plaatjes heart = simplegui.load_image('http://googledrive.com/host/0bzehwageu4yuvefjazfnvdjirfe/hart.png') heartwh = [heart.get_width(), heart.get_height()] heartcenter = [heartwh[0] / 2, heartwh[1] / 2]  # overige variabelen. padding = (border[0] + enemy_size * 4.0, border[1] + enemy_size * 0.64285714285) spacing = (width - padding[0] * 2.0) / (enemy_row_amount - 1) checkx = padding[0] - border[0] - enemy_centrepoint   # class voor de vijanden van level 1 class enemy_one(object):     image = simplegui.load_image('http://googledrive.com/host/0bzehwageu4yuvefjazfnvdjirfe/vijand1wit.png')     width = image.get_width()     height = image.get_height()     wh = (width, height)     center = (width / 2.0, height / 2.0)     size = (enemy_size,enemy_size * height/width)     vx = enemy_size/20.0/60.0     vy = enemy_size / 2.0     att = 2000     def __init__(self, x, y):         self.x = x         self.y = y         self.pos = [float(self.x),float(self.y)]         self.x_org = self.pos[0]     # verandert positie vijanden         def update(self):         self.x = self.pos[0]         self.y = self.pos[1]     # bepaalt of de vijanden schieten of niet.     def shoot(self):         self.attack = random.randrange(0,int(self.att))         if 6 <= self.attack <= 7:                 enemylaserlist.append(enemylaser(self.pos[0], self.pos[1]))     # bepaalt of de vijanden worden geraakt of niet     def laserhit(self, laser):         global wait, friendlaserlist         if abs(self.x - laser.x) < self.size[0] / 2.0 , abs(self.y - laser.y) < self.size[1] / 2.0:             laser.x = width             laser.y = height             laser.pos = [width, height]             laser.vy = 0             wait = 0.0             friendlaserlist = []             kill(self) # class voor de fighter (jij) class fighter(object):     image = simplegui.load_image('http://googledrive.com/host/0bzehwageu4yuvefjazfnvdjirfe/speler.png')     width = image.get_width()     height = image.get_height()     wh = (width, height)     center = (width / 2.0, height / 2.0)     vx = 0     vx_org = 5     size = (player_size, player_size * height/width)     def __init__(self, x, y):         self.x = x         self.y = y         self.pos = [self.x, self.y]     def move(self):         if border[0] + player_size / 2.0 < self.x < width - border[0] - player_size / 2.0:             self.x += self.vx             self.pos = [self.x, self.y]         elif self.x <= border[0] + player_size / 2.0:             self.x += 1         elif self.x >= width - border[0] - player_size / 2.0:             self.x -= 1    # kijken of de speler wordt geraakt door een laser     def laserhit(self, laser):         global lives         if abs(self.x - laser.x) < self.size[0] / 2.0 , abs(self.y - laser.y) < self.size[1] / 2.0:             lives -= 1             elaserkill(laser)     # schieten     def shoot(self):         global friendlaserlist         friendlaserlist.append(friendlylaser(self.x, self.y)) friendlaserlist = [] # class voor de laser van de speler class friendlylaser(object):     image = simplegui.load_image('http://googledrive.com/host/0bzehwageu4yuvefjazfnvdjirfe/lasergroen.png')     width = image.get_width()     height = image.get_height()     wh = (width, height)     center = (width / 2, height / 2)     vy = -10     size = (enemy_size / 10.0, enemy_size * 4 / 10.0)     def __init__(self,x,y):         self.x = x         self.y = y         self.pos = [self.x, self.y]     # verandert positie vriendelijke laser.     def update(self):         self.y += self.vy         self.pos = [self.x, self.y]         if self.y < border[1] or self.pos == [width, height]:             flaserkill(self) # class voor de vijand lasers class enemylaser(object):     image = simplegui.load_image('http://googledrive.com/host/0bzehwageu4yuvefjazfnvdjirfe/laser.png')     width = image.get_width()     height = image.get_height()     wh = (width, height)     center = (width / 2, height / 2)     vy = 5     size = (enemy_size / 10.0, enemy_size * 4 / 10.0)     def __init__(self,x,y):         self.x = x         self.y = y         self.pos = [self.x, self.y]     # verandert positie vijandelijke laser.     def update(self):         self.y += self.vy         self.pos = [self.x, self.y]         if self.y > height - 10 or self.pos == [width, height]:             elaserkill(self) # speler player = fighter(width / 2.0, (height - border[1]) + (border[1] / 2.0)) # vijand laser list enemylaserlist = [] # level 1 vijanden maken enemylist_1 = [] def level_1():     global enemylist_1, lives, points, enemylaserlist, level, friendlaserlist, wait     wait = 0.0     level = 1     lives = 3     points = 0     enemylist_1 = []     enemylaserlist = []     friendlaserlist = []     in range(0,enemy_amount + 1):         # rij 1         if < enemy_row_amount:             enemylist_1.append(enemy_one(padding[0] + spacing * (i),padding[1]))         # rij 2         elif enemy_row_amount < <= enemy_row_amount * 2:             enemylist_1.append(enemy_one(padding[0] + spacing * (i-enemy_row_amount - 1),padding[1] + spacing))         # rij 3         elif > enemy_row_amount * 2:             enemylist_1.append(enemy_one(padding[0] + spacing * (i-enemy_row_amount * 2 - 1),padding[1] + spacing * 2))  # vijanden positie verplaatsen def posupdate():     global lives     enemy in enemylist_1:         if enemy.x_org - checkx <= enemy.pos[0] <= enemy.x_org + checkx:             enemy.pos[0] += enemy.vx         elif enemy.y + enemy.wh[1] >= height - border[1]:             lives = 0         else:             enemy.vx = -enemy.vx             enemy.pos[0] += enemy.vx             enemy.pos[1] += enemy.vy # haal vijanden weg def kill(enemy):     global points     points += 1     enemies in enemylist_1:         if enemies.vx > 0:             enemies.vx += 1.0/4/60         elif enemies.vx < 0:             enemies.vx -= 1.0/4/60         if enemies.att > 2000 * 0.95 ** 10:             enemies.att = enemy.att * 0.95         elif enemies.att < 2000 * 0.95 ** 10:             enemies.att = enemy.att * 0.97     enemylist_1.remove(enemy)     print len(enemylist_1) def elaserkill(laser):     enemylaserlist.remove(laser) def flaserkill(laser):     friendlaserlist.remove(laser) def draw(canvas):     global level, points, lives, wait     # tekst, score, levens, etc.     canvas.draw_text('levens: ',(border[0] + 10, 20),20, 'white', 'monospace')     canvas.draw_text('score: ' + str(points),(border[0] + 10, 20 * 2),20, 'white', 'monospace')     canvas.draw_text('level: ' + str(level),(width - border[0] - frame.get_canvas_textwidth('level:  ' + str(level),20,'monospace'), 20 * 2),20, 'white', 'monospace')     in range(1, lives + 1):         canvas.draw_image(heart,                           heartcenter,                           heartwh,                             [border[0] + 10 + text_levens + 20 * i,                              13],                             (20,                              20.0 * heartwh[1]/heartwh[0]))     # borders voor debuggen     canvas.draw_line((border[0],0),                      (border[0],height),                      border[2],                      border[3]                      )     canvas.draw_line((width-border[0],0),                      (width-border[0],height),                      border[2],                      border[3]                      )     canvas.draw_line((0,border[1]),                      (width,border[1]),                      border[2],                      border[3]                      )     canvas.draw_line((0,height - border[1]),                      (width,height - border[1]),                      border[2],                      border[3]                      )     # teken vijandelijke lasers     laser in enemylaserlist:         laser.update()         canvas.draw_image(laser.image,                           laser.center,                           laser.wh,                           laser.pos,                           laser.size)     if points == enemy_amount , level == 1:         #level = 2         #levellabel.set_text("level 2")         if lives < 4:             lives += 1         canvas.draw_text("je hebt gewonnen!",                          (width / 2 - frame.get_canvas_textwidth('je hebt gewonnen!', 40, 'monospace') / 2,height / 2),                          40, 'white', 'monospace')         canvas.draw_text("druk op 'r' voor een replay!",                          (width / 2 - 10 - frame.get_canvas_textwidth('druk op 'r' voor een replay!', 20, 'monospace') / 2,height / 2 + height / 8),                          20, 'white', 'monospace')     if lives <= 0:         canvas.draw_text("je hebt verloren!",                          (width / 2 - frame.get_canvas_textwidth('je hebt verloren!', 40, 'monospace') / 2,height / 2),                          40, 'white', 'monospace')         canvas.draw_text("druk op 'r' voor een replay!",                          (width / 2 - 10 - frame.get_canvas_textwidth('druk op 'r' voor een replay!', 20, 'monospace') / 2,height / 2 + height / 8),                          20, 'white', 'monospace')     else:         # teken & beweeg speler         player.move()         canvas.draw_image(player.image,                           player.center,                           player.wh,                           player.pos,                           player.size)         # laser hit checken voor de speler         laser in enemylaserlist:                 player.laserhit(laser)         # schieten check         if wait > 0.0:             wait -= 1/60.0         else:             wait = 0.0         # laser v.d. speler tekenen         laser in friendlaserlist:             laser.update()             canvas.draw_image(laser.image,                               laser.center,                               laser.wh,                               laser.pos,                               laser.size)         # level 1 vijanden         if level == 1:                     # update posities             enemy in enemylist_1:                 posupdate()                 enemy.update()                 enemy.shoot()                 laser in friendlaserlist:                     enemy.laserhit(laser)              # teken vijanden             enemy in enemylist_1:                 canvas.draw_image(enemy.image,                                 enemy.center,                                 enemy.wh,                                 enemy.pos,                                 enemy.size)     # debug labels     pointslabel.set_text("punten: " + str(points))     liveslabel.set_text("levens: " + str(lives)) # debug vermoorden van vijanden # ze worden sneller naarmate er meer doodgaan. # ze gaan meer aanvallen naarmate er meer doodgaan. indexlist = [] wait = 0.0 def keydown(key):     global wait     # speler bewegen naar links     if key == simplegui.key_map['left']:         player.vx = -player.vx_org     # speler bewegen naar rechts     elif key == simplegui.key_map['right']:         player.vx = player.vx_org     # spel opnieuw spelen     elif key == simplegui.key_map['r']:         level_1()     # schieten     elif key == simplegui.key_map['space']:         if wait == 0.0:             wait = 1.0             player.shoot() def keyup(key):     if key == simplegui.key_map['left']:         player.vx = 0     elif key == simplegui.key_map['right']:         player.vx = 0  def button_kill():     global points, indexlist     # 'dood' vijand, voor testen!     kill(enemylist_1[0])  def button_lives_add():     global lives     lives += 1 def button_lives_del():     global lives     lives -= 1 # frame maken frame = simplegui.create_frame("game",width,height) frame.set_draw_handler(draw) frame.set_keyup_handler(keyup) frame.set_keydown_handler(keydown)  text_levens = frame.get_canvas_textwidth('levens:', 20, 'monospace')  # debug buttons. debuglabel = frame.add_label("debug knoppen.") button = frame.add_button("random kill enemy",button_kill,150) pointslabel = frame.add_label("") liveslabel = frame.add_label("") levellabel = frame.add_label("level: " + str(level)) button2 = frame.add_button("extra leven",button_lives_add,150) button2 = frame.add_button("een leven minder",button_lives_del,150)  # starten (level 1 & frame) level_1() frame.start() 

if need more specific parts of code please ask. hear people don't whehn have read through entire code...

my thought performance-related. when have bunch of enemies, takes longer draw them. don't know how simplegui handles this, looks expect give frame 60 times second, may not reasonable assumption.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -