-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
114 lines (92 loc) · 3.83 KB
/
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
109
110
111
112
113
114
import pygame as pg
import random
from setup import *
from sprite import *
class Game:
def __init__(self):
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
self.font_name = pg.font.match_font(FONT_NAME)
def new(self):
self.score_p1 = 0
self.score_p2 = 0
self.all_sprites = pg.sprite.Group()
self.platforms = pg.sprite.Group()
self.player1 = Player1(self)
self.all_sprites.add(self.player1)
self.player2 = Player2(self)
self.all_sprites.add(self.player2)
p1 = Platform(0, HEIGHT - 40, WIDTH, 40)
self.all_sprites.add(p1)
self.platforms.add(p1)
self.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
#Game Loop - Update
self.all_sprites.update()
#check if player hits a platform - only if falling
hits_platform = pg.sprite.spritecollide(self.player1, self.platforms, False)
if hits_platform:
self.player1.pos.y = hits_platform[0].rect.top + 1
self.player1.vel.y = 0
hits_players = pg.sprite.collide_rect(self.player1, self.player2)
if hits_players:
if self.player1.rect.right > self.player2.rect.left and self.player1.rect.right < self.player2.rect.right:
self.player1.vel.x = -10
self.player2.vel.x= 10
elif self.player2.rect.right > self.player1.rect.left and self.player2.rect.right < self.player1.rect.right:
self.player1.vel.x = 10
self.player2.vel.x= -10
if self.player1.rect.bottom > self.player2.rect.top and self.player1.rect.top < self.player2.rect.top:
self.score_p1 += 1
self.player1.pos.y = self.player2.rect.top
self.player1.vel.y = -10
self.player2.vel.y = 10
elif self.player2.rect.bottom > self.player1.rect.top and self.player2.rect.top < self.player1.rect.top:
self.score_p2 += 1
self.player2.pos.y = self.player1.rect.top
self.player2.vel.y = 0
self.player2.vel.y = -10
self.player1.vel.y = 10
if self.player2.vel.y > 0:
hits = pg.sprite.spritecollide(self.player2, self.platforms, False)
if hits:
self.player2.pos.y = hits[0].rect.top + 1
self.player2.vel.y = 0
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
self.player2.jump(self.player1)
if event.key == pg.K_UP:
self.player1.jump(self.player2)
def draw(self):
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
self.draw_text(str(self.score_p2) + " : " + str(self.score_p1), 22, WHITE, WIDTH / 2, 25)
pg.display.flip()
def draw_text(self, text, size, color, x, y):
font = pg.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
g = Game()
while g.running:
g.new()
pg.quit()
quit()