-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocket_simulation.py
121 lines (95 loc) · 3.41 KB
/
rocket_simulation.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
# Import standard modules.
import sys
# Import non-standard modules.
import pygame as pyg
from pygame.locals import *
import numpy as np
import numpy.linalg as la
import scipy.linalg as spla
import matplotlib.pyplot as plt
from pprint import pprint as pp
from classes.controls import controls
from classes.ctr_sys import ctr_sys
class Background(pyg.sprite.Sprite):
def __init__(self, image_file, location, w=1280,h=720):
pyg.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pyg.image.load(image_file)
self.image = pyg.transform.scale(self.image, (w, h))
self.rect = self.image.get_rect()
self.location = location
self.rect.left, self.rect.top = location
def draw(self,screen,moved=False):
if moved == True:
self.rect.left, self.rect.top = self.location
screen.blit(self.image, self.rect)
class Rocket():
def __init__(self,x0,y0,theta0):
self.x = x0
self.y = y0
self.theta = theta0
self.img_h = 135
self.img_w = 110
self.img_x = x0 + 0.5 * self.img_w
self.img_y = y0 + 0.5 * self.img_h
self.img = Background('rocket_simulation/rocket.png',[self.img_x,self.img_y],self.img_w,self.img_h)
def update_location(self,x,y):
self.x = x
self.y = y
self.img_x = x + 0.5 * self.img_w
self.img_y = y + 0.5 * self.img_h
self.img.location = [self.img_x,self.img_y]
self.img.rect.left = self.img_x
self.img.rect.top = self.img_y
print('location: ' + str(self.img.location))
def draw(self,screen):
self.img.draw(screen,True)
def update(dt):
"""
Update game. Called once per frame.
dt is the amount of time passed since last frame.
"""
# Go through events that are passed to the script by the window.
for event in pyg.event.get():
# We need to handle these events. Initially the only one you'll want to care
# about is the QUIT event, because if you don't handle it, your game will crash
# whenever someone tries to exit.
if event.type == QUIT:
pyg.quit() # Opposite of pygame.init
sys.exit() # Not including this line crashes the script on Windows. Possibly
# on other operating systems too, but I don't know for sure.
# Handle other events as you wish.
def draw(screen,BackGround,Platform,Rocket):
"""
Draw things to the window. Called once per frame.
"""
screen.fill((0, 0, 0))
BackGround.draw(screen)
Platform.draw(screen)
Rocket.draw(screen)
pyg.display.flip()
def runPyGame():
# Initialise PyGame.
pyg.init()
# Set up the clock. This will tick every frame and thus maintain a relatively constant framerate. Hopefully.
fps = 60.0
fpsClock = pyg.time.Clock()
# Set up the window.
width, height = 1280, 720
screen = pyg.display.set_mode((width, height))
# screen is the surface representing the window.
BackGround = Background('rocket_simulation/desert-nights-moon.jpg', [0,0])
Platform = Background('rocket_simulation/platform.png', [500,720-300],300,100)
Rkt = Rocket(0,0,0)
# Main game loop.
dt = 1/fps # dt is the time since last frame.
x = 0
y = 0
while True: # Loop forever!
update(dt) # You can update/draw here, I've just moved the code for neatness.
Rkt.update_location(x,y)
draw(screen,BackGround,Platform,Rkt)
x += 1
y += 1
dt = fpsClock.tick(fps)
if __name__ == '__main__':
runPyGame()