-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygame
executable file
·112 lines (97 loc) · 4.92 KB
/
mygame
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
#!/usr/bin/python
import pygame
import sys
from hackmap import hackmap
from hackplayer import hackplayer
from hackdebug import hackdebug
import math
class HackNSlash:
def __init__(self):
# actual game window resolution
self.SCREEN_WIDTH = 1024
self.SCREEN_HEIGHT = 768
# ideal height of the internal screen
self.PIXEL_HEIGHT = 240
# the following get reset when the screen is setup
self.PIXEL_RATIO = 1
self.ISCREEN_WIDTH = 0
self.ISCREEN_HEIGHT = 0
self.SCREEN_X_OFFSET = 0
self.SCREEN_Y_OFFSET = 0
# some defaults
self.debug = hackdebug(self)
self.quit = False
self.INIT_MAP = "main"
self.INIT_PLAYER_LOCATION = [3, 3]
self.INIT_PLAYER_SPEED = 2.0 # should be a whole number
# actually get to work here
self.screen, self.iscreen = self.setup_screen(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
self.clock = pygame.time.Clock()
self.main_map = hackmap(self, self.INIT_MAP)
self.map_surface = self.main_map.draw()
self.player = hackplayer(self, self.INIT_PLAYER_LOCATION[0], self.INIT_PLAYER_LOCATION[1], \
self.INIT_PLAYER_SPEED)
def setup_screen(self, width, height):
# these two lines just set the game window size
self.SCREEN_WIDTH = width
self.SCREEN_HEIGHT = height
# figure out what ratio we will have to scale things
self.PIXEL_RATIO = height / self.PIXEL_HEIGHT
# make the iscreen height into a factor of the screen height
self.ISCREEN_HEIGHT = self.PIXEL_HEIGHT + \
((self.SCREEN_HEIGHT % self.PIXEL_HEIGHT) / self.PIXEL_RATIO)
# make the iscreen width into a factor of the width
self.ISCREEN_WIDTH = self.SCREEN_WIDTH / self.PIXEL_RATIO
# offsets help us center the rendered iscreen when we put it onto the game window
self.SCREEN_X_OFFSET = (self.SCREEN_WIDTH - (self.ISCREEN_WIDTH * self.PIXEL_RATIO)) / 2
self.SCREEN_Y_OFFSET = (self.SCREEN_HEIGHT - (self.ISCREEN_HEIGHT * self.PIXEL_RATIO)) / 2
return pygame.display.set_mode([width, height], pygame.SRCALPHA, 32), \
pygame.Surface([self.ISCREEN_WIDTH, self.ISCREEN_HEIGHT], pygame.SRCALPHA, 32)
def draw_scene(self):
self.iscreen.fill((0,0,0))
self.iscreen.blit(self.map_surface, self.get_camera_coords())
self.iscreen.blit(self.player.image, [self.ISCREEN_WIDTH / 2, self.ISCREEN_HEIGHT / 2])
def get_camera_coords(self):
x = (math.floor(self.player.position[0]) - (self.ISCREEN_WIDTH / 2) + self.main_map.buffer) * -1
y = (math.floor(self.player.position[1]) - (self.ISCREEN_HEIGHT / 2) + self.main_map.buffer) * -1
return [x, y]
def flip_display(self, iscreen, screen):
screen.blit(pygame.transform.scale(iscreen, \
[self.ISCREEN_WIDTH * self.PIXEL_RATIO, self.ISCREEN_HEIGHT * self.PIXEL_RATIO]), \
[self.SCREEN_X_OFFSET, self.SCREEN_Y_OFFSET])
if self.debug.render: screen.blit(self.debug.render_it(), [0, 0])
pygame.display.flip()
def handle_input(self, buttons):
if buttons[pygame.K_UP] and buttons[pygame.K_RIGHT]: self.player.move_ne()
elif buttons[pygame.K_RIGHT] and buttons[pygame.K_DOWN]: self.player.move_se()
elif buttons[pygame.K_DOWN] and buttons[pygame.K_LEFT]: self.player.move_sw()
elif buttons[pygame.K_LEFT] and buttons[pygame.K_UP]: self.player.move_nw()
elif buttons[pygame.K_UP]: self.player.move_n()
elif buttons[pygame.K_RIGHT]: self.player.move_e()
elif buttons[pygame.K_DOWN]: self.player.move_s()
elif buttons[pygame.K_LEFT]: self.player.move_w()
def handle_keydown(self, key):
if key == pygame.K_d: self.debug.render = not self.debug.render
elif key == pygame.K_q or key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT))
def loop(self):
while not self.quit:
time_passed = self.clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT: self.quit = True
elif event.type == pygame.KEYDOWN: self.handle_keydown(event.key)
self.handle_input(pygame.key.get_pressed())
coords = self.player.get_tile_position()
#self.debug.set_value("tile XY", str(coords[0]) + ", " + str(coords[1]))
#self.debug.set_value("tile pixel location", str(self.main_map.get_xy_from_tile(coords[0], coords[1])))
#current_tile_properties = self.main_map.get_tile_properties(coords[0], coords[1])
#for key in current_tile_properties:
# self.debug.set_value("tile " + str(key), str(current_tile_properties[key]))
self.draw_scene()
self.flip_display(self.iscreen, self.screen)
pygame.quit()
sys.exit()
def main():
pygame.init()
game_object = HackNSlash()
game_object.loop()
main()