-
Notifications
You must be signed in to change notification settings - Fork 1
/
SkiRun.py
195 lines (156 loc) · 6.44 KB
/
SkiRun.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
from pygame import *
from player import *
from dataManager import *
from object import *
import time
from random import randint
import serial
WIN_WIDTH = 800
WIN_HEIGHT = 640
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
BACKGROUND_COLOR = "#F9F0F8"
total_level_width = 0
total_level_height = WIN_HEIGHT
FILE_DIR = os.path.dirname(__file__)
valueFromSimulator = 0
isRunnig = True
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def reverse(self, pos):# получение внутренних координат из глобальных
return pos[0] - self.state.left, pos[1] - self.state.top
def camera_configure(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t = -l+WIN_WIDTH / 2, -t+WIN_HEIGHT / 2
l = min(0, l) # Не движемся дальше левой границы
l = max(-(camera.width-WIN_WIDTH), l) # Не движемся дальше правой границы
t = max(-(camera.height-WIN_HEIGHT), t) # Не движемся дальше нижней границы
t = min(0, t) # Не движемся дальше верхней границы
return Rect(l, t, w, h)
def main(profileId, distanceId):
global valueFromSimulator, isRunnig
lastValueFromSimulator = 0
pygame.init()
screen = pygame.display.set_mode(DISPLAY)
pygame.display.set_caption("SKiRun")
bg = Surface((WIN_WIDTH, WIN_HEIGHT))
snowFlakes = pygame.sprite.Group()
heroes = pygame.sprite.Group()
flags = pygame.sprite.Group()
dm = DataManager()
dm.currentDistanceId = distanceId
dm.currentProfileId = profileId
raceId = dm.newRace(dm.currentProfileId,dm.currentDistanceId)
total_level_width =dm.getRaceDistance(raceId) + 84 #+64 - финишная черта
timer = pygame.time.Clock()
bg.fill(Color(BACKGROUND_COLOR))
for i in range(1,100):
snowflake = Snowflake(randint(1,total_level_width),randint(1,total_level_height))
snowFlakes.add(snowflake)
for i in range(0,total_level_width / 200):
flagRed = FlagRed(i * 200,316)
flagBlue = FlagBlue(i * 200,366)
flags.add(flagRed)
flags.add(flagBlue)
speed = 0
hero = Player(0, 300)
heroes.add(hero)
lastHero = LastPlayer(1, 350)
heroes.add(lastHero)
currentTime = time.time()
raceTime = time.time()
timer = pygame.time.Clock()
camera = Camera(camera_configure, total_level_width, total_level_height)
finish = FinishLine(total_level_width-64,0)
heroes.add(finish)
isSetCurrentTime = False
while not hero.winner:
timer.tick_busy_loop(60)
speed = dm.getImpulse(valueFromSimulator)
if speed > 0:
dm.logSpeed(hero.rect.x,dm.getCurrentRaceId())
if not isSetCurrentTime and dm.isRaceStart:
currentTime = time.time()
isSetCurrentTime = True
try:
distanceLastRace = dm.getLastRaceDistanceAtCurrentTime(dm.getLastRaceId(dm.currentProfileId),currentTime)
except Exception as e:
distanceLastRace = -1
print(e)
for e in pygame.event.get():
if e.type == QUIT:
dm.closeDB()
isRunnig = False
exit(0)
raise SystemExit, "QUIT"
for e in snowFlakes:
screen.blit(e.image, camera.apply(e))
e.rect.y+=1
if e.rect.y >= WIN_HEIGHT:
e.rect.y = 0
for e in flags:
screen.blit(e.image, camera.apply(e))
for e in heroes:
screen.blit(e.image, camera.apply(e))
hero.update(speed, finish)
lastHero.update(distanceLastRace)
camera.update(hero) # центризируем камеру относительно персонаж
raceTime = time.time() - currentTime
heroSpeed = dm.speed
font=pygame.font.Font(None,70)
textSpeed=font.render((u"Скорость : %.2f км\ч || %.2f м\с " % ((heroSpeed / 1000 * 3600), heroSpeed)), 1,(0,0,0))
textDistance=font.render((u"Дистанция: %s м из %s м" % ((hero.rect.x),dm.getDistanceNameById(dm.currentDistanceId))), 1,(0,0,0))
h = ((raceTime / 3600)) % 24
m = (raceTime / 60) % 60
s = raceTime % 60
textTime=font.render((u"Время: %d:%02d:%02d" % (h, m, s)), 1,(0,0,0))
textProfile=font.render(("%s" % (dm.getProfileNameById(dm.currentProfileId))), 1,(0,0,0))
screen.blit(textSpeed, (10,10))
screen.blit(textDistance, (10,50))
screen.blit(textTime, (10,90))
screen.blit(textProfile, (10,580))
pygame.display.update()
screen.blit(bg, (0, 0))
dm.logSpeed(hero.rect.x,dm.getCurrentRaceId())
while isRunnig:
if lastHero.rect.x == 0:
finalText = u"Второй...:("
else:
finalText = u"Поздравляем победителя! :)"
font=pygame.font.Font(None,65)
averageSpeed = dm.getAverageSpeedByRace(dm.getCurrentRaceId())
textSpeed=font.render((u"Ср. скорость: %.2f км\ч || %.2f м\с " % ((averageSpeed / 1000 * 3600), averageSpeed)), 1,(0,0,0))
textDistance=font.render((u"Пройдено: %s м" % (hero.rect.x)), 1,(0,0,0))
h = ((raceTime / 3600)) % 24
m = (raceTime / 60) % 60
s = raceTime % 60
textTime=font.render((u"Затрачено времени: %d:%02d:%02d" % (h, m, s)), 1,(0,0,0))
text=font.render(finalText, 1,(0,0,200))
screen.blit(textSpeed, (10,10))
screen.blit(textDistance, (10,50))
screen.blit(textTime, (10,90))
pygame.display.update()
screen.blit(bg, (0, 0))
screen.blit(text, (10,150))
for e in pygame.event.get():
if e.type == QUIT or e.type == KEYDOWN :
isRunnig = False
dm.closeDB()
def getDataFromSimulator():
global valueFromSimulator, isRunnig
ser = serial.Serial('/dev/ttyACM0', 9600)
while isRunnig:
value = ser.readline()
try:
valueFromSimulator = int(value)
except:
pass