-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (119 loc) · 5.16 KB
/
main.py
File metadata and controls
157 lines (119 loc) · 5.16 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pygame
import random
from datetime import datetime, timedelta
from settings import *
from player import Player
from item import Item
from platformbar import PlatformBar
""" Create n_players and add to all_sprites container.
"""
def add_players(all_sprites, n_players=1):
players = pygame.sprite.Group()
if n_players >= 1:
player = Player(500, 0, player_name="Nick")
all_sprites.add(player)
players.add(player)
if n_players >= 2:
player2 = Player(0, 0, move_keys=[pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d], player_name="Xavier")
all_sprites.add(player2)
players.add(player2)
if n_players >= 3:
player3 = Player(200, 0, move_keys=[pygame.K_i, pygame.K_k, pygame.K_j, pygame.K_l], player_name="Eke")
all_sprites.add(player3)
players.add(player3)
return players
""" Add platformbars where player can stand on to all_sprites container.
"""
def add_platformbars(all_sprites):
platformbars = pygame.sprite.Group()
locations = [(0, 150), (0, 450), (0, 750), (734, 150), (734, 450), (734, 750), (360, 300), (360, 600)]
for location in locations:
platformbar = PlatformBar("platform_img/black_bar.png", location[0], location[1])
all_sprites.add(platformbar)
platformbars.add(platformbar)
return platformbars
""" Return a random item from the items list.
"""
def random_item(items):
image, score = items[random.randint(0, len(items) - 1)]
return Item(image, points=score)
def init_gameboard():
# Initialize game.
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sicke game")
clock = pygame.time.Clock()
# Items list containing item image path and score value.
items = [("item_img/apple.png", 1), ("item_img/orange.png", 2), ("item_img/banana.png", 4), ("item_img/pineapple.png", 5), ("item_img/pear.png", 3), ("item_img/eggplant.png", -5)]
# Create container that holds sprites and add players.
all_sprites = pygame.sprite.Group()
player_sprites = add_players(all_sprites, 3)
item_sprites = pygame.sprite.Group()
# Add black bar sprite to middle of screen.
platformbar_sprites = add_platformbars(all_sprites)
# Main game loop.
running = True
clock_timer = None
while running:
# Stop if quit event is received.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear screen.
screen.fill(WHITE)
# Update all sprite animations/movement.
all_sprites.update()
# Player update loop.
for player in player_sprites:
# Make player fall to bottom of screen.
player.rect.y += GRAVITY
# Stop falling at platformbar.
for platformbar in platformbar_sprites:
# Make sprite that is a pixel at the bottom of the player position.
player_bottom = pygame.sprite.Sprite()
player_bottom.image = pygame.Surface((1, 1))
player_bottom.rect = player_bottom.image.get_rect()
player_bottom.rect.x = player.rect.x + player.rect.width/2
player_bottom.rect.y = player.rect.y + player.rect.height
# Check if player_bottom collides with platformbar, and stop falling.
if pygame.sprite.collide_rect(player_bottom, platformbar):
player.rect.y = platformbar.rect.y - player.rect.height
# Stop falling at bottom.
if player.rect.y >= HEIGHT - player.rect.height:
player.rect.y = HEIGHT - player.rect.height
# Check for collisions.
for item in item_sprites:
if pygame.sprite.collide_rect_ratio(0.6)(player, item):
player.score += item.points
item_sprites.remove(item)
all_sprites.remove(item)
# Add player name above player.
font = pygame.font.SysFont('arial', 16)
text = font.render(player.name + " ["+str(player.score)+"]", True, BLACK)
text_rect = text.get_rect()
text_rect.center = (player.rect.x+40, player.rect.y - 5)
screen.blit(text, text_rect)
# Add item score above items.
for item in item_sprites:
font = pygame.font.SysFont('arial', 16)
if item.points >= 0:
text = font.render("+"+str(item.points), True, BLACK)
else:
text = font.render(str(item.points), True, BLACK)
text_rect = text.get_rect()
text_rect.center = (item.rect.x+40, item.rect.y - 5)
screen.blit(text, text_rect)
clock_now = datetime.now()
# Add item every few seconds.
if clock_timer is None or clock_now >= clock_timer:
item = random_item(items)
all_sprites.add(item)
item_sprites.add(item)
clock_timer = clock_now + timedelta(seconds = 2)
# Redraw/update screen.
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(FRAMERATE)
pygame.quit()
if __name__ == "__main__":
init_gameboard()