Advertisement
Ridz112

Untitled

Jan 6th, 2021
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pygame import *
  2.  
  3. init()
  4.  
  5.  
  6.  
  7. # ------------------------------------------------------------------------------------------
  8.  
  9. # set up code
  10.  
  11.  
  12. # Set up screen
  13.  
  14.  
  15. width = 1200
  16.  
  17. height = 700
  18.  
  19. screen = display.set_mode((width, height))
  20.  
  21. clock = time.Clock()
  22.  
  23. # ------------------------------------------------------------------------------------------
  24.  
  25. # load pictures and scale it!
  26.  
  27.  
  28. shipImage = image.load("ship.xcf")
  29.  
  30. shipImage = transform.scale(shipImage, (60, 60))
  31.  
  32. alienImage = image.load("alien.jpeg")
  33.  
  34. alienImage = transform.scale(alienImage, (60, 60))
  35.  
  36. bulletImage = image.load("bullet.xcf")
  37.  
  38. bulletImage = transform.scale(bulletImage, (40, 50))
  39.  
  40. blockImage = image.load("block.png")
  41.  
  42. blockImage = transform.scale(blockImage,(50,3))
  43.  
  44. # ------------------------------------------------------------------------------------------
  45.  
  46. # alien properties
  47.  
  48.  
  49. x = 350  # position of first alien
  50.  
  51. y = 20
  52.  
  53. alienList = []
  54.  
  55. count = 0
  56.  
  57. row = 0
  58.  
  59. floor = []
  60.  
  61. floorcount = 0
  62.  
  63. pause = False
  64.  
  65. # ------------------------------------------------------------------------------------------
  66.  
  67. # colours
  68.  
  69.  
  70. black = (0, 0, 0)
  71.  
  72. red = (200, 0, 0)
  73.  
  74. bright_red = (255, 0, 0)
  75.  
  76. white = (255, 255, 255)
  77.  
  78. green = (0, 200, 0)
  79.  
  80. bright_green = (0, 255, 0)
  81.  
  82.  
  83. # ------------------------------------------------------------------------------------------
  84. # Functions - for text
  85.  
  86. def text_objects(text, font):
  87.     textSurface = font.render(text, True, black)
  88.  
  89.     return textSurface, textSurface.get_rect()
  90.  
  91.  
  92. def message_display(text):
  93.     largeText = pygame.font.Font('freesansbold.ttf', 115)
  94.  
  95.     TextSurf, TextRect = text_objects(text, largeText)
  96.  
  97.     TextRect.center = ((width / 2), (height / 2))
  98.  
  99.     screen.blit(TextSurface, TextRect)
  100.  
  101.     screen.update()
  102.  
  103.  
  104.  
  105. def things(thingx, thingy, thingw, thingh, colour):
  106.     draw.rect(screen, colour, [thingx, thingy, thingw, thingh])
  107.  
  108. # ----------------------------------------------------------------
  109. # Function - button
  110.  
  111. def button(msg, x, y, w, h, ic, ac, action=None):
  112.     m = mouse.get_pos()
  113.  
  114.     click = mouse.get_pressed()
  115.  
  116.     if x + w > m[0] > x and y + h > m[1] > y:
  117.  
  118.         draw.rect(screen, ac, (x, y, w, h))
  119.  
  120.         if click[0] == 1 and action != None:
  121.  
  122.             action()
  123.  
  124.             if action == "play":
  125.  
  126.                 game_loop()
  127.  
  128.  
  129.  
  130.             elif action == "quit":
  131.  
  132.                 quit()
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.     else:
  141.  
  142.         draw.rect(screen, ic, (x, y, w, h))
  143.  
  144.     smallText = font.Font("freesansbold.ttf", 20)
  145.  
  146.     textSurf, textRect = text_objects(msg, smallText)
  147.  
  148.     textRect.center = ((x + (w / 2)), (y + (h / 2)))
  149.  
  150.     screen.blit(textSurf, textRect)
  151.  
  152. #----------------------------------------------------------------
  153.  
  154. # Functions - pause/ un pause, credit to sentdex for tutorial
  155.  
  156. pause = False
  157.  
  158.  
  159. def unpause():
  160.     global pause
  161.  
  162.     pause = False
  163.  
  164.  
  165. def paused():
  166.  
  167.  
  168.     while pause:
  169.  
  170.         for e in event.get():
  171.  
  172.             if e.type == QUIT:
  173.                 quitgame()
  174.  
  175.         # screen.fill((white))
  176.  
  177.         pause_label = main_font2.render("PAUSED", 1, (white))
  178.  
  179.         screen.blit(pause_label, (300, 250))
  180.  
  181.         button("CONTINUE?", 150, 450, 150, 50, green, bright_green, unpause)
  182.  
  183.         button("QUIT", 850, 450, 100, 50, red, bright_red, "quit")
  184.  
  185.         display.update()
  186.  
  187.  
  188.  
  189. # ------------------------------------------------------------------------------------------
  190.  
  191.  
  192. #  Function for main menu, Credit to Harsh for helping me with this , and credit to sentdex for tutorial
  193.  
  194.  
  195. def game_intro():
  196.     intro = True
  197.  
  198.     while intro:
  199.  
  200.         for e in event.get():
  201.  
  202.             if e.type == QUIT:
  203.                 quitgame()
  204.  
  205.         screen.fill((white))
  206.  
  207.         main_font = font.SysFont("comicsans", 40)
  208.  
  209.         largeText = font.Font('freesansbold.ttf', 115)
  210.  
  211.  
  212.         TextSurf, TextRect = text_objects("Skerritt Invaders", largeText)
  213.         TextRect.center = ((width / 2), (height / 2))
  214.         screen.blit(TextSurf, TextRect)
  215.  
  216.  
  217.         TextSurf, TextRect = text_objects("Controls:", main_font)
  218.         TextRect.center = (200, 525)
  219.         screen.blit(TextSurf, TextRect)
  220.  
  221.         TextSurf, TextRect = text_objects("P - Pause", main_font)
  222.         TextRect.center = (200, 575)
  223.         screen.blit(TextSurf, TextRect)
  224.  
  225.         TextSurf, TextRect = text_objects("Space - Shoot", main_font)
  226.         TextRect.center = (200, 625)
  227.         screen.blit(TextSurf, TextRect)
  228.  
  229.         TextSurf, TextRect = text_objects("Left/Right arrow keys - move", main_font)
  230.         TextRect.center = (200, 675)
  231.         screen.blit(TextSurf, TextRect)
  232.  
  233.         TextSurf, TextRect = text_objects("Rules:", main_font)
  234.         TextRect.center = (800, 525)
  235.         screen.blit(TextSurf, TextRect)
  236.  
  237.         TextSurf, TextRect = text_objects("Shoot all Skerritts to move on to next wave", main_font)
  238.         TextRect.center = (800, 575)
  239.         screen.blit(TextSurf, TextRect)
  240.  
  241.         TextSurf, TextRect = text_objects("Get shot or aliens reach you, you lose a life", main_font)
  242.         TextRect.center = (800, 625)
  243.         screen.blit(TextSurf, TextRect)
  244.  
  245.         TextSurf, TextRect = text_objects("Lose all lives and it's gameover", main_font)
  246.         TextRect.center = (800, 675)
  247.         screen.blit(TextSurf, TextRect)
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.         button("PLAY", 150, 450, 100, 50, green, bright_green, game_loop)
  255.  
  256.         button("QUIT", 850, 450, 100, 50, red, bright_red, quit)
  257.  
  258.         display.update()
  259.  
  260.         clock.tick(60)
  261.  
  262. # function to quit the game
  263.  
  264.  
  265. def quitgame():
  266.     quit()
  267.  
  268.  
  269. # ------------------------------------------------------------------------------------------
  270. # function to create the floor
  271. def createfloor(floorcount):
  272.     fx = 10
  273.     fy = 670
  274.  
  275.     while floorcount < 200:
  276.         floor1 = Rect(fx,fy,50,3)
  277.         floor.append(floor1)
  278.         fx +=20
  279.         floorcount += 1
  280.     return floor
  281.  
  282. floors = createfloor(floorcount)
  283.  
  284. # ------------------------------------------------------------------------------------------
  285. # function to create aliens, Gabriel G made this
  286.  
  287. def createAliens(count, row):
  288.     x = 350
  289.  
  290.     y = 100
  291.  
  292.     while count < 5 and row < 1:  # count = number of aliens, row = number of rows
  293.  
  294.         alien1 = Rect(x, y, 60, 60)
  295.  
  296.         alienList.append(alien1)
  297.  
  298.         x = x + 60  # distance of aliens on x-axis
  299.  
  300.         count = count + 1
  301.  
  302.         if count == 11:
  303.             count = 0
  304.  
  305.             y = y + 70  # distance of aliens on y-axis
  306.  
  307.             row = row + 1
  308.  
  309.             x = 350
  310.  
  311.     return alienList
  312.  
  313.  
  314. aliens = createAliens(count, row)  # calls function
  315.  
  316. main_font = font.SysFont("comicsans", 40)
  317. main_font2 = font.SysFont("comicsans", 200)
  318. # ------------------------------------------------------------------------------------------
  319. # function to create bullets
  320.  
  321. def createbullets(bullets,x,y):
  322.     bullet = Rect(sx,sy+10,15,40)
  323.     bullets.append(bullet)
  324.     return bullets
  325.  
  326. bullets = []
  327.  
  328. gameOver = False
  329.  
  330. waves = 100
  331. dx = waves  # direction
  332. dy = 0
  333.  
  334. score = 0
  335. lives = 3
  336. # ------------------------------------------------------------------------------------------
  337. # Function for a game over, when you lose all lives.
  338. def game_over():
  339.  
  340.     screen.fill(bright_red)
  341.  
  342.     gameover_label = main_font2.render("GAME OVER", 1, (white))
  343.  
  344.     screen.blit(gameover_label, (250, 250))
  345.  
  346.     while True:
  347.  
  348.         for e in event.get():
  349.  
  350.             if e.type == QUIT:
  351.                 quitgame()
  352.  
  353.         button("PLAY AGAIN?", 150, 450, 150, 50, green, bright_green, game_loop)
  354.  
  355.         button("QUIT", 850, 450, 100, 50, red, bright_red, "quit")
  356.  
  357.         display.update()
  358.  
  359.         clock.tick(15)
  360. # ------------------------------------------------------------------------------------------
  361.  
  362. # start of game and the entire game in one function
  363.  
  364. def game_loop():
  365.     global pause
  366.  
  367.  
  368. # ship properties
  369.     sx = 600
  370.  
  371.     sy = 605
  372.  
  373.     x_change = 0
  374.  
  375.     ship = Rect(sx,sy,60,60)
  376.  
  377. # function to create bullets, needed here again otherwise it breaks
  378.  
  379.     def createbullets(bullets,x,y):
  380.         bullet = Rect(sx,sy+10,15,40)
  381.         bullets.append(bullet)
  382.         return bullets
  383.  
  384.     bullets = []
  385.  
  386.     gameOver = False
  387.  
  388.     waves = 1
  389.     dx = waves  # direction
  390.     dy = 0
  391.  
  392.     score = 0
  393.     lives = 3
  394.  
  395.     while gameOver == False:
  396.  
  397.         # ------------------------------------------------------------------------------------------
  398.  
  399.         # Button inputs
  400.  
  401.         for e in event.get():
  402.  
  403.             if e.type == constants.QUIT:  # to quit game (close tab)
  404.  
  405.                 gameOver = True
  406.  
  407.             if e.type == KEYDOWN:  # to pause game (p)
  408.  
  409.                 if e.key == K_p:
  410.                     pause = True
  411.  
  412.                     paused()
  413.  
  414.                 if e.key == K_RIGHT:
  415.                     x_change = 30
  416.  
  417.                 if e.key == K_LEFT:
  418.                     x_change = -30
  419.  
  420.                 if e.key == K_SPACE:
  421.                     createbullets(bullets,x,y)
  422.  
  423.  
  424.             if e.type == KEYUP:
  425.                 if e.key == K_RIGHT:
  426.                     x_change = 0
  427.  
  428.                 if e.key == K_LEFT:
  429.                     x_change = 0
  430.  
  431.  
  432.         screen.fill((0, 0, 0))
  433.         score_label = main_font.render(f"SCORE: {score}", 1, (white))
  434.         lives_label = main_font.render(f"LIVES: {lives}", 1, (white))
  435.         waves_label = main_font.render(f"WAVE: {waves}", 1, (white))
  436.  
  437.         # score
  438.  
  439.         screen.blit(score_label, (10, 10))
  440.  
  441.         # lives
  442.         screen.blit(lives_label, (10, 675))
  443.  
  444.         # waves
  445.         screen.blit(waves_label, (500, 10))
  446.  
  447. # ship movement
  448.  
  449.  
  450.         sx = sx + x_change
  451.         if sx > width - 60:
  452.             sx = width - 60
  453.  
  454.         if sx < 0:
  455.             sx = 0
  456.  
  457.  
  458.  
  459.             # ------------------------------------------------------------------------------------------
  460.  
  461.         for f in floor:
  462.             screen.blit(blockImage, f)
  463.  
  464.         # draw aliens
  465.  
  466.         for a in alienList:
  467.             screen.blit(alienImage, a)
  468.  
  469.         # Draw ship
  470.  
  471.         screen.blit(shipImage, (sx, sy))
  472.  
  473.  
  474. # create bullet
  475.         for b in bullets:
  476.             screen.blit(bulletImage, b)
  477. # move bullet
  478.         for b in bullets:
  479.             b.move_ip(0,-50)
  480.             if b.y <= 0:
  481.                 bullets.remove(b)
  482.  
  483.  
  484.  
  485.         # ------------------------------------------------------------------------------------------
  486.  
  487.         # moving aliens
  488.  
  489.         for alien in alienList:
  490.  
  491.             alien.move_ip(dx, dy)
  492.  
  493.             if alien.x + alien.w >= 1200 or alien.x <= 0:
  494.  
  495.                 # alien.x is the left side of image and alien.w is the width of image
  496.  
  497.                 dx = dx * -1
  498.  
  499.                 # moves alien down
  500.  
  501.                 for alien in alienList:
  502.                     dy = (waves * 5)
  503.  
  504.                     alien.move_ip(dx, dy)
  505.  
  506.                 dy = 0
  507.  
  508.             alien.move_ip(dx, 0)
  509.  
  510.  
  511. # bullet to alien hit detection
  512.  
  513.             for alien in alienList:
  514.                 for bullet in bullets:
  515.                     if bullet.colliderect(alien):
  516.                         bullets.remove(bullet)
  517.                         alienList.remove(alien)
  518.                         score = score + 10
  519.  
  520.  
  521.             if len(alienList) == 0:
  522.                 waves = waves + 1
  523.                 aliens = createAliens(count, row)
  524.                 dx = waves
  525.  
  526.  
  527.             # gameover when alien hits player
  528.  
  529.             for alien in alienList:
  530.                 if alien.colliderect(ship):
  531.                     game_over()
  532.  
  533.  
  534.  
  535.             display.flip()
  536.             clock.tick(60)
  537.         # -----------------------------------------------------------------------------
  538.  
  539.     # ------------------------------------------------------------------------------------------
  540.  
  541.  
  542.  
  543.  
  544. game_intro()
  545.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement