diff --git a/game.py b/game.py index e753032..4920e58 100644 --- a/game.py +++ b/game.py @@ -5,169 +5,153 @@ import pygame #from pygame import Rect, Color #from pygame.sprite import Sprite -#you already imported pygame, why import modules twice? Your code will run faster if +#you already imported pygame, why import modules twice? Your code will run faster if #you change Rect to pygame.Rect like I did. If this is a style preference, let me know. from utils import Timer from vec2d import vec2d from widgets import * -#You were importing all widgets anyway, and there was an unknown error. +#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' SCREEN_WIDTH, SCREEN_HEIGHT = 580, 500 GRID_SIZE = 20 FIELD_SIZE = 400, 400 - - #need to implement resource loading here - - #global game constants make cheating easy! - + def __init__(self): pygame.init() - print "Pygame started." - - #set up screen and background + print("Pygame started.") + + # set up screen and background self.screen = pygame.display.set_mode( - (self.SCREEN_WIDTH, self.SCREEN_HEIGHT), 0, 32) + (self.SCREEN_WIDTH, self.SCREEN_HEIGHT), 0, 32) 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 - #Can use these for any text. - print "Configuring tboard MessageBoard params." - self.tboard_text = ['This is a test.'] + + # Drawing a handy MessageBoard widget + # Can use these for any text. + print("Configuring tboard MessageBoard params.") + self.tboard_text = ['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..." - - 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, - pos=vec2d(self.tboard_width, self.tboard_y-15), - btntype='Close', - imgnames=self.button_bgimgs, - attached=self.tboard) - - print "Created close button." - - self.togglebtn_bgimgs = ['images/toggle1.png', 'images/toggle2.png'] - - self.togglebtn = Button(self.screen, - pos=vec2d(250, 250), - btntype='Toggle', - imgnames=self.togglebtn_bgimgs, - attached="", - text="Toggle", - textcolor=(255,255,255)) - - print "Created toggle button." - - 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.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.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.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.clock = pygame.time.Clock() - self.paused = False - - #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 - - self.field_rect = pygame.Rect(0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT) - - self.options = dict(debug=True, - draw_grid=False) - - print "Done setting game options, exiting Game init." - + + print("Moving on to buttons...") + + self.button_bgimgs = ['images/x.png'] + 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.") + + self.togglebtn_bgimgs = ['images/toggle1.png', 'images/toggle2.png'] + self.togglebtn = Button(self.screen, + pos=vec2d(250, 250), + btntype='Toggle', + imgnames=self.togglebtn_bgimgs, + attached="", + text="Toggle", + textcolor=(255, 255, 255)) + + 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.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.moveImg = movingImg(self.screen, + "images/toggle1.png", + pos=vec2d(0, 0)) + + self.ball = circles(self.screen, + pos=vec2d(25, 25), + radius=25) + + 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.clock = pygame.time.Clock() + self.paused = False + + #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 + + self.field_rect = pygame.Rect(0, 0, self.SCREEN_WIDTH, self.SCREEN_HEIGHT) + + self.options = dict(debug=True, + draw_grid=False) + + print("Done setting game options, exiting Game init.") + def xy2coord(self, pos): """ Convert a (x, y) pair to a (nrow, ncol) coordinate """ x, y = (pos[0] - self.field_rect.left, pos[1] - self.field_rect.top) return (int(y) / self.GRID_SIZE, int(x) / self.GRID_SIZE) - + def coord2xy_mid(self, coord): """ Convert a (nrow, ncol) coordinate to a (x, y) pair, where x,y is the middle of the square at the coord """ nrow, ncol = coord return ( - self.field_rect.left + ncol * self.GRID_SIZE + self.GRID_SIZE / 2, + self.field_rect.left + ncol * self.GRID_SIZE + self.GRID_SIZE / 2, self.field_rect.top + nrow * self.GRID_SIZE + self.GRID_SIZE / 2) - + def get_field_rect(self): """ Return the internal field rect - the rect of the game - field exluding its border. + field excluding its border. """ return self.field_box.get_internal_rect() - + def draw_background(self): img_rect = self.tile_img.get_rect() nrows = int(self.screen.get_height() / img_rect.height) + 1 ncols = int(self.screen.get_width() / img_rect.width) + 1 - + for y in range(nrows): for x in range(ncols): - img_rect.topleft = (x * img_rect.width, + img_rect.topleft = (x * img_rect.width, y * img_rect.height) self.screen.blit(self.tile_img, img_rect) - + def draw_grid(self): for y in range(self.grid_nrows + 1): pygame.draw.line( @@ -175,70 +159,68 @@ def draw_grid(self): pygame.Color(50, 50, 50), (self.field_rect.left, self.field_rect.top + y * self.GRID_SIZE - 1), (self.field_rect.right - 1, self.field_rect.top + y * self.GRID_SIZE - 1)) - + for x in range(self.grid_ncols + 1): pygame.draw.line( self.screen, pygame.Color(50, 50, 50), (self.field_rect.left + x * self.GRID_SIZE - 1, self.field_rect.top), (self.field_rect.left + x * self.GRID_SIZE - 1, self.field_rect.bottom - 1)) - + def draw(self): #draw background image self.draw_background() - + #decide if we should draw grid. if self.options['draw_grid']: self.draw_grid() - + 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 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: + if button.rect.collidepoint(event.pos): + button.click() + for entry in self.textEntries: + entry.mouse_click_event(event.pos) + + #pass temporarily disabled, don't think it does anything + + #entity events here. #update hud, counters, score, anything like that here if not self.paused: @@ -247,7 +229,7 @@ def run(self): #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() #actually flip Surface buffer @@ -258,7 +240,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..efad33a 100644 --- a/simpleanimation.py +++ b/simpleanimation.py @@ -6,43 +6,43 @@ class SimpleAnimation(object): """ A simple animation. Scrolls cyclically through a list of - images, drawing them onto the screen in the same posision. + images, drawing them onto the screen in the same position. """ - def __init__(self, screen, pos, images, scroll_period, duration=-1): - """ Create an animation. - + def __init__(self, screen, pos, images, duration, scroll_speed): + """ Create an animation. + screen: The screen to which the animation will be drawn pos: Position on the screen - images: + images: A list of surface objects to cyclically scroll through - scroll_period: + scroll_period: Scrolling period (in ms) duration: - Duration of the animation (in ms). If -1, the + 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 - self.img_ptr = 0 - self.active = True + self.images = images self.duration = duration - - self.scroll_timer = Timer(scroll_period, self._advance_img) - self.active_timer = Timer(duration, self._inactivate, True) - + self.scroll_speed = scroll_speed + self.active = True + self.img_ptr = 0 + self.scroll_timer = Timer(self.scroll_speed, self._advance_img) + self.active_timer = Timer(self.duration, self._inactivate) + def is_active(self): """ Is the animation active ? - + An animation is active from the moment of its creation and until the duration has passed. """ return self.active - + def update(self, time_passed): """ Update the animation's state. - + time_passed: The time passed (in ms) since the previous update. """ @@ -56,39 +56,35 @@ def draw(self): cur_img = self.images[self.img_ptr] self.draw_rect = cur_img.get_rect().move(self.pos) self.screen.blit(cur_img, self.draw_rect) - + def _inactivate(self): if self.duration >= 0: self.active = False - + 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) - - 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) - - 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() +if __name__ == "__main__": + print("starting animation.") + 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)] + + 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() diff --git a/widgets.py b/widgets.py index 2cd4c2f..ad55427 100644 --- a/widgets.py +++ b/widgets.py @@ -1,9 +1,9 @@ """ Simplistic "GUI Widgets" for a Pygame screen. - + Most widgets receive a 'surface' argument in the constructor. - This is the pygame surface to which the widget will draw + This is the pygame surface to which the widget will draw itself when it's draw() method is called. - + Unless otherwise specified, all rectangles are pygame.Rect instances, and all colors are pygame.Color instances. """ @@ -22,11 +22,11 @@ class LayoutError(WidgetError): pass class Box(object): """ A rectangular box. Has a background color, and can have a border of a different color. - + Has a concept of the "internal rect". This is the rect inside the border (not including the border itself). """ - def __init__(self, + def __init__(self, surface, rect, bgcolor, @@ -35,11 +35,11 @@ def __init__(self, """ rect: The (outer) rectangle defining the location and size of the box on the surface. - bgcolor: + bgcolor: The background color border_width: - Width of the border. If 0, no border is drawn. - If > 0, the border is drawn inside the bounding + Width of the border. If 0, no border is drawn. + If > 0, the border is drawn inside the bounding rect of the widget (so take this into account when computing internal space of the box). border_color: @@ -50,16 +50,16 @@ def __init__(self, self.bgcolor = bgcolor self.border_width = border_width self.border_color = border_color - + # Internal rectangle self.in_rect = Rect( self.rect.left + self.border_width, self.rect.top + self.border_width, self.rect.width - self.border_width * 2, self.rect.height - self.border_width * 2) - + def draw(self): - pygame.draw.rect(self.surface, self.border_color, self.rect) + pygame.draw.rect(self.surface, self.border_color, self.rect) pygame.draw.rect(self.surface, self.bgcolor, self.in_rect) def get_internal_rect(self): @@ -69,13 +69,13 @@ def get_internal_rect(self): class MessageBoard(object): - """ A rectangular "board" for displaying messages on the + """ A rectangular "board" for displaying messages on the screen. Uses a Box with text drawn inside. - - The text is a list of lines. It can be retrieved and + + The text is a list of lines. It can be retrieved and changed with the .text attribute. """ - def __init__(self, + def __init__(self, surface, rect, text, @@ -85,9 +85,9 @@ def __init__(self, bgcolor=Color('gray25'), border_width=0, border_color=Color('black')): - """ rect, bgcolor, border_width, border_color have the + """ rect, bgcolor, border_width, border_color have the same meaning as in the Box widget. - + text: The initial text of the message board. font: @@ -103,14 +103,14 @@ def __init__(self, self.font = pygame.font.SysFont(*font) self.font_color = font_color self.border_width = border_width - + self.box = Box(surface, rect, bgcolor, border_width, border_color) - + def draw(self): #Draw the surrounding box self.box.draw() - - # Internal drawing rectangle of the box + + # Internal drawing rectangle of the box # # # Need a method that takes in a width and height of space required for text and padding @@ -118,104 +118,104 @@ def draw(self): # Calculate required space for text+padding+border # utils.get_messagebox_coords(width, height, padding) # returns x, y, height, width? - + # Internal rectangle where the text is actually drawn text_rect = Rect( self.rect.left + self.border_width, self.rect.top + self.border_width, self.rect.width - self.border_width * 2, self.rect.height - self.border_width * 2) - + x_pos = text_rect.left - y_pos = text_rect.top - + y_pos = text_rect.top + # Render all the lines of text one below the other # for line in self.text: line_sf = self.font.render(line, True, self.font_color, self.bgcolor) - + #test if we can fit text into the MessageBoard + padding - + if ( line_sf.get_width() + x_pos + self.padding > self.rect.right or line_sf.get_height() + y_pos + self.padding > self.rect.bottom): raise LayoutError('Cannot fit line "%s" in widget' % line) - + self.surface.blit(line_sf, (x_pos+self.padding, y_pos+self.padding)) y_pos += line_sf.get_height() class Button(object): - """ Employs some crap from Box to draw a rectangular button, - has some methods to handle click events. - """ - - # needs to be replaced. - (UNCLICKED, CLICKED) = range(2) - - 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" - self.surface = surface - self.pos = pos - self.btntype = btntype - self.imgnames = imgnames - self.text = text - self.textcolor = textcolor - self.textimg = textimg - self.padding = padding - self.attached = attached - self.state = Button.UNCLICKED - self.toggle = 0 - - #load images - self.imgs = [] - for name in self.imgnames: - img = pygame.image.load(name).convert_alpha() - #img = img.set_colorkey((255,255,255)) - #it would be nice to make the images transparent, - #but it throws an error not worth fighting - self.imgs.append(img) - - 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) - - #creates a text label to place in the middle of the button - font = pygame.font.SysFont("Times New Roman", 25) - self.textOverlay = font.render(self.text,1,self.textcolor) - self.textSize = vec2d(font.size(self.text)) - self.textRect = Rect(self.pos.x+self.imgwidth/2-self.textSize.x/2,self.pos.y+self.imgheight/2-self.textSize.y/2,0,0) - - - def draw(self): - if self.btntype == "Close": - self.surface.blit(self.imgs[0], self.rect) - elif self.btntype == "Toggle": - self.surface.blit(self.imgs[self.toggle], self.rect) - if self.toggle == self.textimg: - self.surface.blit(self.textOverlay, self.textRect) - - - def mouse_click_event(self, pos): - if self.btntype == "Close": - if self._point_is_inside(vec2d(pos)): - self.state = Button.CLICKED - elif self.btntype == "Toggle": - if self._point_is_inside(vec2d(pos)): - self.state = not self.state - self.toggle = not self.toggle - self.imgwidth, self.imgheight = self.imgs[self.toggle].get_size() - self.rect = Rect(self.pos.x, self.pos.y, self.imgwidth, self.imgheight) - self.textRect = Rect(self.pos.x+self.imgwidth/2-self.textSize.x/2,self.pos.y+self.imgheight/2-self.textSize.y/2,0,0) - elif self.btntype == "Action": - if self._point_is_inside(vec2d(pos)): - self.count = 100 - expl = simpleanimation.start() - print "Action" - - def _point_is_inside(self, mpos): - if mpos.x > self.rect.x and mpos.x < self.rect.x+self.imgwidth: - if mpos.y > self.rect.y and mpos.y < self.rect.y+self.imgheight: - return True + """ Employs some crap from Box to draw a rectangular button, + has some methods to handle click events. + """ + + # needs to be replaced. + (UNCLICKED, CLICKED) = range(2) + + def __init__(self, surface, pos, btntype, imgnames, attached, text="", textcolor=(0, 0, 0)): + self.surface = surface + self.pos = pos + self.btntype = btntype + self.imgs = [pygame.image.load(img).convert_alpha() for img in imgnames] + self.img = self.imgs[0] + self.imgwidth, self.imgheight = self.img.get_size() + self.rect = pygame.Rect(self.pos.x, self.pos.y, self.imgwidth, self.imgheight) + self.attached = attached + self.text = text + self.textcolor = textcolor + self.font = pygame.font.SysFont("Times New Roman", 25) + self.textOverlay = self.font.render(self.text, 1, self.textcolor) + self.textSize = vec2d(self.font.size(self.text)) + self.textRect = pygame.Rect(self.pos.x + self.imgwidth / 2 - self.textSize.x / 2, self.pos.y + self.imgheight / 2 - self.textSize.y / 2, 0, 0) + self.state = False + self.toggle = False + + print("In button init method") + print("Image dimensions are: " + str(self.imgwidth) + ", " + str(self.imgheight)) + + def click(self): + print("Button clicked") + # Add your button click handling logic here + + def load_images(self): + self.imgs = [] + for name in self.imgnames: + img = pygame.image.load(name).convert_alpha() + self.imgs.append(img) + self.imgwidth, self.imgheight = self.imgs[self.toggle].get_size() + self.rect = pygame.Rect(self.pos.x, self.pos.y, self.imgwidth, 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) + self.textOverlay = font.render(self.text, 1, self.textcolor) + self.textSize = vec2d(font.size(self.text)) + self.textRect = pygame.Rect(self.pos.x + self.imgwidth / 2 - self.textSize.x / 2, self.pos.y + self.imgheight / 2 - self.textSize.y / 2, 0, 0) + + def draw(self): + if self.btntype == "Close": + self.surface.blit(self.imgs[0], self.rect) + elif self.btntype == "Toggle": + self.surface.blit(self.imgs[self.toggle], self.rect) + if self.toggle == self.textSize: + self.surface.blit(self.textOverlay, self.textRect) + + def mouse_click_event(self, pos): + if self.btntype == "Close": + if self._point_is_inside(vec2d(pos)): + self.state = Button.CLICKED + elif self.btntype == "Toggle": + if self._point_is_inside(vec2d(pos)): + self.state = not self.state + self.toggle = not self.toggle + self.imgwidth, self.imgheight = self.imgs[self.toggle].get_size() + self.rect = pygame.Rect(self.pos.x, self.pos.y, self.imgwidth, self.imgheight) + self.textRect = pygame.Rect(self.pos.x + self.imgwidth / 2 - self.textSize.x / 2, self.pos.y + self.imgheight / 2 - self.textSize.y / 2, 0, 0) + + def _point_is_inside(self, mpos): + if mpos.x > self.rect.x and mpos.x < self.rect.x + self.imgwidth: + if mpos.y > self.rect.y and mpos.y < self.rect.y + self.imgheight: + return True + return False class Images(object): """ allows for unclickable images """ @@ -226,12 +226,12 @@ 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) - self.rect = Rect(self.pos.x, self.pos.y, self.imgwidth, 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): #rotations didn't work well on diagonals. Could use a smoother method - #but it's really only to test image draw and pause easier + #but it's really only to test image draw and pause easier if self.imgtype == "Spinner": """ spinners make a full rotation every second """ x, y, ix, iy = self.rect @@ -252,9 +252,9 @@ def draw(self): self.surface.blit(self.img, self.rect) class textEntry(object): - """ allows for reading input from the user """ + """ 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 @@ -265,13 +265,13 @@ def __init__(self, surface, pos=vec2d(0, 0), size = vec2d(200,50), text="", text self.rect = Rect(self.pos.x, self.pos.y, self.size.x, self.size.y) self.lastKey = "" self.delay = 1 - + #creates a text label to place in the middle of the rectangle self.font = pygame.font.SysFont("Times New Roman", 25) self.textOverlay = self.font.render(self.text,1,self.textcolor) self.textSize = vec2d(self.font.size(self.text)) self.textRect = Rect(self.pos.x, self.pos.y, self.textSize.x, self.textSize.y) - + def draw(self): if self.clicked: if pygame.key.get_focused(): @@ -299,16 +299,16 @@ def draw(self): self.text = self.text[:-1] self.delay = 0 self.lastKey = key - + self.textOverlay = self.font.render(self.text,1,self.textcolor) pygame.draw.rect(self.surface, (255,255,255), self.rect) self.surface.blit(self.textOverlay, self.textRect) - + def mouse_click_event(self, pos): if self._point_is_inside(vec2d(pos)): self.clicked = not self.clicked - + def _point_is_inside(self, mpos): if mpos.x > self.pos.x and mpos.x < self.pos.x+self.size.x: if mpos.y > self.pos.y and mpos.y < self.pos.y+self.size.y: @@ -325,7 +325,7 @@ def __init__(self, surface, pos=vec2d(0,0), size=vec2d(20,20), color=(255,0,0), self.gravity = gravity self.rect = Rect(self.pos.x, self.pos.y, self.size.x, self.size.y) self.color = color - + def draw(self): if self.pos.x + self.size.x > self.surfaceSize.x or self.pos.x < 0: self.speed.x *= -1 @@ -346,7 +346,7 @@ def __init__(self, surface, image, pos=vec2d(0,0), speed=vec2d(1,0), gravity=1): self.gravity = gravity self.size = vec2d(self.image.get_size()) self.rect = Rect(self.pos.x, self.pos.y, self.size.x, self.size.y) - + def draw(self): if self.pos.x + self.size.x > self.surfaceSize.x or self.pos.x < 0: self.speed.x *= -1 @@ -356,7 +356,7 @@ def draw(self): self.pos.y += self.speed.y self.rect = Rect(self.pos.x, self.pos.y, self.size.x, self.size.y) self.surface.blit(self.image, self.rect) - + class circles(object): def __init__(self, surface, pos=vec2d(10,10), radius=5, bgcolor=(0,0,0)): """ creates a simple useless circle """ @@ -364,6 +364,6 @@ def __init__(self, surface, pos=vec2d(10,10), radius=5, bgcolor=(0,0,0)): self.pos = pos self.radius = radius self.bgcolor = bgcolor - + def draw(self): pygame.draw.circle(self.surface, self.bgcolor, self.pos, self.radius) \ No newline at end of file