Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import random
- # zmienne globalne
- okno_otwarte = True
- plik = 'majne.png'
- okno_otwarte = True
- kolec_gora = "kolec_gora.png"
- kolec_dol = "kolec_dol.png"
- kolec_prawo = "kolec_prawo.png"
- kolec_lewo = "kolec_lewo.png"
- # stałe
- ROZMIAR = SZEROKOŚĆ, WYSOKOŚĆ = (400, 600)
- BIAŁY = pygame.color.THECOLORS['white']
- CZARNY = pygame.color.THECOLORS['black']
- CIEMNOCZERWONY = pygame.color.THECOLORS['darkred']
- CIEMNOZIELONY = pygame.color.THECOLORS['darkgreen']
- SZARY = pygame.color.THECOLORS['darkgray']
- JASNONIEBISKI = pygame.color.THECOLORS['lightblue']
- GOLD = pygame.color.THECOLORS['gold']
- # ustawienia okna i gry
- pygame.init()
- ekran = pygame.display.set_mode(ROZMIAR)
- pygame.display.set_caption('Gra platformowa.')
- zegar = pygame.time.Clock()
- # klasy
- class Gracz(pygame.sprite.Sprite):
- def __init__(self, plik_obrazu):
- super().__init__()
- self.image = pygame.image.load(plik_obrazu)
- self.rect = self.image.get_rect()
- self.ruch_x = 0
- self.ruch_y = 0
- self.kierunek = 'RIGHT'
- def update(self):
- # ruch w pionie
- self.grawitacja()
- self.rect.y += self.ruch_y
- # ruch w poziomie
- self.rect.x += self.ruch_x
- def grawitacja(self):
- if self.ruch_y == 0:
- self.ruch_y = 1
- else:
- self.ruch_y += 0.35
- def reakcja_na_zdarzenia(self, event):
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- self.gora()
- def lewo(self):
- self.ruch_x = -6
- if self.kierunek == 'RIGHT':
- self.image = pygame.transform.flip(self.image, True, False)
- self.kierunek = 'LEFT'
- def prawo(self):
- self.ruch_x = 6
- if self.kierunek == 'LEFT':
- self.image = pygame.transform.flip(self.image, True, False)
- self.kierunek = 'RIGHT'
- def dol(self):
- self.ruch_y = 6
- def gora(self):
- self.ruch_y = -9
- def stop(self):
- self.ruch_x = 0
- self.ruch_y = 0
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- class Kolec(pygame.sprite.Sprite):
- def __init__(self, kolec, x, y):
- super().__init__()
- self.image = pygame.image.load(kolec)
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- def draw(self, surface):
- surface.blit(self.image, self.rect)
- class Platforma(pygame.sprite.Sprite):
- def __init__(self, szerokość, wysokość, kolor):
- super().__init__()
- self.szerokość = szerokość
- self.wysokość = wysokość
- self.kolor = kolor
- self.image = pygame.Surface([self.szerokość, self.wysokość ])
- self.rect = self.image.get_rect()
- self.image.fill(self.kolor)
- class Plansza:
- def __init__(self,gracz):
- self.gracz = gracz
- self.platformy = pygame.sprite.Group()
- self.ws_platform = [[SZEROKOŚĆ, 15, 0, WYSOKOŚĆ - 15],
- [SZEROKOŚĆ, 15, 0, 0],
- [15, WYSOKOŚĆ, 0 , 0],
- [15, WYSOKOŚĆ, SZEROKOŚĆ - 15 , 0] ]
- self.rysuj_plansze()
- def rysuj_plansze(self):
- for p in self.ws_platform:
- platforma = Platforma(p[0], p[1], SZARY)
- platforma.rect.x = p[2]
- platforma.rect.y = p[3]
- self.platformy.add(platforma)
- def draw(self, surface):
- surface.fill(BIAŁY)
- self.platformy.draw(surface)
- # konkretyzacja obiektów
- def losuj_lewo(grupa_boczne):
- grupa_boczne.empty()
- for tmp in range(random.randint(1,5)):
- y = random.randint(41, WYSOKOŚĆ - 15-26-44)
- while sprawdz(grupa_boczne, y):
- y = random.randint(41, WYSOKOŚĆ - 15-26-44)
- grupa_boczne.add(Kolec(kolec_lewo, 15, y))
- return grupa_boczne
- def losuj_prawo(grupa_boczne):
- grupa_boczne.empty()
- for x in range(random.randint(1,5)):
- y = random.randint(41, WYSOKOŚĆ - 15-26-44)
- while sprawdz(grupa_boczne, y):
- y = random.randint(41, WYSOKOŚĆ - 15-26-44)
- grupa_boczne.add(Kolec(kolec_prawo, SZEROKOŚĆ - 15-26, y))
- return grupa_boczne
- def sprawdz(grupa, y):
- for obj in grupa:
- if obj.rect.y < y < obj.rect.y+44 or obj.rect.y < y+44 < obj.rect.y+44:
- return True
- return False
- ##kolec = Kolec(kolec_gora, 100, 70)
- gracz = Gracz(plik)
- gracz.rect.left = SZEROKOŚĆ//2-26/2
- gracz.rect.bottom = 200
- gracz.ruch_x = 5
- aktualna_plansza = Plansza(gracz)
- grupa_kolce = pygame.sprite.Group()
- grupa_boczne = pygame.sprite.Group()
- for x in range(7):
- ## kolec = Kolec(kolec_gora, 15*x+5, 20)
- grupa_kolce.add(Kolec(kolec_gora, 50*x+26, 15))
- grupa_kolce.add(Kolec(kolec_dol, 50*x+26, WYSOKOŚĆ-41))
- ##grupa_boczna = losuj_prawo(grupa_boczna)
- # pętla gry
- while okno_otwarte:
- # pętla zdarzeń
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- okno_otwarte = False
- else:
- gracz.reakcja_na_zdarzenia(event)
- if gracz.rect.right > SZEROKOŚĆ-15:
- gracz.lewo()
- grupa_boczne = losuj_lewo(grupa_boczne)
- if gracz.rect.left < 16:
- gracz.prawo()
- grupa_boczne = losuj_prawo(grupa_boczne)
- if gracz.rect.bottom > WYSOKOŚĆ - 15-26:
- okno_otwarte = False
- gracz.stop()
- if gracz.rect.top < 15+26:
- okno_otwarte = False
- for obj in grupa_boczne:
- if pygame.sprite.collide_mask(gracz, obj) != None:
- okno_otwarte = False
- aktualna_plansza.draw(ekran)
- grupa_kolce.draw(ekran)
- grupa_boczne.draw(ekran)
- gracz.update()
- gracz.draw(ekran)
- pygame.display.flip()
- zegar.tick(30)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement