Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
1
Indexable
Never
#shiptest.py

import pygame
class Ship:
    def __init__(self,game):
        #self.game=game
        self.screen=game.screen
        self.screen_rect=game.screen.get_rect()
        #haetaan image muuttujaan kuva
        #Try laita try:n "taakse"!
        self.image=pygame.image.load("images/ship.png").convert_alpha()
        #rect muutjaan asetaan kuvan dimensiot
        self.rect=self.image.get_rect()
        #asetetaan olion suorakulmio keskelle pääohjelman ruudun alanostaa
        self.rect.midbottom=self.screen_rect.midbottom
        self.moving_right=False
        self.press_down=False


    def blit(self):
        #draw picture 1. parameter is picture , the other is rectangele
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_right:
            self.rect.x+=1

#settings.py

import sys
import pygame
from shiptest import Ship

class Settings:
    def __init__(self):
        self.screenWidth=800
        self.ScreenHeight=1200
        self.caption="Space Invaders"
        self.bg_color=(0,0,230)

    def check_key(self,game_ship):
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
            elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    game_ship.moving_right=True
        pygame.display.flip()

#main.py


import pygame
import sys
from settings import Settings
from shiptest import Ship

class SpaceInvaders:
    def __init__(self):
        pygame.init()
        self.settings=Settings()
        self.screen=pygame.display.set_mode((self.settings.ScreenHeight,self.settings.screenWidth))
        pygame.display.set_caption(self.settings.caption)
        #self.caption=self.settings.caption
        #tee ship
        self.ship=Ship(self) #ship-olii saa käyttöönsä kaikki muuttujat
        

    def run(self):
        while True:
            self.settings.check_key(self.ship)
            self.screen.fill(self.settings.bg_color)
            self.ship.blit()
            pygame.display.flip()
            self.ship.update()
           # self.ship.termianate()

if __name__=="__main__":
    
    game=SpaceInvaders()
    game.run()