• Content count

  • Joined

  • Last visited

Community Reputation

151 Neutral

About rlink

  • Rank
  1. I made this simple pong game in Python using Pygame. It consists of two files, one which contains the main game code and the other contains the code for the classes. I'd like some advice on how to improve this code.   # classes.py # 3/19/2013 import pygame import random # Paddle properties PADDLE_WIDTH = 25 PADDLE_HEIGHT = 100 PADDLE_COLOR = (0, 0, 0) PADDLE_SPEED = 6 # Ball properties BALL_RADIUS = 20 BALL_COLOR = (0, 0, 0) BALL_INITIAL_VX = -5 BALL_INITIAL_VY = -5 # Base object class class Object: # Initializes the object with coordinates, size and color def __init__(self, x, y, w, h, color): self.x = x self.y = y self.w = w self.h = h self.vx = 0 self.vy = 0 self.color = color # Updates object by moving it and checking if it's in screen range def update(self, screenWidth, screenHeight): self.x += self.vx self.y += self.vy if self.x < 0: self.x = 0 if self.y < 0: self.y = 0 if self.x > screenWidth - self.w: self.x = screenWidth - self.w if self.y > screenHeight - self.h: self.y = screenHeight - self.h # Must be implemented by child classes def draw(self, surface): pass # Returns whether object collides with another object (rectangular collision detection) def collides(self, obj): return self.y < obj.y + obj.h and self.y + self.h > obj.y and self.x < obj.x + obj.w and self.x + self.w > obj.x # Called when object collides with anoher object, must be implemented by child classes def onCollide(self, obj): pass # Paddle class class Paddle(Object): # Initializes Paddle object def __init__(self, x, y): super(Paddle, self).__init__(x, y, PADDLE_WIDTH, PADDLE_HEIGHT, PADDLE_COLOR) # Draws paddle with a rectangle def draw(self, surface): pygame.draw.rect(surface, self.color, (self.x, self.y, self.w, self.h)) # Moves paddle up def moveUp(self): self.vy -= PADDLE_SPEED # Moves paddle down def moveDown(self): self.vy = PADDLE_SPEED # Stops moving the paddle def stopMoving(self): self.vy = 0 # ComputerPaddle class class ComputerPaddle(Paddle): # Initializes ComputerPaddle object def __init__(self, x, y): super(ComputerPaddle, self).__init__(x, y) # Adjust Y-velocity based on speed and direction of ball def update(self, ball, screenWidth, screenHeight): super(ComputerPaddle, self).update(screenWidth, screenHeight) if ball.vx < 0: self.stopMoving() return ballX = ball.x ballY = ball.y ballVX = ball.vx ballVY = ball.vy while ballX + ball.w < self.x: ballX += ballVX ballY += ballVY if ballY < 0 or ballY > screenHeight - ball.h: ballVY = -ballVY if ballY > self.y + self.h: self.moveDown() elif ballY + ball.h < self.y: self.moveUp() else: self.stopMoving() # Ball class class Ball(Object): # Initializes the Ball object along with initial velocities def __init__(self, x, y): super(Ball, self).__init__(x, y, BALL_RADIUS, BALL_RADIUS, BALL_COLOR) self.startX = x self.startY = y self.vx = BALL_INITIAL_VX self.vy = BALL_INITIAL_VY # Updates the ball object, if it hits screen edge then negate velocity def update(self, screenWidth, screenHeight): super(Ball, self).update(screenWidth, screenHeight) if self.x == 0 or self.x == screenWidth - self.w: self.vx = -self.vx if self.y == 0 or self.y == screenHeight - self.h: self.vy = -self.vy # Resets the ball back to its initial coordinates def reset(self): self.x = self.startX self.y = self.startY self.vx = BALL_INITIAL_VX self.vy = BALL_INITIAL_VY # Draws a circle onto screen to represent the ball def draw(self, surface): pygame.draw.circle(surface, self.color, (self.x, self.y), self.w) # If ball collides with another object, then negate both velocities by a random amount def onCollide(self, obj): # If ball is "inside" of paddle, then reposition it so it's just outside the paddle if self.x < obj.x + obj.w: self.x = obj.x + obj.w elif self.x + self.w > obj.x: self.x = obj.x - self.w if self.y < obj.y + obj.h: self.y = obj.y + obj.h elif self.y + self.h > obj.y: self.y = obj.y - self.h rx = int(random.uniform(-3, 5)) ry = int(random.uniform(-2, 4)) self.vx = -self.vx + rx self.vy = -self.vy + ry   # game.py # 3/19/2013 import pygame, sys, os from pygame.locals import * from classes import * # Basic constants FPS = 60 SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 SCREEN_BACKGROUND = (255, 255, 255) # Game states GSTATE_START = 1 GSTATE_PLAYING = 2 GSTATE_VICTORY = 3 # Some global variables gameState = GSTATE_START playerScore = 0 computerScore = 0 # Handle all inputs to application def input(events, obj): global gameState for event in events: if event.type == QUIT: return False elif event.type == KEYDOWN: if event.key == K_w: obj.moveUp() elif event.key == K_s: obj.moveDown() elif event.type == KEYUP: if event.key == K_w or event.key == K_s: obj.stopMoving() elif event.key == K_SPACE and (gameState == GSTATE_START or gameState == GSTATE_VICTORY): gameState = GSTATE_PLAYING return True # Main drawing function def draw(screen, font, gameObjects): global gameState global playerScore global computerScore screen.fill(SCREEN_BACKGROUND) if gameState != GSTATE_PLAYING: if gameState == GSTATE_START: message = "Press Space to start playing" elif gameState == GSTATE_VICTORY: message = "Round complete, press Space to continue" screen.blit(font.render(message, 1, (0, 0, 0)), ((SCREEN_WIDTH / 2) - 200, SCREEN_HEIGHT / 2)) screen.blit(font.render("Player: %d" % playerScore, 1, (0, 0, 0)), (int(SCREEN_WIDTH / 4), 20)) screen.blit(font.render("Computer: %d" % computerScore, 1, (0, 0, 0)), (int(SCREEN_WIDTH * 3 / 4), 20)) for obj in gameObjects: obj.draw(screen) pygame.display.flip() # Main update function def update(p1, p2, ball): global gameState global playerScore global computerScore # Do not update if game hasn't started yet if gameState != GSTATE_PLAYING: return # Update game objects p1.update(SCREEN_WIDTH, SCREEN_HEIGHT) p2.update(ball, SCREEN_WIDTH, SCREEN_HEIGHT) ball.update(SCREEN_WIDTH, SCREEN_HEIGHT) # Check if the ball collides with either of the two paddles if ball.collides(p1): ball.onCollide(p1) elif ball.collides(p2): ball.onCollide(p2) # Check for victory conditions if ball.x == 0: computerScore += 1 gameState = GSTATE_VICTORY ball.reset() elif ball.x == SCREEN_WIDTH - ball.w: playerScore += 1 gameState = GSTATE_VICTORY ball.reset() # Main function, entry point of application def main(): # Initialize pygame pygame.init() pygame.display.set_caption('Pygame Pong (RL)') # Setup window, screen, clock and fonts window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) screen = pygame.display.get_surface() clock = pygame.time.Clock() font = pygame.font.Font('freesansbold.ttf', 20) # Initialize game objects paddlePlayer = Paddle(0, int((SCREEN_HEIGHT - PADDLE_HEIGHT) / 2)) paddleComputer = ComputerPaddle(int(SCREEN_WIDTH - PADDLE_WIDTH), int((SCREEN_HEIGHT - PADDLE_HEIGHT) / 2)) ball = Ball(int((SCREEN_WIDTH - BALL_RADIUS) / 2), int((SCREEN_HEIGHT - BALL_RADIUS) / 2)) # Game loop while input(pygame.event.get(), paddlePlayer): update(paddlePlayer, paddleComputer, ball) draw(screen, font, [ paddlePlayer, paddleComputer, ball ]) clock.tick(FPS) # Exit game pygame.quit() sys.exit() # Start program if __name__ == "__main__": main()   Also there is a bug in the game where when the ball gets to the right end of the screen, it always counts as a computer victory even if it touches the computer paddle. Any idea how to fix it?
  2. I have a small sub-site in my website (a subdomain of a domain) that only I know about. It logs the IP addresses of all page requests. I'm getting requests from the IP and I have no clue why. No one knows about this site and I'm absolutely sure it's not mine.   EDIT: This is the user agent -- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; MS-RTC LM 8; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)   What could it be?
  3. I have several ideas for some games I think would be really fun, so I'd like to get started with game development and learn how to make games the right way. I have some experience with programming, but most of my experience lies within web development which is what I've been doing for the past 5 years. I've dabbled with game development in the past with C++ and the SDL library (before I started webdev) a little, but nothing serious -- just a pong and tetris game, and a (very) simple 2d shooter.   But now I'd like to take this more seriously, and actually get started on real game development. The problem is I'm not sure what to do right now. Should I re-learn C++ (I haven't used it for several years) and a graphics library like DirectX or OpenGL, or should I use another language like Python? Also now that HTML5 technology is supported on most up-to-date browsers, would developing games with a library like LimeJS be practical?   Any ideas?  
  4. So recently I finished this book called "The Elements of Computing Systems" where you learn how to build a computer system from the ground up, starting from NAND gates and working your way up to a fully functioning virtual computer. Basically what happens is you start from primitive logic gates, then use those to implement combinational and sequential logic, then create a CPU with a machine language followed by an intermediate (VM) language, and finally a semi-OOP programming language called jack.   I really enjoyed the book (it's available here btw: http://www.nand2tetris.org/course.php it's also called "From Nand to Tetris") and would like to create something similar that extends upon the capabilities of this computer system, except as a web application with JavaScript and possibly a PHP back-end if necessary. However, I'm not sure how to go about doing this and am also unsure if it's a feasible project.   I created a small document explaining how it should work here: https://docs.google.com/document/d/1vGX6ec1ADPEtkq2sMrrdqBv7qtzplbuORzuUgWTj1GA/edit Only difference is that I added in some stuff like bitwise operators into the machine and VM language so it doesn't have to be implemented by the OS. EDIT: Also I changed the layout of the RAM cause I want this to be a 32bit system instead of 16bit. I don't think 65565 words of memory (that's for both ROM and RAM) is enough if I'm going to write anything significant in Jack.   I'd like to extend upon this by adding:   1. Computer screen -- probably 640x480 with support for rgba values (the original only supports black & white) 2. Keyboard & mouse input -- ascii codes for keys along with keydown, keyup, etc and mouse button state and x,y coordinates 3. An actual filesystem instead of storing everything in RAM... not sure how hard this will be to implement though 4. A second programming language apart from Jack, that also compiles to intermediate code... might look something like python 5. Ability to output sound 6. Networking, able to communicate with other PC emulators (running on a separate browser tab, everything is just gonna be local for now) through a simple protocol.     After it's complete I'm probably gonna write some basic apps for it, things like a paint tool, notepad, calculator, some games, etc.   I'm not sure if #3, 4, and 6 are actually possible though. But anyway I have about 3 years experience with Web development and wouldn't mind spending a good amount of time implementing this, although I'm unsure as to how long this will take. Sorry if this sounds overly-ambitious or anything, but I've wanted to make something like this for quite a while.