forked from reddit-pygame/snowboard-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
74 lines (63 loc) · 2.46 KB
/
Copy patheditor.py
File metadata and controls
74 lines (63 loc) · 2.46 KB
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
import os
import json
import pygame as pg
import tools
import prepare
from state_engine import GameState
from labels import Button, ButtonGroup
from course import Course
from obstacles import Tree, Rock, RightGate, LeftGate, Jump
from obstacles import GreenSign, BlueSign, BlackSign
class Editor(GameState):
"""Allows the user to edit a course."""
def __init__(self):
super(Editor, self).__init__()
self.next_state = "MAIN_MENU"
def startup(self, persistent):
"""Creates a Course object from the previously selected JSON file."""
self.persist = persistent
name = self.persist["course_name"]
filepath = os.path.join("resources", "courses", "{}.json".format(name))
with open(filepath, "r") as f:
course_info = json.load(f)
self.course = Course(course_info)
self.scroll_speed = .25
self.view_center = list(self.course.view_rect.center)
def save_to_json(self):
"""Saves location of all course objects to be loaded for future use."""
course_info = {
"map_name": self.course.map_name,
"map_size": self.course.map_size,
"obstacles": [[x.name, x.rect.midbottom] for x in self.course.obstacles]
}
filepath = os.path.join("resources", "courses", "{}.json".format(self.course.map_name))
with open(filepath, "w") as f:
json.dump(course_info, f)
def get_event(self, event):
if event.type == pg.QUIT:
self.save_to_json()
self.done = True
elif event.type == pg.KEYUP:
if event.key == pg.K_ESCAPE:
self.save_to_json()
self.done = True
def scroll(self, dt, mouse_pos):
"""Move the view rect when the mouse is at the edge of the screen."""
speed = self.scroll_speed * dt
x, y = mouse_pos
w, h = prepare.SCREEN_SIZE
if x < 20:
self.view_center[0] -= speed
elif x > w - 20:
self.view_center[0] += speed
if y < 20:
self.view_center[1] -= speed
elif y > h - 20:
self.view_center[1] += speed
self.course.view_rect.center = self.view_center
def update(self, dt):
mouse_pos = pg.mouse.get_pos()
self.scroll(dt, mouse_pos)
def draw(self, surface):
surface.fill(pg.Color(242, 255, 255))
self.course.draw(surface)