-
Notifications
You must be signed in to change notification settings - Fork 1
/
dive.py
127 lines (106 loc) · 3.38 KB
/
dive.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
# INTIALISATION
import pygame, math, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 600))
clock = pygame.time.Clock()
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])
pygame.display.set_caption("Dive")
def clamp (x, min, max):
if x > max:
return max
if x < min:
return min
return x
class DiverSprite(pygame.sprite.Sprite):
MAX_SPEED = 100
NOP = 0
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
def __init__(self, image, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image)
self.position = position
self.x_speed = 0
self.y_speed = 0
self.xdir = self.NOP;
self.ydir = self.NOP;
def update(self, deltat):
# SIMULATION
self.rect = self.image.get_rect()
if self.xdir == self.RIGHT:
self.addXSpeed(4)
elif self.xdir == self.LEFT:
self.addXSpeed(-4)
else:
self.xFriction(2)
if self.ydir == self.UP:
self.addYSpeed(-4)
elif self.ydir == self.DOWN:
self.addYSpeed(4)
else:
self.yFriction(2)
self.move(self.x_speed/10, self.y_speed/10)
self.rect.center = self.position
def addXSpeed(self, xdiff):
self.x_speed += xdiff
self.x = clamp(self.x_speed, -self.MAX_SPEED, self.MAX_SPEED)
def addYSpeed(self, ydiff):
self.y_speed += ydiff
self.y = clamp(self.y_speed, -self.MAX_SPEED, self.MAX_SPEED)
def setXDir(self, xdir):
self.xdir = xdir
def setYDir(self, ydir):
self.ydir = ydir
def xFriction(self, diff):
if self.x_speed > 0:
self.addXSpeed(- min(2, self.x_speed))
elif self.x_speed < 0:
self.addXSpeed(min(2, -self.x_speed))
def yFriction(self, diff):
if self.y_speed > 0:
self.addYSpeed(- min(2, self.y_speed))
elif self.y_speed < 0:
self.addYSpeed(min(2, -self.y_speed))
def move(self, xdiff, ydiff):
x = self.position[0] + xdiff
y = self.position[1] + ydiff
if x < 0:
x = 0
self.x_speed = 0
elif x > 600:
self.x_speed = 0
x = 600
if y < 0:
y = 0
self.y_speed = 0
elif y > 600:
self.y_speed = 0
y = 600
self.position = (x,y)
rect = screen.get_rect()
car = DiverSprite('triangle.png', rect.center)
car_group = pygame.sprite.RenderPlain(car)
#TODO use a game loop with better ticking
while 1:
deltat = clock.tick(30)
for event in pygame.event.get():
if not hasattr(event, 'key'): continue
if event.type == KEYDOWN:
if event.key == K_RIGHT: car.setXDir(DiverSprite.RIGHT)
elif event.key == K_LEFT: car.setXDir(DiverSprite.LEFT)
elif event.key == K_UP: car.setYDir(DiverSprite.UP)
elif event.key == K_DOWN: car.setYDir(DiverSprite.DOWN)
elif event.key == K_ESCAPE: sys.exit(0)
elif event.type == KEYUP:
if event.key == K_RIGHT: car.setXDir(DiverSprite.NOP)
elif event.key == K_LEFT: car.setXDir(DiverSprite.NOP)
elif event.key == K_UP: car.setYDir(DiverSprite.NOP)
elif event.key == K_DOWN: car.setYDir(DiverSprite.NOP)
# RENDERING
screen.fill((127,127,127))
car_group.update(deltat)
car_group.draw(screen)
pygame.display.flip()