diff --git a/game.py b/game.py index e753032..4d7c93d 100644 --- a/game.py +++ b/game.py @@ -14,7 +14,7 @@ #You were importing all widgets anyway, and there was an unknown error. class Game(object): - print "Setting global Game params." + print("Setting global Game params.") # Game parameters BG_TILE_IMG = 'images/wood2.png' BUTTON_BGIMG = 'images/x.png' @@ -28,7 +28,7 @@ class Game(object): def __init__(self): pygame.init() - print "Pygame started." + print("Pygame started.") #set up screen and background self.screen = pygame.display.set_mode( @@ -36,105 +36,111 @@ def __init__(self): self.tile_img = pygame.image.load(self.BG_TILE_IMG).convert_alpha() self.tile_img_rect = self.tile_img.get_rect() - #Drawing a handy MessageBoard widget + # StartScreen added + self.start_screen = True + self.start_font = pygame.font.SysFont('comic sans', 36) + self.start_text = self.start_font.render('Press Enter to Start', True, pygame.Color('yellow')) + self.start_text_rect = self.start_text.get_rect(center=(self.SCREEN_WIDTH // 2, self.SCREEN_HEIGHT // 2)) + + #Drawing a handy MessageBoard widget #Can use these for any text. - print "Configuring tboard MessageBoard params." + print("Configuring tboard MessageBoard params.") self.tboard_text = ['This is a test.'] self.tboard_x = 120 self.tboard_y = 120 - self.tboard_width = 125 - self.tboard_height = 30 + self.tboard_width = 200 + self.tboard_height = 50 self.tboard_rect = pygame.Rect(self.tboard_x, self.tboard_y, self.tboard_width, self.tboard_height) self.tboard_bgcolor = pygame.Color(50, 20, 0) self.tboard = MessageBoard(self.screen, rect=self.tboard_rect, bgcolor=self.tboard_bgcolor, - border_width=4, + border_width=4, border_color=pygame.Color('black'), text=self.tboard_text, padding=5, font=('comic sans', 18), font_color=pygame.Color('yellow')) - print "Moving on to buttons..." + print("Moving on to buttons...") - self.button_bgimgs = ['images/x.png'] - #self.button_width = self.button_bgimgs[0].get_width() - #self.button_height = self.button_bgimgs[0].get_height() + self.button_bgimgs = ['images/x.png'] + #self.button_width = self.button_bgimgs[0].get_width() + #self.button_height = self.button_bgimgs[0].get_height() - #hopefully this will draw the button -15 pixels from the right end, +15 from the top - #(hopefully giving us a nice X) - # should be replaced in the future with a method that returns the coords for an x button - # in whatever corner we want. - #self.button_rect = Rect(self.tboard_width, self.tboard_y-15, self.button_width, self.button_height) - self.button = Button(self.screen, + #hopefully this will draw the button -15 pixels from the right end, +15 from the top + #(hopefully giving us a nice X) + # should be replaced in the future with a method that returns the coords for an x button + # in whatever corner we want. + #self.button_rect = Rect(self.tboard_width, self.tboard_y-15, self.button_width, self.button_height) + self.button = Button(self.screen, pos=vec2d(self.tboard_width, self.tboard_y-15), btntype='Close', imgnames=self.button_bgimgs, attached=self.tboard) - print "Created close button." + print("Created close button.") - self.togglebtn_bgimgs = ['images/toggle1.png', 'images/toggle2.png'] + self.togglebtn_bgimgs = ['images/toggle1.png', 'images/toggle2.png'] - self.togglebtn = Button(self.screen, + self.togglebtn = Button(self.screen, pos=vec2d(250, 250), btntype='Toggle', imgnames=self.togglebtn_bgimgs, attached="", - text="Toggle", - textcolor=(255,255,255)) + text="Toggle", + textcolor=(255,255,255)) - print "Created toggle button." + print("Created toggle button.") - self.clockImg = Images(self.screen, - 'images/clock.png', - pos=vec2d(430,0)) + self.clockImg = Images(self.screen, + 'images/clock.png', + pos=vec2d(430,0)) - self.hand = Images(self.screen, - 'images/secondHand.png', - pos=vec2d(505,15), - imgtype='Spinner') + self.hand = Images(self.screen, + 'images/secondHand.png', + pos=vec2d(505,15), + imgtype='Spinner') - self.textTest = textEntry(self.screen, - pos=vec2d(0, self.SCREEN_HEIGHT-50), - size=vec2d(self.SCREEN_WIDTH,50)) + self.textTest = textEntry(self.screen, + pos=vec2d(0, self.SCREEN_HEIGHT-50), + size=vec2d(self.SCREEN_WIDTH,50)) - self.floater = movingRect(self.screen, - pos=vec2d(self.SCREEN_WIDTH/2, 0), - speed=vec2d(0,5)) + self.floater = movingRect(self.screen, + pos=vec2d(self.SCREEN_WIDTH/2, 0), + speed=vec2d(0,5)) - self.moveImg = movingImg(self.screen, - "images/toggle1.png", - pos=vec2d(0,self.SCREEN_HEIGHT*3/4), - speed=vec2d(5, 0)) + self.moveImg = movingImg(self.screen, + "images/toggle1.png", + pos=vec2d(0,self.SCREEN_HEIGHT*3/4), + speed=vec2d(5, 0)) - self.ball = circles(self.screen, - pos=vec2d(25,25), - radius = 25) + self.ball = circles(self.screen, + pos=vec2d(25,25), + radius = 25) - self.buttons = [self.togglebtn] - self.textEntries = [self.textTest] + self.buttons = [self.togglebtn] + self.textEntries = [self.textTest] - self.world = [self.button, self.togglebtn, self.clockImg, self.hand, self.textTest, self.moveImg, self.floater, self.ball] + self.world = [self.button, self.togglebtn, self.clockImg, self.hand, self.textTest, self.moveImg, self.floater, self.ball] - self.clock = pygame.time.Clock() - self.paused = False + self.clock = pygame.time.Clock() + self.paused = False - #spawning entities + #spawning entities - #Setting up gamefield - #need a method for dynamically figuring out how many rows/columns we need based on - #the spacing we want and field size. Using some constants for now. - self.grid_nrows = 30 - self.grid_ncols = 30 + #Setting up gamefield + #need a method for dynamically figuring out how many rows/columns we need based on + #the spacing we want and field size. Using some constants for now. + self.grid_nrows = 30 + self.grid_ncols = 30 - self.field_rect = pygame.Rect(0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT) + self.field_rect = pygame.Rect(0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT) - self.options = dict(debug=True, + self.options = dict(debug=True, draw_grid=False) - print "Done setting game options, exiting Game init." + print("Done setting game options, exiting Game init.") def xy2coord(self, pos): """ Convert a (x, y) pair to a (nrow, ncol) coordinate @@ -193,63 +199,61 @@ def draw(self): self.tboard.draw() - for obj in self.world: - obj.draw() + for obj in self.world: + obj.draw() def run(self): - print "Beginning run sequence." + print("Beginning run sequence.") # The main game loop - # while True: # Limit frame speed to 30 FPS - # self.time_passed = self.clock.tick(30) - #~ time_passed = self.clock.tick() - #~ print time_passed - + # If too long has passed between two frames, don't # update (the game must have been suspended for some # reason, and we don't want it to "jump forward" # suddenly) - # if self.time_passed > 100: continue - - active = False - for entry in self.textEntries: - if entry.clicked: - active = True + + active = False + for entry in self.textEntries: + if entry.clicked: + active = True + #Event loop. In-game control is routed through here - #Will probably need something more robust soon. - for event in pygame.event.get(): - if event.type == pygame.QUIT: - self.quit() - elif event.type == pygame.KEYDOWN and not active: - if event.key == pygame.K_SPACE: - self.paused = not self.paused - elif event.key == pygame.K_g: - #toggle draw grid - self.options['draw_grid'] = not self.options['draw_grid'] - elif (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1): - for button in self.buttons: - button.mouse_click_event(event.pos) - for entry in self.textEntries: - entry.mouse_click_event(event.pos) - - #pass temporarily disabled, don't think it does anything - - #entity events here. + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.quit() + elif event.type == pygame.KEYDOWN: + if self.start_screen and event.key == pygame.K_RETURN: + self.start_screen = False + elif not self.start_screen and not active: + if event.key == pygame.K_SPACE: + self.paused = not self.paused + elif event.key == pygame.K_g: + #toggle draw grid + self.options['draw_grid'] = not self.options['draw_grid'] + elif (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1): + if not self.start_screen: + for button in self.buttons: + button.mouse_click_event(event.pos) + for entry in self.textEntries: + entry.mouse_click_event(event.pos) - #update hud, counters, score, anything like that here - if not self.paused: - msg1 = '' - msg2 = '' - #update stats counters. Not doing anything yet - self.mboard_text = [msg1, msg2] + if self.start_screen: + self.screen.fill((0, 0, 0)) + self.screen.blit(self.start_text, self.start_text_rect) + else: + if not self.paused: + msg1 = '' + msg2 = '' + #update stats counters. Not doing anything yet + self.mboard_text = [msg1, msg2] - #update entities with time passed for internal calculations + #update entities with time passed for internal calculations + self.draw() - self.draw() #actually flip Surface buffer pygame.display.flip() @@ -258,7 +262,7 @@ def quit(self): if __name__ == "__main__": - print "Creating game object..." + print("Creating game object...") game = Game() - print "Done. Starting run method" - game.run() + print("Done. Starting run method") + game.run() \ No newline at end of file diff --git a/simpleanimation.py b/simpleanimation.py index a23e828..862d52e 100644 --- a/simpleanimation.py +++ b/simpleanimation.py @@ -21,7 +21,7 @@ def __init__(self, screen, pos, images, scroll_period, duration=-1): Duration of the animation (in ms). If -1, the animation will have indefinite duration. """ - print "starting animation." + print("starting animation.") self.screen = screen self.images = images self.pos = pos @@ -65,30 +65,29 @@ def _advance_img(self): self.img_ptr = (self.img_ptr + 1) % len(self.images) class start(): - if __name__ == "__main__": - print "initializing" - pygame.init() - screen = pygame.display.set_mode((300, 300), 0, 32) + if __name__ == "__main__": + print("initializing") + pygame.init() + screen = pygame.display.set_mode((300, 300), 0, 32) - clock = pygame.time.Clock() - explosion_img = pygame.image.load('images/explosion1.png').convert_alpha() - images = [explosion_img, pygame.transform.rotate(explosion_img, 90)] + clock = pygame.time.Clock() + explosion_img = pygame.image.load('images/explosion1.png').convert_alpha() + images = [explosion_img, pygame.transform.rotate(explosion_img, 90)] - expl = SimpleAnimation(screen, (100, 100), images, 100, 2120) + expl = SimpleAnimation(screen, (100, 100), images, 100, 2120) - while True: - time_passed = clock.tick(50) - - screen.fill((0, 0, 0)) - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - sys.exit() - - expl.update(time_passed) - expl.draw() - - pygame.display.flip() - -go = start() + while True: + time_passed = clock.tick(50) + + screen.fill((0, 0, 0)) + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + + expl.update(time_passed) + expl.draw() + + pygame.display.flip() +go = start() \ No newline at end of file diff --git a/widgets.py b/widgets.py index 2cd4c2f..7b48acf 100644 --- a/widgets.py +++ b/widgets.py @@ -153,7 +153,7 @@ class Button(object): def __init__(self, surface, pos=vec2d(0, 0), btntype="", imgnames=[], text="", textcolor=(0,0,0), textimg=0,padding=0, attached=""): - print "In button init method" + print ("In button init method") self.surface = surface self.pos = pos self.btntype = btntype @@ -177,7 +177,7 @@ def __init__(self, surface, pos=vec2d(0, 0), btntype="", imgnames=[], text="", t self.imgwidth, self.imgheight = self.imgs[self.toggle].get_size() self.rect = Rect(self.pos.x, self.pos.y, self.imgwidth, self.imgheight) - print "Image dimensions are: " + str(self.imgwidth) + ", " + str(self.imgheight) + print ("Image dimensions are: " + str(self.imgwidth) + ", " + str(self.imgheight)) #creates a text label to place in the middle of the button font = pygame.font.SysFont("Times New Roman", 25) @@ -210,7 +210,7 @@ def mouse_click_event(self, pos): if self._point_is_inside(vec2d(pos)): self.count = 100 expl = simpleanimation.start() - print "Action" + print ("Action") def _point_is_inside(self, mpos): if mpos.x > self.rect.x and mpos.x < self.rect.x+self.imgwidth: @@ -226,7 +226,7 @@ def __init__(self, surface, image, pos=vec2d(0, 0),imgtype=""): self.pos = pos self.count = 0 self.imgwidth, self.imgheight = self.img.get_size() - print "Image dimensions are: " + str(self.imgwidth) + ", " + str(self.imgheight) + print ("Image dimensions are: " + str(self.imgwidth) + ", " + str(self.imgheight)) self.rect = Rect(self.pos.x, self.pos.y, self.imgwidth, self.imgheight) def draw(self): @@ -254,7 +254,7 @@ def draw(self): class textEntry(object): """ allows for reading input from the user """ def __init__(self, surface, pos=vec2d(0, 0), size = vec2d(200,50), text="", textcolor=(0,0,0),padding=0, bgcolor = (255,255,255)): - print "In textEntry init method" + print ("In textEntry init method") self.surface = surface self.pos = pos self.size = size