@seamuswalden3/

WeepyUnderstatedColdfusion

Pygame

No description

fork
loading
Files
  • main.py
  • Font.ttf
  • nohup.out
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#Importing all the modules
try:
    import time, random, sys, os
except ImportError:
    print("Make sure to have the time module")
    sys.exit()
try:
    import pygame
except ImportError:
    print("Make sure you have python 3 and pygame.")
    sys.exit()
from pygame import freetype


#game_font = pygame.freetype.Font("Font.ttf", 75)
#text_surface, rect = game_font.render(("Programmer: 8BitToaster"), (0, 0, 0))
#gameDisplay.blit(text_surface, (150, 300))

# Initialize the game engine
pygame.init()


DisplayWidth,DisplayHeight = 1400, 800
clock = pygame.time.Clock()

gameDisplay = pygame.display.set_mode((DisplayWidth,DisplayHeight))
pygame.display.set_caption("Name")
font_50 = pygame.freetype.Font("Font.ttf", 50)

class Player():
    def __init__(self,Pos, color, Num):
        self.x = Pos[0]
        self.y = Pos[1]
        self.width = 20
        self.height = 20
        self.color = color
        self.direction = "down"
        self.num = Num
        self.alive = True
        self.moveCooldown = 3
        self.Apple = False
        self.MaxCooldown = 3

    def KeyPress(self, Board):
        key = pygame.key.get_pressed()

        if self.num == 1:
            if key[pygame.K_a] and self.direction != "right":
                self.direction = "left"
            if key[pygame.K_d] and self.direction != "left":
                self.direction = "right"
            if key[pygame.K_w] and self.direction != "down":
                self.direction = "up"
            if key[pygame.K_s] and self.direction != "up":
                self.direction = "down"
        if self.num == 2:
            if key[pygame.K_LEFT] and self.direction != "right":
                self.direction = "left"
            if key[pygame.K_RIGHT] and self.direction != "left":
                self.direction = "right"
            if key[pygame.K_UP] and self.direction != "down":
                self.direction = "up"
            if key[pygame.K_DOWN] and self.direction != "up":
                self.direction = "down"
        if self.num == 3:
            if key[pygame.K_j] and self.direction != "right":
                self.direction = "left"
            if key[pygame.K_l] and self.direction != "left":
                self.direction = "right"
            if key[pygame.K_i] and self.direction != "down":
                self.direction = "up"
            if key[pygame.K_k] and self.direction != "up":
                self.direction = "down"
        if self.x >= 0 and self.x <= 49 and self.y >= 0 and self.y <= 39:

            if self.moveCooldown <= 0:
                if self.direction == "left":
                    if Board[self.y][self.x-1] != 0:
                        self.alive = False
                    else:
                        self.x -= 1
                if self.direction == "right":
                    if Board[self.y][self.x+1] != 0:
                        self.alive = False
                    else:
                        self.x += 1
                if self.direction == "up":
                    if Board[self.y-1][self.x] != 0:
                        self.alive = False
                    else:
                        self.y -= 1
                if self.direction == "down":
                    if Board[self.y+1][self.x] != 0:
                        self.alive = False
                    else:
                        self.y += 1
                self.moveCooldown = self.MaxCooldown
            else:
                self.moveCooldown -= 1
        else:
            self.alive = False

        return Board

    def update(self, Board, Apple):
        if self.alive == True:
            Board = self.KeyPress(Board)
            if self.x >= 0 and self.x <= 49 and self.y >= 0 and self.y < 39:
                Board[self.y][self.x] = self.num
            else:
                self.alive = False

            if (self.x, self.y) == (Apple.x, Apple.y):
                Apple.reset(Board)
                self.Apple = True
            
        return Board

class Apple():
    def __init__(self):
        self.x = random.randint(0,49)
        self.y = random.randint(0,39)

    def draw(self):
        pygame.draw.rect(gameDisplay,(150,0,0),(self.x*20,self.y*20,20,20),0)

    def reset(self, board):
        placed = False
        while not placed:
            RanX = random.randint(0,49)
            RanY = random.randint(0,39)
            if board[RanY][RanX] == 0:
                placed = True
                self.x = RanX
                self.y = RanY

    def update(self):
        self.draw()
    
def draw(x,y,Color):
    pygame.draw.rect(gameDisplay,Color,(x,y,20,20),0)
    

def game_loop():
    #Defening all vars and classes
    game_run = True
    board = [[0] * 50 for _ in range(40)]
    Players = [Player((10,5),(0,0,150), 1),Player((35,5),(0,150,0), 2)]
    apple = Apple()

    while game_run == True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        #Checks if players are alive and also updates them
        Alive = 0
        AppleIndex = 999
        for player in Players:
            board = player.update(board, apple)
            if player.alive == True:
                Alive += 1
            if player.Apple == True:
                AppleIndex = Players.index(player)
                player.Apple = False
        if AppleIndex != 999:
            for i, player in enumerate(Players):
                if i != AppleIndex:
                    player.MaxCooldown = int(player.moveCooldown/2)
        if Alive <= 1:
            for player in Players:
                if player.alive == True:
                    Score[Players.index(player)] += 1
                    game_loop()
            

        #Draws the board
        for j in range(len(board)):
            for i in range(len(board[0])):
                if board[j][i] == 0:
                     draw(i*20,j*20,(100,100,100))
                else:
                    draw(i*20,j*20,Players[board[j][i]-1].color)

        #Updates Apple
        apple.update()

            
        #Draws the scoreboard
        pygame.draw.rect(gameDisplay,(75,0,0),(1000,0,400,800),0)
        for i, score in enumerate(Score):
            text_surface, rect = font_50.render(("Player " + str(i+1)), (0, 0, 0))
            gameDisplay.blit(text_surface, (1100, 100*i+100))
            
            text_surface, rect = font_50.render(str(score), (0, 0, 0))
            gameDisplay.blit(text_surface, (1100, 100*i+150))
        

        pygame.display.flip()
        clock.tick(60)


Score = [0,0]
game_loop()