Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Libraries
- import pygame
- import time
- import random
- pygame.init()
- display_width=600
- display_height=400
- # RGB colors
- black=(0,0,0)
- white=(255,255,255)
- red=(255,0,0)
- green=(0,255,0)
- blue=(0,0,255)
- laser_color=(34,255,0)
- stormtrooper_width=61
- # Game display windows
- gameDisplay = pygame.display.set_mode((display_width,display_height))
- # Title
- pygame.display.set_caption("Stormtrooper Dodging Lasers")
- # Clock
- clock = pygame.time.Clock()
- # Image of stormtrooper
- StormtrooperImg = pygame.image.load("Stormtrooper.png")
- # Displays scoreboard
- def things_dodged(count):
- font = pygame.font.SysFont(None,40)
- text = font.render("Score "+str(count),True,black)
- gameDisplay.blit(text,(20,20))
- # Displays the stormtrooper
- def stormtrooper(x,y):
- gameDisplay.blit(StormtrooperImg,(x,y))
- # Displays lasers
- def things(thingx,thingy,thingw,thingh,color):
- color = laser_color
- pygame.draw.rect(gameDisplay,color,[thingx,thingy,thingw,thingh])
- # Creates text
- def text_objects(text,font):
- textSurface=font.render(text, True, red)
- return textSurface, textSurface.get_rect()
- # Displays text
- def message_display(text):
- largeText = pygame.font.Font('freesansbold.ttf',35)
- TextSurf, TextRect = text_objects(text, largeText)
- TextRect.center = ((display_width/2),(display_height/2))
- gameDisplay.blit(TextSurf, TextRect)
- pygame.display.update()
- time.sleep(2)
- game_loop()
- # Displays "You Crashed"
- def crash():
- message_display('You Died. The Empire Expected More of You.')
- def game_loop():
- x=(display_width*0.48)
- y=(display_height*0.79)
- x_change=0
- y_change=0
- thing_startx=random.randrange(0,display_width)
- thing_starty=-600
- thing_speed=5
- thing_width=5
- thing_height=100
- dodged=0
- gameExit=False
- while not gameExit:
- for event in pygame.event.get():
- if event.type==pygame.QUIT:
- pygame.quit()
- quit()
- if event.type==pygame.KEYDOWN:
- if event.key==pygame.K_LEFT:
- x_change=-5
- elif event.key==pygame.K_RIGHT:
- x_change=5
- elif event.key==pygame.K_DOWN:
- y_change=5
- elif event.key==pygame.K_UP:
- y_change=-5
- if event.type==pygame.KEYUP:
- if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
- x_change=0
- elif event.key==pygame.K_UP or event.key==pygame.K_DOWN:
- y_change=0
- x+=x_change
- y+=y_change
- gameDisplay.fill(white)
- #things(thingx,thingy,thingw,thingh,color)
- things(thing_startx,thing_starty,thing_width,thing_height,black)
- thing_starty+=thing_speed
- stormtrooper(x,y)
- things_dodged(dodged)
- if x>display_width-stormtrooper_width or x<0:
- crash()
- if thing_starty>display_height:
- thing_starty=0-thing_height
- thing_startx=random.randrange(0,display_width)
- dodged+=1
- if dodged % 2 == 0:
- thing_speed += 1
- if y<thing_starty+thing_height:
- print('y crossover')
- if x>thing_startx and x<thing_startx+thing_width or x+stormtrooper_width>thing_startx and x+stormtrooper_width<thing_startx+thing_width:
- print('x crossover')
- crash()
- pygame.display.update()
- clock.tick(60)
- game_loop()
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement