Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pygame import *
- init()
- # ------------------------------------------------------------------------------------------
- # set up code
- # Set up screen
- width = 1200
- height = 700
- screen = display.set_mode((width, height))
- clock = time.Clock()
- # ------------------------------------------------------------------------------------------
- # load pictures and scale it!
- shipImage = image.load("ship.xcf")
- shipImage = transform.scale(shipImage, (60, 60))
- alienImage = image.load("alien.jpeg")
- alienImage = transform.scale(alienImage, (60, 60))
- bulletImage = image.load("bullet.xcf")
- bulletImage = transform.scale(bulletImage, (25, 30))
- blockImage = image.load("block.xcf")
- blockImage = transform.scale(blockImage,(50,3))
- # ------------------------------------------------------------------------------------------
- # alien properties
- x = 350 # position of first alien
- y = 20
- alienList = []
- count = 0
- row = 0
- floor = []
- floorcount = 0
- pause = False
- # ------------------------------------------------------------------------------------------
- # colours
- black = (0, 0, 0)
- red = (200, 0, 0)
- bright_red = (255, 0, 0)
- white = (255, 255, 255)
- green = (0, 200, 0)
- bright_green = (0, 255, 0)
- game_green = (94, 255, 0)
- # ------------------------------------------------------------------------------------------
- # Functions - for text
- def text_objects(text, font):
- textSurface = font.render(text, True, black)
- return textSurface, textSurface.get_rect()
- def message_display(text):
- largeText = pygame.font.Font('freesansbold.ttf', 115)
- TextSurf, TextRect = text_objects(text, largeText)
- TextRect.center = ((width / 2), (height / 2))
- screen.blit(TextSurface, TextRect)
- screen.update()
- def things(thingx, thingy, thingw, thingh, colour):
- draw.rect(screen, colour, [thingx, thingy, thingw, thingh])
- # ----------------------------------------------------------------
- # Function - button
- def button(msg, x, y, w, h, ic, ac, action=None):
- m = mouse.get_pos()
- click = mouse.get_pressed()
- if x + w > m[0] > x and y + h > m[1] > y:
- draw.rect(screen, ac, (x, y, w, h))
- if click[0] == 1 and action != None:
- action()
- if action == "play":
- game_loop()
- elif action == "quit":
- quit()
- else:
- draw.rect(screen, ic, (x, y, w, h))
- smallText = font.Font("freesansbold.ttf", 20)
- textSurf, textRect = text_objects(msg, smallText)
- textRect.center = ((x + (w / 2)), (y + (h / 2)))
- screen.blit(textSurf, textRect)
- #----------------------------------------------------------------
- # Functions - pause/ un pause, credit to sentdex for tutorial
- pause = False
- def unpause():
- global pause
- pause = False
- def paused():
- while pause:
- for e in event.get():
- if e.type == QUIT:
- quitgame()
- # screen.fill((white))
- pause_label = main_font2.render("PAUSED", 1, (white))
- screen.blit(pause_label, (300, 250))
- button("CONTINUE?", 150, 450, 150, 50, green, bright_green, unpause)
- button("QUIT", 850, 450, 100, 50, red, bright_red, "quit")
- display.update()
- # ------------------------------------------------------------------------------------------
- # Function for main menu, Credit to Harsh for helping me with this , and credit to sentdex for tutorial
- def game_intro():
- intro = True
- while intro:
- for e in event.get():
- if e.type == QUIT:
- quitgame()
- screen.fill((white))
- main_font = font.SysFont("comicsans", 40)
- largeText = font.Font('freesansbold.ttf', 115)
- TextSurf, TextRect = text_objects("Skerritt Invaders", largeText)
- TextRect.center = ((width / 2), (height / 2))
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Controls:", main_font)
- TextRect.center = (200, 525)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("P - Pause", main_font)
- TextRect.center = (200, 575)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Space - Shoot", main_font)
- TextRect.center = (200, 625)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Left/Right arrow keys - move", main_font)
- TextRect.center = (200, 675)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Rules:", main_font)
- TextRect.center = (800, 525)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Shoot all Skerritts to move on to next wave", main_font)
- TextRect.center = (800, 575)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Get shot or aliens reach you, you lose a life", main_font)
- TextRect.center = (800, 625)
- screen.blit(TextSurf, TextRect)
- TextSurf, TextRect = text_objects("Lose all lives and it's gameover", main_font)
- TextRect.center = (800, 675)
- screen.blit(TextSurf, TextRect)
- button("PLAY", 150, 450, 100, 50, green, bright_green, game_loop)
- button("QUIT", 850, 450, 100, 50, red, bright_red, quit)
- display.update()
- clock.tick(60)
- # function to quit the game
- def quitgame():
- quit()
- # ------------------------------------------------------------------------------------------
- # function to create the floor
- def createfloor(floorcount):
- fx = 10
- fy = 670
- while floorcount < 200:
- floor1 = Rect(fx,fy,50,3)
- floor.append(floor1)
- fx +=20
- floorcount += 1
- return floor
- floors = createfloor(floorcount)
- # ------------------------------------------------------------------------------------------
- # function to create aliens, Gabriel G made this
- def createAliens(count, row):
- x = 350
- y = 100
- while count < 5 and row < 1: # count = number of aliens, row = number of rows
- alien1 = Rect(x, y, 60, 60)
- alienList.append(alien1)
- x = x + 60 # distance of aliens on x-axis
- count = count + 1
- if count == 11:
- count = 0
- y = y + 70 # distance of aliens on y-axis
- row = row + 1
- x = 350
- return alienList
- aliens = createAliens(count, row) # calls function
- main_font = font.SysFont("comicsans", 40)
- main_font2 = font.SysFont("comicsans", 200)
- # ------------------------------------------------------------------------------------------
- # function to create bullets
- def createbullets(bullets,x,y):
- bullet = Rect(sx,sy+10,15,40)
- bullets.append(bullet)
- return bullets
- bullets = []
- gameOver = False
- waves = 1
- dx = waves # direction
- dy = 0
- score = 0
- lives = 3
- # ------------------------------------------------------------------------------------------
- # Function for a game over, when you lose all lives.
- def game_over():
- screen.fill(bright_red)
- gameover_label = main_font2.render("GAME OVER", 1, (white))
- screen.blit(gameover_label, (250, 250))
- while True:
- for e in event.get():
- if e.type == QUIT:
- quitgame()
- button("PLAY AGAIN?", 150, 450, 150, 50, green, bright_green, game_loop)
- button("QUIT", 850, 450, 100, 50, red, bright_red, "quit")
- display.update()
- clock.tick(15)
- # ------------------------------------------------------------------------------------------
- # start of game and the entire game in one function
- def game_loop():
- global pause
- # ship properties
- sx = 600
- sy = 605
- x_change = 0
- ship = Rect(sx,sy,60,60)
- # function to create bullets, needed here again otherwise it breaks
- def createbullets(bullets,x,y):
- bullet = Rect(sx,sy+10,15,40)
- bullets.append(bullet)
- return bullets
- bullets = []
- gameOver = False
- waves = 1
- dx = waves # direction
- dy = 0
- score = 0
- lives = 3
- while gameOver == False:
- # ------------------------------------------------------------------------------------------
- # Button inputs
- for e in event.get():
- if e.type == constants.QUIT: # to quit game (close tab)
- gameOver = True
- if e.type == KEYDOWN: # to pause game (p)
- if e.key == K_p:
- pause = True
- paused()
- if e.key == K_RIGHT:
- x_change = 30
- if e.key == K_LEFT:
- x_change = -30
- if e.key == K_SPACE:
- createbullets(bullets,x,y)
- if e.type == KEYUP:
- if e.key == K_RIGHT:
- x_change = 0
- if e.key == K_LEFT:
- x_change = 0
- screen.fill((0, 0, 0))
- score_label = main_font.render(f"SCORE: {score}", 1, (white))
- lives_label = main_font.render(f"LIVES: {lives}", 1, (white))
- waves_label = main_font.render(f"WAVE: {waves}", 1, (white))
- # score
- screen.blit(score_label, (10, 10))
- # lives
- screen.blit(lives_label, (10, 675))
- # waves
- screen.blit(waves_label, (500, 10))
- # ship movement
- sx = sx + x_change
- if sx > width - 60:
- sx = width - 60
- if sx < 0:
- sx = 0
- # ------------------------------------------------------------------------------------------
- for f in floor:
- screen.blit(blockImage, f)
- # draw aliens
- for a in alienList:
- screen.blit(alienImage, a)
- # Draw ship
- screen.blit(shipImage, (sx, sy))
- # create bullet
- for b in bullets:
- screen.blit(bulletImage, b)
- # move bullet
- for b in bullets:
- b.move_ip(0,-50)
- if b.y <= 0:
- bullets.remove(b)
- # ------------------------------------------------------------------------------------------
- # moving aliens
- for alien in alienList:
- alien.move_ip(dx, dy)
- if alien.x + alien.w >= 1200 or alien.x <= 0:
- # alien.x is the left side of image and alien.w is the width of image
- dx = dx * -1
- # moves alien down
- for alien in alienList:
- dy = (waves * 5)
- alien.move_ip(dx, dy)
- dy = 0
- alien.move_ip(dx, 0)
- # bullet to alien hit detection
- for alien in alienList:
- for bullet in bullets:
- if bullet.colliderect(alien):
- bullets.remove(bullet)
- alienList.remove(alien)
- score = score + 10
- for floors in floor:
- for bullet in bullets:
- if bullet.colliderect(floors):
- floor.remove(floors)
- if len(alienList) == 0:
- waves = waves + 1
- aliens = createAliens(count, row)
- dx = waves
- # gameover when alien hits player
- for alien in alienList:
- if alien.colliderect(ship):
- game_over()
- display.flip()
- clock.tick(60)
- # -----------------------------------------------------------------------------
- # ------------------------------------------------------------------------------------------
- game_intro()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement