python pygame tutorial coderslegacy

Solutions on MaxInterview for python pygame tutorial coderslegacy by the best coders in the world

showing results for - "python pygame tutorial coderslegacy"
Gaëlle
26 Sep 2018
1#Setting up FPS 
2FPS = 60
3FramePerSec = pygame.time.Clock()
4 
5#Creating colors
6BLUE  = (0, 0, 255)
7RED   = (255, 0, 0)
8GREEN = (0, 255, 0)
9BLACK = (0, 0, 0)
10WHITE = (255, 255, 255)
11 
12#Other Variables for use in the program
13SCREEN_WIDTH = 400
14SCREEN_HEIGHT = 600
15SPEED = 5
16SCORE = 0
17 
18#Setting up Fonts
19font = pygame.font.SysFont("Verdana", 60)
20font_small = pygame.font.SysFont("Verdana", 20)
21game_over = font.render("Game Over", True, BLACK)
22 
23background = pygame.image.load("Command Center/Lessons/AnimatedStreet.png")
24 
25#Create a white screen 
26DISPLAYSURF = pygame.display.set_mode((400,600))
27DISPLAYSURF.fill(WHITE)
28pygame.display.set_caption("Game")
29 
30class Enemy(pygame.sprite.Sprite):
31      def __init__(self):
32        super().__init__() 
33        self.image = pygame.image.load("Enemy.png")
34        self.surf = pygame.Surface((42, 70))
35        self.rect = self.surf.get_rect(center = (random.randint(40,SCREEN_WIDTH-40)
36                                                 , 0))
37 
38      def move(self):
39        global SCORE
40        self.rect.move_ip(0,SPEED)
41        if (self.rect.top > 600):
42            SCORE += 1
43            self.rect.top = 0
44            self.rect.center = (random.randint(40, SCREEN_WIDTH - 40), 0)
45 
46 
47class Player(pygame.sprite.Sprite):
48    def __init__(self):
49        super().__init__() 
50        self.image = pygame.image.load("Player.png")
51        self.surf = pygame.Surface((40, 75))
52        self.rect = self.surf.get_rect(center = (160, 520))
53        
54    def move(self):
55        pressed_keys = pygame.key.get_pressed()
56       #if pressed_keys[K_UP]:
57            #self.rect.move_ip(0, -5)
58       #if pressed_keys[K_DOWN]:
59            #self.rect.move_ip(0,5)
60         
61        if self.rect.left > 0:
62              if pressed_keys[K_LEFT]:
63                  self.rect.move_ip(-5, 0)
64        if self.rect.right < SCREEN_WIDTH:        
65              if pressed_keys[K_RIGHT]:
66                  self.rect.move_ip(5, 0)
67                   
68#Setting up Sprites        
69P1 = Player()
70E1 = Enemy()
71 
72#Creating Sprites Groups
73enemies = pygame.sprite.Group()
74enemies.add(E1)
75all_sprites = pygame.sprite.Group()
76all_sprites.add(P1)
77all_sprites.add(E1)
78 
79#Adding a new User event 
80INC_SPEED = pygame.USEREVENT + 1
81pygame.time.set_timer(INC_SPEED, 1000)
82 
83#Game Loop
84while True:
85       
86    #Cycles through all events occurring  
87    for event in pygame.event.get():
88        if event.type == INC_SPEED:
89              SPEED += 0.5     
90        if event.type == QUIT:
91            pygame.quit()
92            sys.exit()
93 
94    DISPLAYSURF.blit(background, (0,0))
95    scores = font_small.render(str(SCORE), True, BLACK)
96    DISPLAYSURF.blit(scores, (10,10))
97 
98    #Moves and Re-draws all Sprites
99    for entity in all_sprites:
100        DISPLAYSURF.blit(entity.image, entity.rect)
101        entity.move()
102 
103    #To be run if collision occurs between Player and Enemy
104    if pygame.sprite.spritecollideany(P1, enemies):
105          pygame.mixer.Sound('crash.wav').play()
106          time.sleep(0.5)
107                    
108          DISPLAYSURF.fill(RED)
109          DISPLAYSURF.blit(game_over, (30,250))
110           
111          pygame.display.update()
112          for entity in all_sprites:
113                entity.kill() 
114          time.sleep(2)
115          pygame.quit()
116          sys.exit()        
117         
118    pygame.display.update()
119    FramePerSec.tick(FPS)
120