misiekii123

AlienDesktop

Nov 1st, 2021 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.35 KB | None | 0 0
  1. import sys
  2. from random import *
  3. from time import *
  4.  
  5. try:
  6.     import pygame
  7.     from pygame.locals import*
  8. except ModuleNotFoundError:
  9.     import pip
  10.     pip.main(['install','pygame'])
  11.     import pygame
  12.     from pygame.locals import*
  13.  
  14.  
  15. pygame.init()
  16.  
  17. # deklaracja zmiennych
  18.  
  19. SZEROKOSC = 1000
  20. WYSOKOSC = 700
  21. obcy_szer = 160
  22. obcy_wys = 250
  23. count = 0
  24. FPS = 75
  25. clock = pygame.time.Clock()
  26. x = 120
  27. y = 200
  28. celx = 0
  29. cely = 0
  30. obcy_xpoz = 800
  31. x_move = 10
  32. HP = 100
  33. upgrade = 1
  34. counter = 3
  35.  
  36. obcyimg = pygame.image.load("obcy.png")
  37. apteczkaimg = pygame.image.load("apteczka2.png")
  38.  
  39. icon = pygame.image.load("ikona.png")
  40. oknogry = pygame.display.set_mode((SZEROKOSC,WYSOKOSC),0,32)
  41. # oknogry = pygame.display.set_mode((0, 0), pygame.RESIZABLE)
  42. # pygame.RESIZABLE()
  43. pygame.display.set_caption("ALIEN")
  44. pygame.display.set_icon(icon)
  45.  
  46. #podstawowe funkcje
  47.  
  48.  
  49. def odliczanie():
  50.     global counter
  51.     global gra
  52.     while counter > 0:
  53.         counter -= 1
  54.         oknogry.fill((0, 0, 0))
  55.         fontObj = pygame.font.Font("DS-DIGIT.ttf", 50)
  56.         tekst = fontObj.render(str(counter), True, (255, 255, 255))
  57.         oknogry.blit(tekst, (SZEROKOSC / 2 - 100, WYSOKOSC / 2 - 25))
  58.         print(str(counter))
  59.         sleep(1)
  60.     if counter == 0:
  61.         gra = True
  62.  
  63. def tlo():
  64.     bg = pygame.image.load("mapa-deszcz_meteorytow.png")
  65.     oknogry.blit(bg, (0, 0))
  66.  
  67.  
  68. def postac():
  69.     img = pygame.image.load("postać_trzymajaca_bron.png")
  70.     oknogry.blit(img, (25, 100))
  71.  
  72.  
  73. def bron():
  74.     img = pygame.image.load("pistolet.png")
  75.     oknogry.blit(img, (100, 195))
  76.  
  77.  
  78. def naboj(x,y):
  79.     img = pygame.image.load("naboj.png")
  80.     oknogry.blit(img, (x, y))
  81.  
  82.  
  83. def punkty():
  84.     fontObj = pygame.font.Font("DS-DIGIT.ttf", 50)
  85.     tekst = fontObj.render("Score: " + str(count), True, (255, 255, 255))
  86.     oknogry.blit(tekst, (SZEROKOSC/2-80, 10))
  87.  
  88.  
  89. def hp():
  90.     global HP
  91.     fontObj = pygame.font.Font("DS-DIGIT.ttf", 50)
  92.     if HP > 20:
  93.         tekst = fontObj.render("HP: " + str(HP), True, (255, 255, 255))
  94.     else:
  95.         tekst = fontObj.render("HP: " + str(HP), True, (255, 0, 0))
  96.     oknogry.blit(tekst,(25,10))
  97.  
  98.  
  99. def wspolrzedne_klikniecia(x,y):
  100.     fontObj = pygame.font.Font("DS-DIGIT.ttf",25)
  101.     tekst = fontObj.render("X: " + str(x) + " " + "Y:" + str(y), True, (255,255,255))
  102.     oknogry.blit(tekst, (SZEROKOSC-150, 10))
  103.  
  104.  
  105. def celownik(x,y):
  106.     img = pygame.image.load("celownik.png")
  107.     oknogry.blit(img,(x-60,y-50))
  108.  
  109.  
  110. def obcy():
  111.     global obcy_xpoz
  112.     global x_move
  113.     global HP
  114.     global przegrana
  115.     global gra
  116.     oknogry.blit(obcyimg, (obcy_xpoz, 250))
  117.     obcy_xpoz -= x_move
  118.     if obcy_xpoz < 125:
  119.         obcy_xpoz = 800
  120.         HP -= 10
  121.         if HP == 0:
  122.             przegrana = True
  123.             gra = False
  124.  
  125.  
  126. def apteczka():
  127.     global HP
  128.     oknogry.blit(apteczkaimg, (SZEROKOSC/2-50,WYSOKOSC-90))
  129.  
  130.  
  131. def leczenie():
  132.     global HP
  133.     HP += 10
  134.  
  135. def upgrade_button():
  136.     img = pygame.image.load("upgrade.png")
  137.     oknogry.blit(img, (800,WYSOKOSC-110))
  138.  
  139. def upgrade_func():
  140.     global count
  141.     global upgrade
  142.     if count >= 50:
  143.         count -= 50
  144.         upgrade += 2
  145.  
  146. # główna pętla programu
  147.  
  148.  
  149. przegrana = False
  150. gra = False
  151. start = True
  152.  
  153. while start:
  154.     odliczanie()
  155.     gra = True
  156.  
  157. while gra:
  158.     tlo()
  159.     postac()
  160.     punkty()
  161.     hp()
  162.     naboj(x, y)
  163.     bron()
  164.     apteczka()
  165.     upgrade_button()
  166.  
  167.     for event in pygame.event.get():
  168.         if event.type == pygame.QUIT:
  169.             pygame.quit()
  170.             sys.exit()
  171.         if event.type == pygame.MOUSEMOTION:
  172.             myszaX, myszaY = event.pos
  173.             celx = myszaX
  174.             cely = myszaY
  175.         if event.type == pygame.MOUSEBUTTONDOWN:
  176.             if event.button == 1:
  177.                 mouseX, mouseY = event.pos
  178.                 x = mouseX
  179.                 y = mouseY
  180.                 if x > obcy_xpoz and x < obcy_xpoz + obcy_szer and y > 275 and y < 275 + obcy_wys:
  181.                     obcy_xpoz = 800
  182.                     count += upgrade
  183.                 if x > 450 and x < 550 and y > WYSOKOSC - 90 and y < WYSOKOSC and HP != 100:
  184.                     leczenie()
  185.                 if x > 800 and x < 996 and y > 590 and y < 590+140:
  186.                     upgrade_func()
  187.  
  188.         if event.type == pygame.MOUSEBUTTONUP:
  189.             if event.button == 1:
  190.                 x = 120
  191.                 y = 200
  192.  
  193.     obcy()
  194.  
  195.     celownik(celx, cely)
  196.  
  197.     wspolrzedne_klikniecia(x,y)
  198.     pygame.display.update()
  199.     clock.tick(FPS)
  200.  
  201. while przegrana:
  202.     oknogry.fill((0,0,0))
  203.     fontObj = pygame.font.Font("DS-DIGIT.ttf", 50)
  204.     tekst = fontObj.render("GAME OVER", True, (255, 255, 255))
  205.     oknogry.blit(tekst, (SZEROKOSC/2-100, WYSOKOSC/2-25))
  206.  
  207.     smallerfontObj = pygame.font.Font("DS-DIGIT.ttf", 25)
  208.     tekst2 = smallerfontObj.render("Score: " + str(count), True, (255,255,255))
  209.     oknogry.blit(tekst2, (SZEROKOSC/2-50, WYSOKOSC/2+25))
  210.  
  211.     instructionsfontObj = pygame.font.Font("DS-DIGIT.ttf", 25)
  212.     instructions = instructionsfontObj.render("Click to play again", True, (100,100,100))
  213.     oknogry.blit(instructions, (SZEROKOSC/2-100, WYSOKOSC/2+100))
  214.  
  215.     for event in pygame.event.get():
  216.         if event.type == pygame.QUIT:
  217.             pygame.quit()
  218.             sys.exit()
  219.         if event.type == pygame.MOUSEBUTTONDOWN:
  220.             if event.button == 1:
  221.                 HP = 100
  222.                 count = 0
  223.                 upgrade = 1
  224.                 gra = True
  225.                 przegrana = False
  226.                 while gra:
  227.                     tlo()
  228.                     postac()
  229.                     punkty()
  230.                     hp()
  231.                     naboj(x, y)
  232.                     bron()
  233.                     apteczka()
  234.                     upgrade_button()
  235.  
  236.                     for event in pygame.event.get():
  237.                         if event.type == pygame.QUIT:
  238.                             pygame.quit()
  239.                             sys.exit()
  240.                         if event.type == pygame.MOUSEMOTION:
  241.                             myszaX, myszaY = event.pos
  242.                             celx = myszaX
  243.                             cely = myszaY
  244.                         if event.type == pygame.MOUSEBUTTONDOWN:
  245.                             if event.button == 1:
  246.                                 mouseX, mouseY = event.pos
  247.                                 x = mouseX
  248.                                 y = mouseY
  249.                                 if x > obcy_xpoz and x < obcy_xpoz + obcy_szer and y > 275 and y < 275 + obcy_wys:
  250.                                     obcy_xpoz = 800
  251.                                     count += upgrade
  252.                                 if x > 450 and x < 550 and y > WYSOKOSC - 90 and y < WYSOKOSC and HP != 100:
  253.                                     leczenie()
  254.                                 if x > 800 and x < 996 and y > 590 and y < 590 + 140:
  255.                                     upgrade_func()
  256.  
  257.                         if event.type == pygame.MOUSEBUTTONUP:
  258.                             if event.button == 1:
  259.                                 x = 120
  260.                                 y = 200
  261.  
  262.                     obcy()
  263.  
  264.                     celownik(celx, cely)
  265.  
  266.                     wspolrzedne_klikniecia(x, y)
  267.                     pygame.display.update()
  268.                     clock.tick(FPS)
  269.  
  270.     pygame.display.update()
  271.     clock.tick(FPS)
  272.  
Add Comment
Please, Sign In to add comment