repl.it
@p_loo/

(Full) Swamp_Game Ask

Pygame

Functional first 2 levels of game --No end battle--

fork
loading
Files
  • main.py
  • pics
  • boss.py
  • character.py
  • nohup.out
  • second_level.py
  • straw.py
  • water.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pygame
import random
from water import Water
from character import Char
from second_level import level_2
pygame.init()

WHITE = (255,255,255)
BLACK = (0,0,0)

#set screen scale
screen_w = 800
screen_h = 600
size = (screen_w, screen_h)
screen = pygame.display.set_mode(size)

x = 30
y = 400

font_name = pygame.font.match_font('arial')

def score_text(surf, text, size, x, y, color):
    font = pygame.font.SysFont(font_name, size)
    text_surface = font.render(text, True, color)
    text_rect = text_surface.get_rect()
    text_rect.topleft = (x, y)
    surf.blit(text_surface, text_rect)

#load & scale images
field = pygame.image.load("pics/field.png")
field = pygame.transform.scale(field, (800,600))
field_rect = field.get_rect()

swamp = pygame.image.load("pics/swamp.png")
swamp = pygame.transform.scale(swamp, (800,600))
swamp_rect = swamp.get_rect()

fruit_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

score = 0

for i in range(10):
    # This represents a block
    fruit = Water(BLACK, 75, 85)
 
    # Set a random location for the block
    fruit.rect.x = random.randrange(screen_w)
    fruit.rect.y = random.randrange(screen_h)
 
    # Add the block to the list of objects
    fruit_list.add(fruit)
    all_sprites_list.add(fruit)

player = Char(BLACK, 30, 30)
all_sprites_list.add(player)

#set loop
carryOn = True
clock = pygame.time.Clock()

score = 0

# - - - - MAIN LOOP - - - - - - - - - -
while carryOn:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            carryOn = False

    if score < 20:
      screen.blit(field,(0,0))
      # Calls update() method on every sprite in the list
      all_sprites_list.update()
  
      keys = pygame.key.get_pressed()
      if keys[pygame.K_LEFT]:
          player.moveLeft(20)
      if keys[pygame.K_RIGHT]:
          player.moveRight(20)
          
      # See if the player block has collided with anything.
      fruit_hit_list = pygame.sprite.spritecollide(player, fruit_list, False)
      
      # Check the list of collisions.
      for fruit in fruit_hit_list:
          score += 1
  
          fruit.reset_pos()
  
      # Draw all the spites
      all_sprites_list.draw(screen)

      screen.fill(BLACK)
      screen.blit(field, field_rect)
      all_sprites_list.draw(screen)
      score_text(screen, "Score: " + str(score), 50, 20, 10, BLACK)
      clock.tick(20)
      pygame.display.flip()

    else:
      screen.blit(field, field_rect)
      score_text(screen, "Level Complete!", 100, 150, 200, BLACK)
      pygame.time.delay(2000)
      level_2()
      clock.tick(20)
 
    
pygame.quit()