class Game: def __init__(self, REALWIDTH, REALHEIGHT, GAMEWIDTH, GAMEHEIGHT): """ peaklass """ global levelTime pygame.init() self.width = GAMEWIDTH self.height = GAMEHEIGHT self.realwidth = REALWIDTH self.realheight = REALHEIGHT self.screen = pygame.display.set_mode((self.realwidth,self.realheight)) pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0)) # tavaline hiir n'htamatuks self.welcomeScreen = pygame.image.load('Pics/gameAvaekraan4.png').convert() self.statboard = pygame.image.load("Pics/statboard3.png").convert() self.pauseScreen = pygame.image.load('Pics/paused.png').convert_alpha() self.speedpot = pygame.image.load("Pics/speedpot.png").convert_alpha() self.hppot2 = pygame.image.load("Pics/2HPpot.png").convert_alpha() self.hppot5 = pygame.image.load("Pics/5HPpot.png").convert_alpha() self.background = pygame.transform.scale((pygame.image.load("Pics/spacev3.png").convert()), (1024,768)) self.MachineGun = pygame.transform.scale((pygame.image.load("Pics/machinegun.png").convert_alpha()), (60,40)) self.ShotGun = pygame.transform.scale((pygame.image.load("Pics/shotgun.png").convert_alpha()), (60,40)) self.HandGun = pygame.transform.scale((pygame.image.load("Pics/handgun2.png").convert_alpha()), (50,50)) self.bg_imgRect = self.background.get_rect() pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096) pygame.mixer.music.load('Sounds/track1_track2.mp3') # <--------------------------------------------------------- SIIN TAUSTAMUSS pygame.mixer.music.play(-1) # maitu korda m'ngib self.music_playing = 1 self.play_sounds = 1 """ kiirus - bloki kiirusW maxw - maksimaalne laius maxh - maksimaaline pikkus lykkab - mitu pixlit lykkab eemale kokkuporkel dmg - mitu dmg teeb lykkamisega """ self.blokityyp = { "tavaline" : { "maxKiirus" : 0.2, # bloki maksimaalne kiirus "lykkab" : 0, # lykkamistugevus "dmg" : 0, # dmg kokkuporkel "img" : "Pics/tavalineblokkBlurred.png" }, "lykkaja" : { "maxKiirus" : 0.5, "lykkab" : 200, "dmg" : 2, "img" : "Pics/lykkajablokkBlurred.png" } } self.enemytype = { "tavaline" : { "elusi" : 1, "img" : "Pics/tavalinevastane30x30.png", "speed" : 0.1, "dmg" : 1 }, "tulistaja" : { "elusi" : 2, "img" : "Pics/tulistajavastane40x40_2.png", "speed" : 0.05, "dmg" : 2, "weapon" : 1, "delay" : 1 }, "boss" : { "boss" : 1, "elusi" : 10, "img" : "Pics/boss.png", "speed" : 0.1, "dmg" : 1000, "weapon" : 1, "delay" : 1 } } self.blokid = [] self.pahad = [] self.laserid = [] self.lained = [] pygame.font.get_fonts() self.font=pygame.font.SysFont('bauhaus93',35) self.level = 1 self.levelTime = levelTime self.levelTimer = Timer(self.levelTime) self.run = True self.mouseHolding = False self.gaming = False self.randomItems = [] # timer mis hakkab random aja tagant maha asju genereerima self.randomItemTimer = Timer(random.randint(10,self.levelTime*2)) self.randomItemTimer.run() self.bossInit = False # alguses pole bossi self.linex = 300 self.linexDx = 1 def update_logic(self): if not self.gaming: return self.generate_random_items() self.mees.update_logic() # uuendab meest if(self.mouseHolding): # kui hiirt hoitakse all->automaatne tulistamine self.mees.automatic() for laser in self.laserid: laser.update_logic() if(collision(laser.rect,self.mees.rect) and laser.wait.end == True and laser.delay.end == True): # kui mees saab laserit self.mees.getRekt(laser.dmg) # mees saab dmg laser.bye() for laine in self.lained: if not(laine.update_logic()): # laine joudnud loppu if(laine.type == "lyke"): # kui lykkav laine if(laine in self.lained): # kaob ara self.lained.remove(laine) else: # vastasel juhul havitame ------------------------------< SIIN K@IB PAUK for enemy in self.pahad: if(collision_circle_rect(laine,enemy.rect)): if(enemy in self.pahad): self.pahad.remove(enemy) self.lained.remove(laine) continue for enemy in self.pahad: if(collision_circle_rect(laine,enemy.rect)): if(laine.type == "lyke"): if(enemy.vector[0] > 0): # kui vastane liigub paremale, lykkame vasakule enemy.rect.x -= 50 else: enemy.rect.x += 50 if(enemy.vector[1] > 0): enemy.rect.y -= 50 else: enemy.rect.y += 50 for blokk in self.blokid: blokk.update_logic() # uuendame bloki liikumist self.mees.check_collision(blokk) # vaatame kas blokk porkab kokku mehega for enemy in self.pahad: enemy.check_collision(blokk) self.check_bullets() # uuendab v2lja lastud kuulidega seotud loogikat self.man_item_collision() for enemy in game.pahad: enemy.attack(self.mees) # lape if(collision(enemy.rect, self.mees.rect)): # kui paha puutub peameest if(self.mees.getRekt(enemy.dmg)): # mang labi pass self.pahad.remove(enemy) # paha ohverdas kahjuks end :( #if(len(self.pahad) <= 10): # hard mode lels # self.create_enemies(10*self.level) def update_display(self): # uuendab koike mida naidatakse if self.gaming == True: self.screen.blit(self.background, self.bg_imgRect) self.mees.show(self.screen) # peavend for blokk in self.blokid: # joonistame koik blokid blokk.show(self.screen) for enemy in self.pahad: # joonistame koik pahad enemy.show(self.screen) for laser in self.laserid: laser.show(self.screen,self.play_sounds) for item in self.randomItems: #joonistame maas olevaid boonus asju item.show(self.screen) for laine in self.lained: laine.show(self.screen) self.draw_text() if(self.levelTimer.paused == 1): # m2ng pausitud, n2itame pausi pilti self.screen.blit(self.pauseScreen,(0,0)) else: self.screen.blit(self.welcomeScreen,(0,0)) self.linex += self.linexDx if(self.linex > 650 or self.linex < 210): self.linexDx = -self.linexDx pygame.draw.rect(self.screen,(0,255,0),(self.linex,550,100,2)) self.draw_cursor() pygame.display.flip() def draw_text(self): self.screen.blit(self.statboard, (0,620)) scoretext=self.font.render(str(self.level), 1,(20,250,20)) self.screen.blit(scoretext, (471, 639)) scoretext2=self.font.render(str(self.levelTimer.get_secs()), 1,(20,250,20)) self.screen.blit(scoretext2, (550, 702)) for i,slot in enumerate(self.mees.relvakogu): if (slot=="handgun"): self.screen.blit(self.HandGun, (865+i*50,642)) elif(slot=="machinegun"): self.screen.blit(self.MachineGun, (865+i*50,642)) elif(slot=="pump"): self.screen.blit(self.ShotGun, (865+i*50,642)) if (game.mees.relv == slot): self.slotColor = (20,250,20) else: self.slotColor = (0,0,0) self.slots = self.font.render(str(i+1), 1,self.slotColor) self.screen.blit(self.slots, (885+i*50,619)) for i,slot in enumerate(self.mees.potikogu): if(slot == 0): self.screen.blit(self.hppot2,(200+i*100,707)) elif(slot == 1): self.screen.blit(self.hppot5,(200+i*100,707)) elif(slot == 2): self.screen.blit(self.speedpot,(200+i*100,707)) self.slots = self.font.render(str(i+6), 1,(50,0,0)) self.screen.blit(self.slots, (200+i*100,698)) if(game.mees.relv != "handgun"): bulletsNow = str(self.mees.relvad[self.mees.relv]["bullets"]) bulletsTotal = str(self.mees.relvad[self.mees.relv]["kokku"]) else: bulletsNow = "-" bulletsTotal = "-" scoretext=self.font.render(bulletsNow+"/"+bulletsTotal, 1,(20,250,20)) if(self.mees.lives < 3): self.livesColor = (255,0,0) elif(self.mees.lives >= 3 and self.mees.lives < 5): self.livesColor = (255,128,0) else: self.livesColor = (0,255,0) scoretext2=self.font.render(str(self.mees.lives),1,self.livesColor) self.screen.blit(scoretext, (833, self.height+83)) self.screen.blit(scoretext2, (175, self.height+21)) #pygame.draw.rect(self.screen,(250,125,125),(0,self.height+10,self.width,self.realheight-self.height)) def Level(self): # h2ndlib leveleid if(self.levelTimer.end == True): if(self.bossInit == False): self.del_bloks() self.del_enemies() boss = Enemy(self.enemytype["boss"],True,w=self.level*25,h=self.level*25) boss.elusi = (self.level*3) self.pahad.append(boss) self.bossInit = True self.create_lasers(2*self.level) game.mees.speed += 0.2 game.mees.ulti += 2 # anname 2 ulti juurde else: if(len(self.pahad) == 0): self.bossInit = False self.levelTimer.reset() self.del_lasers() self.next_level() def next_level(self): self.level += 1 # uuendame levelit self.create_bloks(self.level*5) self.create_enemies(self.level*15) def create_bloks(self,count): # loob uusi blokke for i in range(count): if(random.randint(1,3) > 1): # yks kolmele et tuleb ull blokk temp = Blokk(self.blokityyp["tavaline"]) else: temp = Blokk(self.blokityyp["lykkaja"]) self.blokid.append(temp) def create_enemies(self,count): # loob uusi vastaseid for i in range(count): if(random.randint(1,5) > 1): # 20% et tulistaja temp = Enemy(self.enemytype["tavaline"]) temp.elusi = random.randint(1,self.level) else: temp = Enemy(self.enemytype["tulistaja"]) temp.elusi = random.randint(2,self.level*2) if(random.randint(1,5) > 3): temp.poiklemine = 1 self.pahad.append(temp) def create_lasers(self,count): for i in range(count): temp = Laser(random.randint(2,self.level*8)) self.laserid.append(temp) def del_bloks(self): self.blokid = [] def del_enemies(self): self.pahad = [] def del_lasers(self): self.laserid = [] def check_bullets(self): # for bullet in self.mees.bullets: # vaatame millega kuulid kokku porkavad : if not(rect_in_map(bullet.rect)): # kustutame kuuli kui see poel enam mapi piires.wd if(bullet in self.mees.bullets): # mingi lamp self.mees.bullets.remove(bullet) continue # kuul eemaldatud, ehk votame jargmise ette for blokk in self.blokid: # blokiga? if(collision(bullet.rect, blokk.rect)): if(bullet in self.mees.bullets): self.mees.bullets.remove(bullet) # kui jah siis kustutame kuuli. break for enemy in self.pahad: # pahade poistega ? if(collision(bullet.rect, enemy.rect)): if (enemy.getRekt(bullet.dmg)): if("boss" in enemy.type): try: # viskab mingi errori non-integer stop for randrange(). Ei oska muudmoodi lahendada :( miniBoss = Enemy(enemy.type,True,enemy.rect.x+random.randint(0,enemy.rect.w),enemy.rect.y,enemy.rect.w/2,enemy.rect.h/2) miniBoss2 = Enemy(enemy.type,True,enemy.rect.x+random.randint(0,enemy.rect.w),enemy.rect.y+enemy.rect.h,enemy.rect.w/2,enemy.rect.h/2) miniBoss.elusi,miniBoss2.elusi = (self.level*3),(self.level*3) if(miniBoss.rect.h > 10): # kontrollime et liiga mini poleks self.pahad.append(miniBoss) self.pahad.append(miniBoss2) except Exception as e: print (e) self.pahad.remove(enemy) if(bullet in self.mees.bullets): # mingi lamp self.mees.bullets.remove(bullet) for enemy in self.pahad: if(enemy.shooter): for bullet in enemy.bullets: if not(rect_in_map(bullet.rect)): # kui kuul mapist valjas, kaotame if(bullet in enemy.bullets): enemy.bullets.remove(bullet) continue for blokk in self.blokid: # blokiga? if(collision(bullet.rect, blokk.rect)): if(bullet in enemy.bullets): enemy.bullets.remove(bullet) # kui jah siis kustutame kuuli. break if(collision(bullet.rect,self.mees.rect)): self.mees.getRekt(bullet.dmg) enemy.bullets.remove(bullet) # kui jah siis kustutame kuuli. def draw_cursor(self): # joonistab hiire sihiku mouse = pygame.mouse.get_pos() self.mouseLineLen = 20 self.mouseColor = (255,255,255) pygame.draw.line(self.screen,(self.mouseColor),(mouse[0]-self.mouseLineLen,mouse[1]),(mouse[0]+self.mouseLineLen,mouse[1]),2) pygame.draw.line(self.screen,(self.mouseColor),(mouse[0],mouse[1]+self.mouseLineLen),(mouse[0],mouse[1]-self.mouseLineLen),2) def generate_random_items(self): self.randomItemTimer.update() # uuendame timerit mis h2ndlib uute asjade loomist for item in self.randomItems: # uuendame asju maas item.update() if(item.end()): self.randomItems.remove(item) if(self.randomItemTimer.end == True): # kui aeg saab otsa loome uue asja temp = RandomItem(self.mees.relvad,self.mees.potid) self.randomItems.append(temp) self.randomItemTimer.reset_n(random.randint(10,self.levelTime*2)) # uus suvaline countdown self.randomItemTimer.reset() def man_item_collision(self): for item in self.randomItems: if(collision(self.mees.rect,item.rect)): # kokkuporge mingi asjaga if(self.mees.pickup(item)): # kui korjamine successful if(item in self.randomItems): # korjame yles, kaotame maast self.randomItems.remove(item)
class Mees(object): # peamees global SCREEN_WIDTH, SCREEN_HEIGHT # ekraani laius ja pikkus def __init__(self): #pygame.mixer.init(frequency=22050, size=-16, channels=4) self.saund = pygame.mixer.Sound("Sounds/singleshot.wav") self.saund.set_volume(0.2) self.chan = pygame.mixer.find_channel() self.saund2 = pygame.mixer.Sound("Sounds/teleport2.wav") self.saund2.set_volume(0.8) self.chan2 = pygame.mixer.find_channel() self.saund3 = pygame.mixer.Sound("Sounds/pickup.wav") self.saund3.set_volume(0.8) self.chan3 = pygame.mixer.find_channel() self.saund4 = pygame.mixer.Sound("Sounds/slurp.wav") self.saund4.set_volume(1) self.chan4 = pygame.mixer.find_channel() self.lives = 7 # mitu elu mehel self.rect = Rect(30,SCREEN_HEIGHT-100,10,10) # ta kast self.image = pygame.transform.scale((pygame.image.load("Pics/Kappa.png").convert_alpha()), (30,30)) self.imageDisco = pygame.transform.scale((pygame.image.load("Pics/discokappa.png").convert_alpha()), (30,30)) self.image2 = pygame.image.load('Pics/Untitled.png').convert_alpha() self.image2Rect = self.image2.get_rect() self.newRect = self.image.get_rect() self.rect.w = self.newRect[2] self.rect.h = self.newRect[3] self.speed = 0.8 # kiirus self.bullets = [] # valjalastud kuulid self.bulletCount = 20 # kuulide arv self.font=pygame.font.Font(None,30) #voimalikud relvad self.relvad = { "handgun" : { "dmg" : 1, # palju relv dmg teeb "speed" : 2, # kui kiirelt kuul lendabs "hoida" : 0, # kas automaat "bullets" : 12, # palju kuule "pide" : 12, # palju pide hoiab "kokku" : -1 # palju kokku kuule }, "machinegun" : { "dmg" : 1, "speed" : 4, "hoida" : 1, "bullets" : 50, "pide" : 50, "kokku" : 300, "vahe" : 0.2 # kuulide laskmis vahe ajaliselt automaatselt }, "pump" : { "dmg" : 1, "speed" : 2, "hoida" : 0, "bullets" : 8, "pide" : 8, "kokku" : 72, } } self.potid = { 0 : { "heals" : 2, #"img" : "Pics/2HPpot.png" }, 1 : { "heals" : 5 #"img" : "Pics/5HPpot.png" }, 2 : { "speed" : 1, "time" : 20 } } self.relv = "handgun" # mis relv hetkel self.relvakogu = ["handgun","machinegun"] self.potikogu = [] self.shootTimer = Timer(1) self.speedTimer = Timer(0) self.shootTimer.run() self.koos = [] self.saiJuurde = 0 self.dead = 0 self.ulti = 5 def update_logic(self): self.shootTimer.update() self.speedTimer.update() #vaatame et kastist v'lja ei laheks if(self.rect.y > SCREEN_HEIGHT-self.rect.h): self.rect.y = SCREEN_HEIGHT-self.rect.h elif(self.rect.y < 0): self.rect.y = 0 if(self.rect.x > SCREEN_WIDTH-self.rect.w): self.rect.x = SCREEN_WIDTH-self.rect.w elif(self.rect.x < 0): self.rect.x = 0 for bullet in self.bullets: bullet.update_logic() if(self.speedTimer.end == True and self.saiJuurde != 0): self.speed -= self.saiJuurde self.saiJuurde = 0 def show(self, scr): if(self.saiJuurde != 0): scr.blit(self.imageDisco,self.rect.get()) else: scr.blit(self.image,self.rect.get()) for bullet in self.bullets: # joonistame koik kuulid bullet.show(scr) if (self.dead == 1): scr.blit(self.image2,self.image2Rect) def switchWeapon(self,slot): # vahetab relva try: self.relv = self.relvakogu[slot] except Exception as e: print (e) return def drinkPotion(self,slot): # juuakse potti try: if(self.speed != 0): # kui pole surnud pot = self.potid[self.potikogu[slot]] if "heals" in pot: self.lives += pot["heals"] if "speed" in pot: self.saiJuurde = pot["speed"] self.speed += self.saiJuurde self.speedTimer.reset_n(pot["time"]) self.speedTimer.reset() self.speedTimer.run() del self.potikogu[slot] except Exception as e: print (e) return def shoot(self,start,end,mouseButton): if(self.relvad[self.relv]["kokku"] <= 0 and self.relvad[self.relv]["bullets"] <= 0 and self.relvad[self.relv]["kokku"] != -1): # pole kuule? return temp = Bullet(start[0],start[1],end[0],end[1],self.relvad[self.relv]) self.chan.queue(self.saund) if(self.relv == "pump"): # 2 kuuli lisaks temp2 = Bullet(start[0],start[1],end[0]-50,end[1]-50,self.relvad[self.relv]) temp3 = Bullet(start[0],start[1],end[0]+50,end[1]+50,self.relvad[self.relv]) self.bullets.append(temp2) self.bullets.append(temp3) self.bullets.append(temp) self.relvad[self.relv]["bullets"] -= 1 # laseb yhe kuuli valja if(self.relvad[self.relv]["bullets"] <= 0): if(self.relvad[self.relv]["kokku"] > 0): #vaatame kas varupidemes if(self.relvad[self.relv]["kokku"] <= self.relvad[self.relv]["pide"]): # viimane pide self.relvad[self.relv]["bullets"] += self.relvad[self.relv]["kokku"] self.relvad[self.relv]["kokku"] = 0 else: self.relvad[self.relv]["bullets"] += self.relvad[self.relv]["pide"] self.relvad[self.relv]["kokku"] -= self.relvad[self.relv]["pide"] def automatic(self): """ automaatne tulistamine """ if(self.relvad[self.relv]["hoida"] == 1): if(self.shootTimer.end): self.shoot((self.rect.x,self.rect.y),pygame.mouse.get_pos(),pygame.mouse.get_pressed()) self.shootTimer.reset_n(self.relvad[self.relv]["vahe"]) self.shootTimer.reset() else: return def getRekt(self,dmg): """ peategelane saab dmgi """ self.lives -= dmg # vahendame elusi dmg vorra if(self.lives <= 0): # kas oleme surnud? # ... siia midagi valja moelda # print ("gameover") self.speed = 0 self.dead = 1 return True # tagastab true kui null elu, et mang teaks mida edasi teha return False def check_collision(self,blokk): # uurib kokkupuudet mehe ja bloki vahel if(collision(blokk.rect, self.rect)): #kokkuporge MEHE JA BLOKI VAHEL if(blokk.suund == "hor"): # blokk liigub horisontaalselt # tulles alt voi ylevalt lykkame tagasi if(self.rect.x+self.rect.w<blokk.rect.x+blokk.rect.w and (self.rect.y < blokk.rect.y)): self.rect.y = blokk.rect.y-self.rect.h return elif(self.rect.x+self.rect.w<blokk.rect.x+blokk.rect.w and(self.rect.y+self.rect.h>blokk.rect.y+blokk.rect.h)): self.rect.y = blokk.rect.y+blokk.rect.h return if(blokk.dx > 0): # blokk liigub paremale if(self.rect.x+self.rect.w>blokk.rect.x+blokk.rect.w): self.rect.x = blokk.rect.x+blokk.rect.w self.rect.x += blokk.lykkab self.getRekt(blokk.dmg) # blokk teeb dmg ka kokkuporkel. else: self.rect.x = blokk.rect.x-self.rect.w elif(blokk.dx < 0): # blokk liigub vasakule if(self.rect.x<blokk.rect.x): self.rect.x = blokk.rect.x-self.rect.w self.rect.x -= blokk.lykkab self.getRekt(blokk.dmg) # blokk teeb dmg ka kokkuporkel. else: self.rect.x = blokk.rect.x+blokk.rect.w elif(blokk.suund == "ver"): # blokk liigub vertikaalselt # tulles alt voi ylevalt lykkame tagasi if(self.rect.y+self.rect.h<blokk.rect.y+blokk.rect.h and (self.rect.x < blokk.rect.x)): self.rect.x = blokk.rect.x-self.rect.w return elif(self.rect.y+self.rect.h<blokk.rect.y+blokk.rect.h and self.rect.x+self.rect.w>blokk.rect.x+blokk.rect.w): self.rect.x = blokk.rect.x+blokk.rect.w return if(blokk.dy > 0): # blokk liigub alla if(self.rect.y+self.rect.h>blokk.rect.y+blokk.rect.h): self.rect.y = blokk.rect.y+blokk.rect.h self.rect.y += blokk.lykkab self.getRekt(blokk.dmg) # blokk teeb dmg ka kokkuporkel. else: self.rect.y = blokk.rect.y-self.rect.h elif(blokk.dy < 0): # blokk liigub yles if(self.rect.y<blokk.rect.y): self.rect.y = blokk.rect.y-self.rect.h self.rect.y -= blokk.lykkab self.getRekt(blokk.dmg) # blokk teeb dmg ka kokkuporkel. else: self.rect.y = blokk.rect.y+blokk.rect.h self.chan2.queue(self.saund2) def pickup(self,item): if(item.type=="pot"): if(len(self.potikogu) < 3): self.potikogu.append(item.value) self.chan3.queue(self.saund3) return True elif(item.type=="weapon"): if not (item.value in self.relvakogu): # kui relva pole lisame juurde self.relvakogu.append(item.value) else: # lisame kuule self.relvad[item.value]["kokku"] += self.relvad[item.value]["pide"]*5 # anname 5 pide jagu kuule self.chan3.queue(self.saund3) return True elif(item.type=="bullets"): if (item.weaponType in self.relvakogu): self.relvad[item.weaponType]["kokku"] += item.value self.chan3.queue(self.saund3) return True def laine(self): # pole valmis .. if(self.ulti >= 1): self.ulti -= 1 return True else: print ("no ult :(")