Advertisement
Ridz112

Untitled

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