Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

279 Neutral

About mrpeed

  • Rank

Personal Information

  • Role
  • Interests

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. Should be #include "SDL.h", Instead of #include <SDL.h>? Looks like it can't find the header file. Where is SDL located on your machine? This may help:
  2. I just started a new position as a programmer and I'm trying to balance doing things well and in a timely manner. The old code bases where I work now are a disaster and I want to at least set a good example going forward. how can I know when I'm over engineering a problem? I read a lot about design patterns and coding principles, but I feel like when I actually code I take forever due to wanting to implement something well and clean. Any advice?
  3. mrpeed

    Java Accessing Inner Class Attributes

    At the class room level it really does not matter IMO, both solutions would be equal (so I would go with the one that is more readable). The reason I would use a set/get method is if in case I decided later that I needed some extra processing done whenever I set or get a given variable, I could just throw it in the method. And you can think of an inner class as just a folder within a folder. The parent class is the outer most folder, and the inner class is the inside the outer folder. Each folder has it's own variables, methods, classes, etc.In the end it's all just storage.
  4. mrpeed

    shooting bullets

    I found this tutorial helpful.
  5. You will probably need a unique use_item function for each item if the behavior is unique for each. I don't code in C++ but perhaps you could create separate use_item functions and pass them as an argument upon object creation? Ex in Python: class UseableItem(InventoryItem): def __init__(self, use_item_function): self.use_item_function = use_item_function health_potion = UseableItem(use_health_potion_function) So you would use pointers to functions I guess? Basically, you would have one UseableItem class that can receive a use_item function upon creation. And perhaps you could pass parameters to the function you send UseableItem upon creation as well so you don't need separate functions for lets say different levels of a health potion. Note again I'm a Python developer and I don't have experience with Cry Engine so I'm just providing whatever insight I can. Basically, if you can change the use_item function into an argument the UseableItem can receive upon creation than you can avoid lots of duplication of code. Please let me know if I was clear or if I did not understand your question correctly. Or if anyone thinks this is a bad approach...
  6. Does anyone have any recommendations for good books on game engine design for 2D games that a beginner would comprehend? I'm not looking for something pertaining to a specific language or anything, but something that will give me a good understanding of game engines in general that I can then apply to my own projects. Basically, I'm self taught up to this point and would like to expand my knowledge. Thanks.
  7. mrpeed

    How Much is Too Much?

    Too much is too much when you are unhappy and not enjoying yourself. Sometimes, we like the idea of doing things but not actually doing them. If you find that doing all those different things is making you unhappy, than you should reconsider how you spend your time. But if you genuinely enjoy what you do, then by all means keep doing it. Well first, if you are talking about improvement you need to make sure you are practicing properly and that you are applying what you practice to actual projects. You can spend lots of time doing things and not get any better because you aren't challenging yourself properly. Also, you can evenly divide your time among the different skills you mentioned to make sure you aren't "suffering" in any area. But as your life gets more busy you will find that you may not have lots of time in a day, and doing everything may no longer be viable. In that case you should probably decide on which skills are more important to you and focus on those. If you focus on everything you may never master one thing, but that may be okay if that's not a goal for you.
  8. mrpeed

    Python code review for Snake

    I did a second draft of the file if you want to take a look. I plan to redo my input manager this week. Some questions: 1. I use lots of functions because I find reading something like draw_segment() easier than reading pygame.draw.rect(self.displaysurf, GREEN, segment.rect), even if the function is one line. Also, I find that smaller functions are easy to write tests for which I often do. And if I need to make a change in the future, I would know exactly where a change needs to go. Do you think that approach is overkill for this program or in general? 2. Do you think putting all of my methods in the Game class for a game of this size is bad? Again thanks for taking your time to look at my game! It has been super helpful. #Removed refresh(), terminate(), extend_snake(), draw_extender(), draw_snake() #Capitalized constants #Added spaces #Modified some methods #Now uses lists import sys import random import itertools import pygame import inputmanager FPS = 10 WINDOW_SIZE = (640, 480) CELL_SIZE = (32, 32) START_LOCATION = (320, 224) UP = "up" DOWN = "down" LEFT = "left" RIGHT = "right" BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0) class Segment: def __init__(self, rect, direction=None): self.rect = rect self.direction = direction class Game: def __init__(self): self.direction = None self.extender = None self.cell_locations = self.cell_locations = set( itertools.product( range(0, WINDOW_SIZE[0], CELL_SIZE[0]), range(0, WINDOW_SIZE[1], CELL_SIZE[1]) ) ) self.snake = [Segment(pygame.Rect(START_LOCATION, CELL_SIZE))] pygame.init() self.fps_clock = pygame.time.Clock() self.displaysurf = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("Snake") def main(self): while True: self.get_input() self.update() self.render() self.fps_clock.tick(FPS) def get_input(self): inputmanager.InputManager.get_events() inputmanager.InputManager.check_for_quit_event() inputmanager.InputManager.update_keyboard_key_state() inputmanager.InputManager.get_keyboard_input() def update(self): self.handle_input() self.update_snake_direction(self.direction) self.move_snake() if self.extender is None: snake_segments_locations = set(segment.rect.topleft for segment in self.snake) location = random.choice(list(self.cell_locations-snake_segments_locations)) ###Need to use set to subtract, and need to convert to list to use random.choice self.extender = pygame.Rect(location, CELL_SIZE) if self.head_segment_collided_with_extender(): self.extender = None self.add_segment_to_snake() if self.game_over(): self.snake = [Segment(pygame.Rect(START_LOCATION, CELL_SIZE))] self.direction = None self.extender = None def handle_input(self): if inputmanager.InputManager.quit: pygame.quit() sys.exit() if (inputmanager.InputManager.keyboard[pygame.K_UP] == inputmanager.InputManager.pressed and self.direction != DOWN): self.direction = UP elif (inputmanager.InputManager.keyboard[pygame.K_DOWN] == inputmanager.InputManager.pressed and self.direction != UP): self.direction = DOWN elif (inputmanager.InputManager.keyboard[pygame.K_LEFT] == inputmanager.InputManager.pressed and self.direction != RIGHT): self.direction = LEFT elif (inputmanager.InputManager.keyboard[pygame.K_RIGHT] == inputmanager.InputManager.pressed and self.direction != LEFT): self.direction = RIGHT def update_snake_direction(self, head_direction): for index in reversed(range(len(self.snake))): self.snake[index].direction = self.snake[index-1].direction self.snake[0].direction = head_direction def move_snake(self): for segment in self.snake: self.move_segment(segment) def move_segment(self, segment): move_amount = { UP: (0, -CELL_SIZE[1]), DOWN: (0, CELL_SIZE[1]), LEFT: (-CELL_SIZE[0], 0), RIGHT: (CELL_SIZE[0], 0) }.get(segment.direction, (0, 0)) segment.rect.move_ip(move_amount) def head_segment_collided_with_extender(self): return self.snake[0].rect.colliderect(self.extender) def add_segment_to_snake(self): topleft = { UP: (self.snake[-1].rect.x, self.snake[-1].rect.y+CELL_SIZE[1]), DOWN: (self.snake[-1].rect.x, self.snake[-1].rect.y-CELL_SIZE[1]), LEFT: (self.snake[-1].rect.x+CELL_SIZE[0], self.snake[-1].rect.y), RIGHT: (self.snake[-1].rect.x-CELL_SIZE[0], self.snake[-1].rect.y) }.get(self.snake[-1].direction, (0, 0)) self.snake.append(Segment(pygame.Rect(topleft, CELL_SIZE), self.snake[-1].direction)) def game_over(self): return (self.head_segment_collided_with_self() or self.head_segment_out_of_bounds()) def head_segment_collided_with_self(self): return any([self.snake[0].rect.colliderect(segment) for segment in self.snake[1:]]) def head_segment_out_of_bounds(self): return (self.snake[0].rect.x < 0 or self.snake[0].rect.y < 0 or self.snake[0].rect.x >= WINDOW_SIZE[0] or self.snake[0].rect.y >= WINDOW_SIZE[1]) def render(self): self.displaysurf.fill(BLACK) for segment in self.snake: pygame.draw.rect(self.displaysurf, GREEN, segment.rect) if self.extender is not None: pygame.draw.rect(self.displaysurf, RED, self.extender) pygame.display.update() if __name__ == "__main__": game = Game() game.main() @Alberth
  9. mrpeed

    Python code review for Snake

    Thanks so much for the reply, I'm going to review your comments and I'll get back with any additional questions.
  10. I wrote Snake in Python 3 using Pygame and was wondering if anyone can do a code review of it? If this is the appropriate fourm to post such a thing? Some things to mention: 1. I realize I could have used a dict in the place of my Segment class, but I decided to go with the class because it looked more clean to me. 2. I used recursion heavily, though I could have used a list instead. I decided to do it recursively for practice and fun (I don't use recursion often). 3. I don't have doc strings for any of my functions. 4. I probably could have used my get_all_snake_segment_locations function to avoid recursion. 5. I set fps to 10 to limit the speed of the game. Is this a bad way to do such a thing? 6. I attached an input manager I created and unit tests for my game for completeness. Though, I'm only asking the actual game to be reviewed, if you want to look at those you can. Also, note the unit tests are not complete yet for several functions I changed. 7. I really appreciate anyone who takes the time to give me feedback of any kind. This fourm has been a huge help to me and I'm grateful for everyone's insight! import sys import random import itertools import pygame import inputmanager class Segment: def __init__(self, rect, direction=None, parent=None, child=None): self.rect = rect self.direction = direction self.parent = parent self.child = child class Game: def __init__(self): pygame.init() self.fps_clock = pygame.time.Clock() self.fps = 10 self.window_size = (640, 480) self.displaysurf = pygame.display.set_mode(self.window_size) pygame.display.set_caption("Snake") self.cell_size = (32, 32) self.start_location = (320, 224) self.head_segment = Segment(pygame.Rect(self.start_location, self.cell_size)) self.up = "up" self.down = "down" self.left = "left" self.right = "right" = (0, 0, 0) = (0, 255, 0) = (255, 0, 0) self.direction = None self.extender = None self.cell_locations = set( itertools.product( range(0, self.window_size[0], self.cell_size[0]), range(0, self.window_size[1], self.cell_size[1]) ) ) def main(self): while True: self.get_input() self.update() self.render() self.fps_clock.tick(self.fps) def get_input(self): inputmanager.InputManager.get_events() inputmanager.InputManager.check_for_quit_event() inputmanager.InputManager.update_keyboard_key_state() inputmanager.InputManager.get_keyboard_input() def update(self): self.handle_input() self.update_snake_direction(self.head_segment, self.direction) self.move_snake(self.head_segment) if self.extender is None: self.add_extender_to_board() if self.head_segment_collided_with_extender(): self.extend_snake() if self.game_over(): self.refresh() def handle_input(self): if inputmanager.InputManager.quit: self.terminate() if (inputmanager.InputManager.keyboard[pygame.K_UP] == inputmanager.InputManager.pressed and self.direction != self.down): self.direction = self.up elif (inputmanager.InputManager.keyboard[pygame.K_DOWN] == inputmanager.InputManager.pressed and self.direction != self.up): self.direction = self.down elif (inputmanager.InputManager.keyboard[pygame.K_LEFT] == inputmanager.InputManager.pressed and self.direction != self.right): self.direction = self.left elif (inputmanager.InputManager.keyboard[pygame.K_RIGHT] == inputmanager.InputManager.pressed and self.direction != self.left): self.direction = self.right def terminate(self): pygame.quit() sys.exit() def update_snake_direction(self, segment, parent_direction): ###TEST if segment.child is not None: self.update_snake_direction(segment.child, parent_direction) if segment.parent is None: segment.direction = parent_direction else: segment.direction = segment.parent.direction def move_snake(self, segment): self.move_segment(segment) if segment.child is not None: self.move_snake(segment.child) def move_segment(self, segment): if segment.direction == self.up: segment.rect.move_ip(0, -self.cell_size[1]) elif segment.direction == self.down: segment.rect.move_ip(0, self.cell_size[1]) elif segment.direction == self.left: segment.rect.move_ip(-self.cell_size[0], 0) elif segment.direction == self.right: segment.rect.move_ip(self.cell_size[0], 0) def add_extender_to_board(self): snake_segments_locations = set(self.get_all_snake_segment_locations(self.head_segment)) location = random.choice(list(self.cell_locations-snake_segments_locations)) self.extender = pygame.Rect(location, self.cell_size) def get_all_snake_segment_locations(self, segment): yield segment.rect.topleft if segment.child is not None: yield from self.get_all_snake_segment_locations(segment.child) def head_segment_collided_with_extender(self): return self.head_segment.rect.colliderect(self.extender) def extend_snake(self): self.extender = None self.add_segment_to_snake(self.head_segment) def add_segment_to_snake(self, segment): if segment.child is None: if segment.direction == self.up: topleft = (segment.rect.x, segment.rect.y+self.cell_size[1]) elif segment.direction == self.down: topleft = (segment.rect.x, segment.rect.y-self.cell_size[1]) elif segment.direction == self.left: topleft = (segment.rect.x+self.cell_size[0], segment.rect.y) elif segment.direction == self.right: topleft = (segment.rect.x-self.cell_size[0], segment.rect.y) segment.child = Segment( pygame.Rect(topleft, self.cell_size), segment.direction, segment ) else: self.add_segment_to_snake(segment.child) def game_over(self): return any([ self.head_segment_collided_with_self(self.head_segment), self.head_segment_out_of_bounds() ]) def head_segment_collided_with_self(self, segment): if segment.child is not None: if self.head_segment.rect.colliderect(segment.child.rect): return True else: return self.head_segment_collided_with_self(segment.child) return False def head_segment_out_of_bounds(self): if (self.head_segment.rect.x < 0 or self.head_segment.rect.y < 0 or self.head_segment.rect.x >= self.window_size[0] or self.head_segment.rect.y >= self.window_size[1]): return True return False def refresh(self): self.head_segment = Segment(pygame.Rect(self.start_location, self.cell_size)) self.direction = None self.extender = None def render(self): self.displaysurf.fill( self.draw_snake(self.head_segment) if self.extender is not None: self.draw_extender() pygame.display.update() def draw_snake(self, segment): pygame.draw.rect(self.displaysurf,, segment.rect) if segment.child is not None: self.draw_snake(segment.child) def draw_extender(self): pygame.draw.rect(self.displaysurf,, self.extender) if __name__ == "__main__": game = Game() game.main()
  11. Lets say I have a character with a shooting animation that plays every time I hit the space key. I need to block input to assure that the animation finishes before another action is taken. What is a common approach to doing this? Right now, I'm using a variable that is a set to the total number of seconds the animation is. When the variable is zero player input is allowed again. Is there a better way? I'm using Python and Pygame for a 2D game.
  12. Currently, I'm working on recreating connect four for a portfolio of simple games. I'm also writing unit tests for all the code I can. Some of my code functions though only call other functions, or only draw to the screen... #EXAMPLES... def draw_refresh_button_text(self): font = pygame.font.Font(None, 24) text = font.render("Refresh", True, self.displaysurf.blit( text, ( self.refresh_button_rect.centerx-self.refresh_button_rect.centerx/self.refresh_button_rect.y, self.refresh_button_rect.centery-self.refresh_button_rect.centery/self.refresh_button_rect.x ) ) def draw_disks(self): for column in self.grid: for disk in column: pygame.draw.ellipse(self.displaysurf, disk['color'], disk['rect']) #ONLY CALLS OTHER FUNCTIONS... def get_input(self): inputmanager.InputManager.get_events() inputmanager.InputManager.check_for_quit_event() inputmanager.InputManager.update_keyboard_key_state() inputmanager.InputManager.get_keyboard_input() inputmanager.InputManager.update_mouse_button_state() inputmanager.InputManager.get_mouse_input() How would I go about writing tests for code like this? Should code like this even be tested? Should all code be tested? I'm using the Python unittest module to test. The code here uses Pygame.
  13. Is redrawing the entire screen every frame the common approach? Even if there are areas on the display that haven't changed? I'm using Pygame and I've read that you should only redraw the area of the screen that has been changed for performance reasons, though I'm not sure if this is unique to Pygame or even relevant to to Pygame anymore.
  14. Though not from Mapt/Packt, this is a good book to give a read (free online!).
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!