Advertisement
Ridz112

Untitled

Dec 15th, 2020
45
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. # 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 alien and ship 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. # ------------------------------------------------------------------------------------------
  35.  
  36. # alien properties
  37.  
  38.  
  39. x = 350  # position of first alien
  40.  
  41. y = 20
  42.  
  43. alienList = []
  44.  
  45. count = 0
  46.  
  47. row = 0
  48.  
  49.  
  50.  
  51. # ------------------------------------------------------------------------------------------
  52.  
  53. # colours
  54.  
  55.  
  56. black = (0, 0, 0)
  57.  
  58. red = (200, 0, 0)
  59.  
  60. bright_red = (255, 0, 0)
  61.  
  62. white = (255, 255, 255)
  63.  
  64. green = (0, 200, 0)
  65.  
  66. bright_green = (0, 255, 0)
  67.  
  68.  
  69. # ------------------------------------------------------------------------------------------
  70.  
  71. # function to quit the game
  72.  
  73.  
  74. def quitgame():
  75.     quit()
  76.  
  77.  
  78. # ------------------------------------------------------------------------------------------
  79.  
  80. # function to create aliens
  81.  
  82.  
  83. def createAliens(count, row):
  84.     x = 350
  85.  
  86.     y = 100
  87.  
  88.     while count < 11 and row < 5:  # count = number of aliens, row = number of rows
  89.  
  90.         alien1 = Rect(x, y, 60, 60)
  91.  
  92.         alienList.append(alien1)
  93.  
  94.         x = x + 60  # distance of aliens on x-axis
  95.  
  96.         count = count + 1
  97.  
  98.         if count == 11:
  99.             count = 0
  100.  
  101.             y = y + 70  # distance of aliens on y-axis
  102.  
  103.             row = row + 1
  104.  
  105.             x = 350
  106.  
  107.     return alienList
  108.  
  109.  
  110. aliens = createAliens(count, row)  # calls function
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. # ------------------------------------------------------------------------------------------
  119.  
  120. # start of game and the entire game in one function
  121.  
  122. def game_loop():
  123.     global pause
  124.  
  125.     # ship properties
  126.     # spawn point
  127.  
  128.  
  129.     sx = 600
  130.  
  131.     sy = 615
  132.  
  133.     x_change = 0
  134.  
  135.     # create ship and its rect
  136.  
  137.     ship = Rect(sx, sy, 60, 60)
  138.  
  139.     gameOver = False
  140.  
  141.     score = 0
  142.  
  143.  
  144.     dx = 5  # direction
  145.  
  146.     dy = 0
  147.  
  148.     while gameOver == False:
  149.  
  150.  
  151.         def player (sx,sy):
  152.             screen.blit(shipImage, (sx, sy))
  153.  
  154.         # ------------------------------------------------------------------------------------------
  155.  
  156.         # Button inputs
  157.  
  158.         for e in event.get():
  159.  
  160.             if e.type == constants.QUIT:  # to quit game (close tab)
  161.  
  162.                 gameOver = True
  163.  
  164.             if e.type == KEYDOWN:  # to pause game (p)
  165.  
  166.                 if e.key == K_p:
  167.                     pause = True
  168.  
  169.                     paused()
  170.  
  171.                 if e.key == K_RIGHT:
  172.                     x_change = 8
  173.  
  174.                 if e.key == K_LEFT:
  175.                     x_change = -8
  176.  
  177.  
  178.             if e.type == KEYUP:
  179.                 if e.key == K_RIGHT:
  180.                     x_change = 0
  181.  
  182.                 if e.key == K_LEFT:
  183.                     x_change = 0
  184.  
  185.  
  186.         screen.fill((0, 0, 0))
  187.         # ------------------------------------------------------------------------------------------
  188.         # Ship movement
  189.         sx = sx + x_change
  190.  
  191.         player(sx,sy)
  192.         if sx > width - 60:
  193.             sx = width - 60
  194.         if sx < 0:
  195.             sx = 0
  196.  
  197.         # ------------------------------------------------------------------------------------------
  198.  
  199.         # draw aliens
  200.  
  201.         for a in alienList:
  202.             screen.blit(alienImage, a)
  203.  
  204.  
  205.         # ------------------------------------------------------------------------------------------
  206.  
  207.         # moving aliens
  208.  
  209.         for alien in alienList:
  210.  
  211.             alien.move_ip(dx, dy)
  212.  
  213.             if alien.x + alien.w >= 1200 or alien.x <= 0:
  214.  
  215.                 # alien.x is the left side of image and alien.w is the width of image
  216.  
  217.                 dx = dx * -1
  218.  
  219.                 # moves alien down
  220.  
  221.                 for alien in alienList:
  222.                     dy = 5
  223.  
  224.                     alien.move_ip(dx, dy)
  225.  
  226.                 dy = 0
  227.  
  228.             alien.move_ip(dx, 0)
  229.  
  230.             display.flip()
  231.         clock.tick(60)
  232.  
  233.  
  234. # ------------------------------------------------------------------------------------------
  235.  
  236.  
  237. # Functions .
  238.  
  239. def text_objects(text, font):
  240.     textSurface = font.render(text, True, black)
  241.  
  242.     return textSurface, textSurface.get_rect()
  243.  
  244.  
  245. def message_display(text):
  246.     largeText = pygame.font.Font('freesansbold.ttf', 115)
  247.  
  248.     TextSurf, TextRect = text_objects(text, largeText)
  249.  
  250.     TextRect.center = ((width / 2), (height / 2))
  251.  
  252.     screen.blit(TextSurface, TextRect)
  253.  
  254.     screen.update()
  255.  
  256.  
  257. def things(thingx, thingy, thingw, thingh, colour):
  258.     draw.rect(screen, colour, [thingx, thingy, thingw, thingh])
  259.  
  260.  
  261. def button(msg, x, y, w, h, ic, ac, action=None):
  262.     m = mouse.get_pos()
  263.  
  264.     click = mouse.get_pressed()
  265.  
  266.     if x + w > m[0] > x and y + h > m[1] > y:
  267.  
  268.         draw.rect(screen, ac, (x, y, w, h))
  269.  
  270.         if click[0] == 1 and action != None:
  271.  
  272.             action()
  273.  
  274.             if action == "play":
  275.  
  276.                 game_loop()
  277.  
  278.  
  279.  
  280.             elif action == "quit":
  281.  
  282.                 quit()
  283.  
  284.     else:
  285.  
  286.         draw.rect(screen, ic, (x, y, w, h))
  287.  
  288.     smallText = font.Font("freesansbold.ttf", 20)
  289.  
  290.     textSurf, textRect = text_objects(msg, smallText)
  291.  
  292.     textRect.center = ((x + (w / 2)), (y + (h / 2)))
  293.  
  294.     screen.blit(textSurf, textRect)
  295.  
  296.  
  297. pause = False
  298.  
  299.  
  300. def unpause():
  301.     global pause
  302.  
  303.     pause = False
  304.  
  305.  
  306. # ------------------------------------------------------------------------------------------
  307.  
  308.  
  309. # pause function - credit to sentdex for tutorial
  310.  
  311.  
  312. def paused():
  313.     pause = True
  314.  
  315.     while pause:
  316.  
  317.         for e in event.get():
  318.  
  319.             if e.type == QUIT:
  320.                 quitgame()
  321.  
  322.         screen.fill((white))
  323.  
  324.         largeText = font.Font('freesansbold.ttf', 115)
  325.  
  326.         TextSurf, TextRect = text_objects("PAUSED", largeText)
  327.  
  328.         TextRect.center = ((width / 2), (height / 2))
  329.  
  330.         screen.blit(TextSurf, TextRect)
  331.  
  332.         button("CONTINUE?", 150, 450, 150, 50, green, bright_green, game_loop)
  333.  
  334.         button("QUIT", 850, 450, 100, 50, red, bright_red, "quit")
  335.  
  336.         display.update()
  337.  
  338.         clock.tick(15)
  339.  
  340.     # ------------------------------------------------------------------------------------------
  341.  
  342.  
  343. # Credit to Harsh for helping me with this , and credit to sentdex for tutorial
  344.  
  345.  
  346. def game_intro():
  347.     intro = True
  348.  
  349.     while intro:
  350.  
  351.         for e in event.get():
  352.  
  353.             if e.type == QUIT:
  354.                 quitgame()
  355.  
  356.         screen.fill((white))
  357.  
  358.         largeText = font.Font('freesansbold.ttf', 115)
  359.  
  360.         TextSurf, TextRect = text_objects("Skerritt Invaders", largeText)
  361.  
  362.         TextRect.center = ((width / 2), (height / 2))
  363.  
  364.         screen.blit(TextSurf, TextRect)
  365.  
  366.         button("PLAY", 150, 450, 100, 50, green, bright_green, game_loop)
  367.  
  368.         button("QUIT", 850, 450, 100, 50, red, bright_red, quit)
  369.  
  370.         display.update()
  371.  
  372.         clock.tick(15)
  373.  
  374.  
  375. game_intro()
  376.  
  377.  
  378.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement