Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.00 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4.  
  5. # zmienne globalne
  6. okno_otwarte = True
  7. plik = 'majne.png'
  8. okno_otwarte = True
  9. kolec_gora = "kolec_gora.png"
  10. kolec_dol = "kolec_dol.png"
  11. kolec_prawo = "kolec_prawo.png"
  12. kolec_lewo = "kolec_lewo.png"
  13.  
  14.  
  15. # stałe
  16. ROZMIAR = SZEROKOŚĆ, WYSOKOŚĆ = (400, 600)
  17. BIAŁY = pygame.color.THECOLORS['white']
  18. CZARNY = pygame.color.THECOLORS['black']
  19. CIEMNOCZERWONY = pygame.color.THECOLORS['darkred']
  20. CIEMNOZIELONY = pygame.color.THECOLORS['darkgreen']
  21. SZARY = pygame.color.THECOLORS['darkgray']
  22. JASNONIEBISKI = pygame.color.THECOLORS['lightblue']
  23. GOLD = pygame.color.THECOLORS['gold']
  24.  
  25. # ustawienia okna i gry
  26. pygame.init()
  27.  
  28. ekran = pygame.display.set_mode(ROZMIAR)
  29. pygame.display.set_caption('Gra platformowa.')
  30. zegar = pygame.time.Clock()
  31.  
  32. # klasy
  33.  
  34. class Gracz(pygame.sprite.Sprite):
  35.     def __init__(self, plik_obrazu):
  36.         super().__init__()
  37.         self.image = pygame.image.load(plik_obrazu)
  38.         self.rect = self.image.get_rect()
  39.         self.ruch_x = 0
  40.         self.ruch_y = 0
  41.  
  42.         self.kierunek = 'RIGHT'
  43.  
  44.     def update(self):
  45.         # ruch w pionie
  46.         self.grawitacja()
  47.         self.rect.y += self.ruch_y
  48.  
  49.         # ruch w poziomie
  50.         self.rect.x += self.ruch_x
  51.     def grawitacja(self):
  52.         if self.ruch_y == 0:
  53.             self.ruch_y = 1
  54.         else:
  55.             self.ruch_y += 0.35
  56.  
  57.     def reakcja_na_zdarzenia(self, event):
  58.         if event.type == pygame.KEYUP:
  59.             if event.key == pygame.K_UP:
  60.                 self.gora()
  61.  
  62.  
  63.     def lewo(self):
  64.         self.ruch_x = -6
  65.         if self.kierunek == 'RIGHT':
  66.             self.image = pygame.transform.flip(self.image, True, False)
  67.             self.kierunek = 'LEFT'
  68.  
  69.     def prawo(self):
  70.         self.ruch_x = 6
  71.         if self.kierunek == 'LEFT':
  72.             self.image = pygame.transform.flip(self.image, True, False)
  73.             self.kierunek = 'RIGHT'
  74.     def dol(self):
  75.         self.ruch_y = 6
  76.        
  77.     def gora(self):
  78.         self.ruch_y = -9
  79.  
  80.     def stop(self):
  81.         self.ruch_x = 0
  82.         self.ruch_y = 0
  83.        
  84.     def draw(self, surface):
  85.        surface.blit(self.image, self.rect)
  86.  
  87.        
  88. class Kolec(pygame.sprite.Sprite):
  89.     def __init__(self, kolec, x, y):
  90.         super().__init__()
  91.         self.image = pygame.image.load(kolec)
  92.         self.rect = self.image.get_rect()
  93.         self.rect.x = x
  94.         self.rect.y = y
  95.        
  96.     def draw(self, surface):
  97.         surface.blit(self.image, self.rect)
  98.        
  99.  
  100. class Platforma(pygame.sprite.Sprite):
  101.     def __init__(self, szerokość, wysokość, kolor):
  102.         super().__init__()
  103.         self.szerokość = szerokość
  104.         self.wysokość = wysokość
  105.         self.kolor = kolor
  106.         self.image = pygame.Surface([self.szerokość, self.wysokość ])
  107.         self.rect = self.image.get_rect()
  108.         self.image.fill(self.kolor)
  109.        
  110.  
  111.  
  112. class Plansza:
  113.     def __init__(self,gracz):
  114.         self.gracz = gracz
  115.         self.platformy = pygame.sprite.Group()
  116.         self.ws_platform = [[SZEROKOŚĆ, 15, 0, WYSOKOŚĆ - 15],
  117.                        [SZEROKOŚĆ, 15, 0, 0],
  118.                        [15, WYSOKOŚĆ, 0 , 0],
  119.                        [15, WYSOKOŚĆ, SZEROKOŚĆ - 15 , 0] ]
  120.        
  121.         self.rysuj_plansze()
  122.  
  123.        
  124.     def rysuj_plansze(self):
  125.         for p in self.ws_platform:
  126.             platforma = Platforma(p[0], p[1], SZARY)
  127.             platforma.rect.x = p[2]
  128.             platforma.rect.y = p[3]
  129.             self.platformy.add(platforma)
  130.  
  131.  
  132.     def draw(self, surface):
  133.         surface.fill(BIAŁY)
  134.         self.platformy.draw(surface)
  135.  
  136. # konkretyzacja obiektów
  137.  
  138. def losuj_lewo(grupa_boczne):
  139.     grupa_boczne.empty()
  140.    
  141.     for tmp in range(random.randint(1,5)):
  142.         y = random.randint(41, WYSOKOŚĆ - 15-26-44)
  143.  
  144.         while sprawdz(grupa_boczne, y):
  145.             y = random.randint(41, WYSOKOŚĆ - 15-26-44)
  146.            
  147.         grupa_boczne.add(Kolec(kolec_lewo, 15, y))
  148.  
  149.     return grupa_boczne
  150.  
  151. def losuj_prawo(grupa_boczne):
  152.     grupa_boczne.empty()
  153.  
  154.     for x in range(random.randint(1,5)):
  155.         y = random.randint(41, WYSOKOŚĆ - 15-26-44)
  156.  
  157.         while sprawdz(grupa_boczne, y):
  158.             y = random.randint(41, WYSOKOŚĆ - 15-26-44)
  159.                
  160.         grupa_boczne.add(Kolec(kolec_prawo, SZEROKOŚĆ - 15-26, y))
  161.  
  162.     return grupa_boczne
  163.  
  164. def sprawdz(grupa, y):
  165.     for obj in grupa:
  166.         if obj.rect.y < y < obj.rect.y+44 or obj.rect.y < y+44 < obj.rect.y+44:
  167.             return True
  168.     return False
  169.  
  170.  
  171. ##kolec = Kolec(kolec_gora, 100, 70)
  172.  
  173. gracz = Gracz(plik)
  174. gracz.rect.left = SZEROKOŚĆ//2-26/2
  175. gracz.rect.bottom = 200
  176. gracz.ruch_x = 5
  177. aktualna_plansza = Plansza(gracz)
  178.  
  179. grupa_kolce = pygame.sprite.Group()
  180. grupa_boczne = pygame.sprite.Group()
  181.  
  182. for x in range(7):
  183. ##    kolec = Kolec(kolec_gora, 15*x+5, 20)
  184.     grupa_kolce.add(Kolec(kolec_gora, 50*x+26, 15))
  185.     grupa_kolce.add(Kolec(kolec_dol, 50*x+26, WYSOKOŚĆ-41))
  186.    
  187. ##grupa_boczna = losuj_prawo(grupa_boczna)
  188.    
  189.    
  190. # pętla gry
  191. while okno_otwarte:
  192.     # pętla zdarzeń
  193.     for event in pygame.event.get():
  194.         if event.type == pygame.QUIT:
  195.             okno_otwarte = False
  196.         else:
  197.             gracz.reakcja_na_zdarzenia(event)
  198.  
  199.     if gracz.rect.right > SZEROKOŚĆ-15:
  200.         gracz.lewo()
  201.         grupa_boczne = losuj_lewo(grupa_boczne)
  202.        
  203.     if gracz.rect.left < 16:
  204.         gracz.prawo()
  205.         grupa_boczne = losuj_prawo(grupa_boczne)
  206.  
  207.     if gracz.rect.bottom > WYSOKOŚĆ - 15-26:
  208.         okno_otwarte = False
  209.         gracz.stop()
  210.  
  211.     if gracz.rect.top < 15+26:
  212.         okno_otwarte = False
  213.  
  214.     for obj in grupa_boczne:
  215.         if pygame.sprite.collide_mask(gracz, obj) != None:
  216.             okno_otwarte = False
  217.    
  218.                    
  219.     aktualna_plansza.draw(ekran)
  220.     grupa_kolce.draw(ekran)
  221.     grupa_boczne.draw(ekran)
  222.     gracz.update()
  223.     gracz.draw(ekran)
  224.    
  225.    
  226.     pygame.display.flip()
  227.     zegar.tick(30)
  228.  
  229.  
  230. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement