X-13 Posted June 26, 2013 Posted June 26, 2013 I've nearly made pong... I r teh best! Just need to figure out enemy paddle AI [along with getting user_paddle stuff actually coded] and sort out a scoring system. So, basically, I have a ball bouncing around on it's own. Go me!
tmcd35 Posted June 27, 2013 Posted June 27, 2013 So come on, lets see some code I actually haven't done much since the last post. I drew a stickman in paint and got Python to display him on screen - yay a sprite!!!
X-13 Posted June 27, 2013 Author Posted June 27, 2013 import sys, pygame pygame.init() size = width, height = 640, 480 speed = [2,2] black = (0, 0, 0) white = (255, 255, 255) screen = pygame.display.set_mode(size) ball = pygame.image.load("ball.bmp") ballrect = ball.get_rect() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(white) screen.blit(ball, ballrect) pygame.display.flip() "ball.bmp" is a 20x20 black square saved in the same place as the script. Even though I didn't code for it, it has the extra ability to crash on exit. [Window stops responding.] I need to go back through it to make sure I fully understand what does what.
tmcd35 Posted June 27, 2013 Posted June 27, 2013 One thing I had drummed into me at college and uni was "comment everything", I still pretty poor at at it, but it does help when you go back and re-read a bit of code and bug finding. Especially when the code starts getting really long.
X-13 Posted June 27, 2013 Author Posted June 27, 2013 I generally do that, but just burned through this quick. Hence the need to go back through it now... [i also just pinched a bit of your code "pygame.display.set_caption"] Le EDIT: Just added some controls esc/q to close it.plus, I pre-defined the up/down controls and space to start.
X-13 Posted June 27, 2013 Author Posted June 27, 2013 OK, I have half of the controls done. [Down works.] if event.type == pygame.KEYDOWN: if event.key in (down_): ballrect = ballrect.move(speed) speed[0] = -speed[0] if event.key in (up_): ballrect = ballrect.move(speed) speed[0] = [sOMETHING] What does [sOMETHING] need to be? In other words, what's the opposite of -speed[0]? [it's not +speed[0]...]
tmcd35 Posted June 27, 2013 Posted June 27, 2013 speed = [2,2] speed[0] = -speed[0] Slightly confused by the choice of variable name. Are you controlling speed or direction? Is the value of speed x,y screen coordinates - or the rate of which the x,y cordinates are being changed by ballrect.move(speed) ? Also doesn't this line negate itself? speed[0] = -speed[0] So that after that line 'speed[0]' is always equal to '0'? My gut instinct say the lines would probably read better... if event.type == pygame.KEYDOWN: if event.key in (down_): speed[0] = speed[0] - 1 if event.key in (up_): speed[0] = speed[0] + 1 ballrect = ballrect.move(speed)
X-13 Posted June 27, 2013 Author Posted June 27, 2013 NAILED IT! # Paddle controls if event.type == pygame.KEYDOWN: if event.key in (down_): if paddlerect.bottom != game.bottom: paddlerect = paddlerect.move(0, y) if event.key in (up_): if paddlerect.top != game.top: paddlerect = paddlerect.move(0, -y) I should point out, I renamed "speed" to "position" and position = (x, y) Moves as smooth as butter and stops at the top/bottom.
X-13 Posted June 28, 2013 Author Posted June 28, 2013 Here you go, gents. import sys import pygame pygame.init() # Set global defaults size = width, height = 640, 480 x = (1) y = (1) black = (0, 0, 0) white = (255, 255, 255) clock = pygame.time.Clock() # Set display settings screen = pygame.display.set_mode(size) game = screen.get_rect() pygame.display.set_caption ("Pong - Made by X-13") # asset defaults paddle = pygame.image.load("paddle.bmp") paddlerect = paddle.get_rect() paddlerect.centery = (height / 2) ball = pygame.image.load("ball.bmp") ballrect = ball.get_rect() #controls exit_ = pygame.K_ESCAPE, pygame.K_q up_ = pygame.K_UP, pygame.K_w down_ = pygame.K_DOWN, pygame.K_s start_ = pygame.K_SPACE # Main loop while 1: # You need an exit strategy! for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key in (exit_): sys.exit(0) # Ball start location and behaviour ballrect.left = paddlerect.right ballrect.centery = paddlerect.centery # Paddle controls if event.type == pygame.KEYDOWN: if event.key in (down_): if paddlerect.bottom != game.bottom: paddlerect = paddlerect.move(0, y) if event.key in (up_): if paddlerect.top != game.top: paddlerect = paddlerect.move(0, -y) # Draw everything that needs to be seen screen.fill(white) screen.blit(paddle, paddlerect) screen.blit(ball, ballrect) pygame.display.flip() This is where I'm up to only a few changes here and there. I'm currently working on the ball physics with a "press space to start" trigger [The one I had I didn't really understand, so I'm starting again.] ATM, the ball sticks to the paddle as it moves. The plan is to get in position, press space then start playing.
Recommended Posts