» »

python "space invaders" fail

python "space invaders" fail

dejco_h ::

dober dan kako ste kaj? torej delam igrico v pythonu imenovano space invaders in sem naletel na težavo..torej spodaj bom pustil res ful veliko kode tako da če kdo hoče jo lahko skopira ali si jo malo ogleda in naredi igrico drugače oz po svoje..še prej pa bi vas lepo prosil če mi pomagate z to težavo sicer compailer mi zazna to napako(File "C:\Users\dejan\Desktop\pyth programs\space invaders\main.py", line 133, in collide
offset_x=obj2.x-obj1.x
AttributeError: 'list' object has no attribute 'x') napako zgleda zelo enostavno vendar sem verjetno tolko že v kodi da sploh nevidim kaj moram naredit zato se obračam na vas...pa hvala za odogovore že vnaprej(aja koda ni dokončana)

import pygame
import os
import time
import random
White=(255,255,255)
RED=(255,0,0)
pygame.init()
pygame.font.init()
WIDTH,HEIGTH=750,750
win=pygame.display.set_mode((WIDTH,HEIGTH))
pygame.display.set_caption("Space Invaders")
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))


YELLOW_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))


RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))

BG=pygame.transform.scale(pygame.image.load(os.path.join("assets","background-black.png")),(WIDTH,HEIGTH))

class Laser:
    def __init__(self,x,y,img):
        self.x=x
        self.y=y
        self.img=img
        self.mask=pygame.mask.from_surface(self.img)

    def draw(self,win):
        win.blit(self.img,(self.x,self.y))

    def move(self,vel):
        self.y+=vel

    def off_Screan(self,height):
        return not self.y<=height and self.y>=0


    def collison(self,obj):
        return collide(self,obj)

class Ship:
    COOLDOWN=30
    def __init__(self,x,y,health=100):
        self.x=x
        self.y=y
        self.health=health
        self.ship_img=None
        self.laser_img=None
        self.lasers=[]
        self.cool_down_counter=0
    def draw(self,win):
        win.blit(self.ship_img,(self.x,self.y))
        for laser in self.lasers:
            laser.draw(win)

    def move_lasers(self,vel,obj):
        self.coolDOwn()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_Screan(HEIGTH):
                self.lasers.remove(laser)
            elif laser.collison(obj):
                obj.health-=10
                self.lasers.remove(laser)


    def coolDOwn(self):
        if self.cool_down_counter>=self.COOLDOWN:
            self.cool_down_counter=0
        elif self.cool_down_counter>0:
            self.cool_down_counter+=1


    def shoot(self):
        if self.cool_down_counter==0:
            laser=Laser(self.x,self.y,self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter=1

    def get_widht(self):
        return self.ship_img.get_width()
    def get_height(self):
        return self.ship_img.get_height()



class Player(Ship):
    def __init__(self,x,y,health=100):
        super().__init__(x,y,health)
        self.ship_img=YELLOW_SPACE_SHIP
        self.laser_img=YELLOW_LASER
        self.mask=pygame.mask.from_surface(self.ship_img)
        self.Max_health=health

    def move_lasers(self, vel, objs):
        self.coolDOwn()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_Screan(HEIGTH):
                self.lasers.remove(laser)
            else:
                for obj in objs:
                    if laser.collison(objs):
                        objs.remove(obj)
                        if laser in self.lasers:
                            self.lasers.remove(laser)


class Enemy(Ship):
    COLOR_MAP={
        "red":(RED_SPACE_SHIP,RED_LASER),
        "green":(GREEN_SPACE_SHIP,GREEN_LASER),
        "blue":(BLUE_SPACE_SHIP,BLUE_LASER)

    }

    def __init__(self,x,y,color,health=100):
        super().__init__(x,y,health)
        self.ship_img,self.laser_img=self.COLOR_MAP[color]
        self.mask = pygame.mask.from_surface(self.ship_img)

    def move(self,vel):
        self.y+=vel


def collide(obj1, obj2):
    offset_x=obj2.x-obj1.x
    offset_y=obj2.y-obj1.y
    return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) != None

def main():
    run=True
    FPS=60
    level=0
    lives=5
    main_font=pygame.font.SysFont("comicsans",40)
    lost_font = pygame.font.SysFont("comicsans", 50)
    enemies=[]
    wave_lenght=5
    enemy_vel=1
    lost=False
    lost_count=0
    laser_vel=4

    player=Player(300,650)
    player_vel=5
    clock=pygame.time.Clock()


    def redraw_window():
        win.blit(BG,(0,0))
        lives_labe=main_font.render(f"lives:{lives}",1,White)
        level_labe=main_font.render(f"level:{level}",1,White)
        win.blit(lives_labe,(10,10))
        win.blit(level_labe,(WIDTH-level_labe.get_width()-10,10))

        for enemy in enemies:
            enemy.draw(win)

        player.draw(win)
        if lost:
            lost_label=lost_font.render("you lost!!!",1,White)
            win.blit(lost_label,(WIDTH//2-lost_label.get_width()//2,350))

        pygame.display.update()


    while run:
        clock.tick(FPS)
        redraw_window()

        if lives<=0 or player.health<=0:
            lost =True
            lost_count+=1

        if lost:
            if lost_count>FPS*3:
                run=False
            else:
                continue

        if len(enemies)==0:
            level+=1
            wave_lenght+=5
            for i in range(wave_lenght):
                enemy=Enemy(random.randrange(50, WIDTH-100), random.randrange(-1500, -100), random.choice(["red", "green", "blue"]))
                enemies.append(enemy)


        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                run=False

        keys=pygame.key.get_pressed()
        if keys[pygame.K_a]and player.x-player_vel>0:
            player.x-=player_vel

        if keys[pygame.K_d]and player.x+player_vel+player.get_widht()<WIDTH:
            player.x += player_vel

        if keys[pygame.K_w]and player.y-player_vel>0:
            player.y -= player_vel

        if keys[pygame.K_s] and player.y+player_vel+player.get_height()<HEIGTH:
            player.y += player_vel
        if keys[pygame.K_SPACE]:
            player.shoot()

        for enemy in enemies[:]:
            enemy.move(enemy_vel)
            enemy.move_lasers(laser_vel,player)
            if enemy.y+enemy.get_height()>HEIGTH:
                lives-=1
                enemies.remove(enemy)

        player.move_lasers(laser_vel,enemies)


main()


kljuka13 ::

V vrstici 108 mora namesto
if laser.collison(objs):

pisati
if laser.collison(obj):

dejco_h ::

kljuka13 je izjavil:

V vrstici 108 mora namesto
if laser.collison(objs):

pisati
if laser.collison(obj):


ej majster najlepša hvala sedaj deluje


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Return ne deluje[python]

Oddelek: Programiranje
8963 (827) Jonatan
»

Python končnica datoteke

Oddelek: Programiranje
51098 (1013) filips
»

python osnova

Oddelek: Programiranje
212313 (1626) detroit
»

Batch skripta za brisanje map, starejših od X dni

Oddelek: Operacijski sistemi
5806 (741) krneki0001
»

python pomoč

Oddelek: Programiranje
111786 (1606) jype

Več podobnih tem