-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbgame_play.py
187 lines (161 loc) · 6.47 KB
/
dbgame_play.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
# -*- coding: utf-8 -*-
import pygame
from NeuralNet import network
import objects
import levels
import init
import sys
import player
import numpy as np
from copy import deepcopy
clock = pygame.time.Clock()
jump_v = -12
continue_game = True
init.init_game()
def game_loop():
#game layer
quitting = False
draw_game = True
speed = 1
#generate first gen of players
bot_list = []
human = []
human = [player.Human()]
while not quitting:
###GENERATION LAYER###
if len(levels.levelarr) > 0:
levels.cur_level = levels.levelarr[0]
else:
levels.cur_level = levels.generate_level(0)
levels.cur_level_n = 0
ndead = 0
while ndead < 1:
###LEVEL LAYER###
#reset level
nwin = 0
give_up = False
for bn,tri in enumerate(bot_list + human):
if not tri.dead:
tri.x_pos = objects.x_init
tri.y_pos = objects.player_floor_1
tri.jumping = False
tri.fallen = False
tri.x_vel = player.xspeed
tri.y_vel = 0
tri.angle = 0
tri.win = False
levelrects = []
#display bg, floors and net
init.gameDisplay.fill((255,255,255))
if draw_game:
objects.draw_level(levels.cur_level,levelrects)
objects.draw_floors()
pygame.display.update()
pygame.time.wait(750)
activerects = []
while ndead + nwin < 1:
###TICK LAYER###
prevrects = activerects
activerects = [] #active parts to update on screen
#fill in previous positions
if draw_game:
init.gameDisplay.fill((255,255,255))
#for rect in prevrects:
#init.gameDisplay.fill((255,255,255),rect=rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitting = True
ndead = len(bot_list) + 5
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not human[0].jumping:
human[0].jumping = True
human[0].y_vel = jump_v
if event.key == pygame.K_PERIOD:
speed = 128
if event.key == pygame.K_COMMA:
speed = 1
if event.key == pygame.K_g:
draw_game = not draw_game
if event.key == pygame.K_r:
give_up = True
for tri in bot_list + human:
###PLAYER LOOP###
if tri.dead or tri.win:
continue
if tri.jumping:
tri.y_vel = tri.y_vel + 0.5
if tri.fallen:
#tri.angle = 0
tri.angle = tri.angle + 5
else:
#tri.angle = 0
tri.angle = tri.angle - 5
#UPDATE POSITION
tri.x_pos = tri.x_pos + tri.x_vel
tri.y_pos = tri.y_pos + tri.y_vel
#FLOOR COLLISIONS
if not tri.fallen:
if tri.y_pos > objects.player_floor_1:
tri.y_pos = objects.player_floor_1
tri.y_vel = 0
tri.jumping = False
tri.angle = 0
elif tri.y_pos > objects.player_floor_2:
tri.y_pos = objects.player_floor_2
tri.x_vel = -player.xspeed
tri.y_vel = 0
tri.jumping = False
tri.angle = 0
if tri.x_pos > init.display_width - objects.player_base:
tri.x_pos = init.display_width - objects.player_base
tri.y_pos = objects.player_floor_1
tri.y_vel = 0
tri.x_vel = 0
tri.fallen = True
tri.jumping = True
if tri.fallen and tri.x_pos < 0:
tri.win = True
nwin += 1
#CHECK FOR CRASH
crashed = objects.test_collision(tri.x_pos,tri.y_pos,tri.fallen,levels.cur_level)
if crashed:
tri.dead = True
ndead += 1
#DRAW IF NOT CRASHED
elif draw_game:
activerects.append(objects.draw_player(tri.x_pos,tri.y_pos,tri.angle,tri.image))
tri.score += 1
#END OF TICK LAYER
if human[0].dead and not give_up:
human[0].dead = False
ndead -= 1
human[0].x_pos = objects.x_init
human[0].y_pos = objects.player_floor_1
human[0].jumping = False
human[0].fallen = False
human[0].x_vel = player.xspeed
human[0].y_vel = 0
human[0].angle = 0
human[0].win = False
if draw_game:
objects.draw_level(levels.cur_level,levelrects)
objects.draw_floors()
#updaterects = prevrects + activerects
pygame.display.update()
clock.tick(60*speed)
#END OF LEVEL LAYER
if(nwin > 0):
if levels.cur_level_n < levels.num_levels or continue_game:
levels.next_level()
else:
quitting = True
ndead = 5
#END OF GEN LAYER, dont mutate if quitting
if not quitting:
#reset human
human[0].dead = False
human[0].score = 0
game_loop()
pygame.quit()
sys.exit()