Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Overview

About this Group

A group for aspiring and accomplished game developers. Learn. Share. Code.
  1. What's new in this group
  2. I can't remember the exact syntax I used, but it was similar to that. I used rect instead of line, as I was trying to merge 2 things. [get_rect and the image] Also, I set the pixel width to 0. If pixel width is 0 it's supposed to fill it with the colour.
  3. I used this... pygame.draw.line(screen,black,[50,400],[200,400],10) To draw the platform in my code. Should be usable for a pong game. The arguements inside the braces are... (<varible for display graphics outputting to>, <colour of line in format (R, G, B)>, <start coordinates in format [x,y]>, <end cordinates in format [x,y], <pixel width>)
  4. I'm messing about with enemy AI and got to thinking about graphics. In python you can either import an image file or draw it. At the moment I'm using bmp files, but for what they are, it may be easier to use pygame.draw.rect [with surface.fill(black)] for the paddles and ball. [surface.fill can also be hardware accelerated, apparently.] Has anyone used pygame.draw to make graphics? If you have, could you lend a hand with syntax, I keep getting "invalid argument".
  5. 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.
  6. I'm using 3.3 as well, no problems as of yet.
  7. Out of curiosity, for those using Python at the moment, which build are you using? Some tutorials seem to say use version 2.7 yet others say use version 3.3. Apparently, some tutorials using 2.7 say the code may not work in 3.3? It's all a bit confusing! I'm got 3.3 at the moment and no issues so far...
  8. 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.
  9. 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)
  10. 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]...]
  11. haha yes you are correct teachs me to mess around on here when I should be doing other things
  12. 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.
  13. 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.
  14. I've moved onto pygame tutorials now, and theirs are OK to start with. Pygame Front Page — Pygame v1.9.2 documentation Some python/pygame stuff. Index of /~pconrad/cs5nm/topics [Plus, Python to Java. ]
  15. 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.
  16. Okay, so I've added a stickman sprite hand drawn in Paint (a 5year old could do better - always my problem with graphics). But I'm thinking, to do this right, the sprite needs to be implemented as a class... class coolPlayerDude: #player variables xPos = None yPos = None Collision = None Sprite = None Score = None #load sprite def loadSprite(filename): Sprite = pygame.image.load(filenae).convert() def displaySprite(): #return sprite and position details to screen.nlit def setPlayerX(X): xPos = X def setPlayerY(Y): yPos = Y Okay, that's just some thoughts. Totally untested code - probably have time for a proper stab at this on Sunday.
  17. 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!!!
  18. 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!
  19. @CAM at the moment. It's super easy to pick up and once we've made something with that, we're moving to something more complex. [until we can code the call of dooties in binary. ]
  20. Are we just doing Python?
  21. Nice work guys, I'm yet to even start as I've been busy the past few evenings!
  22. Awesome stuff. I decided I did not want to be crammed up in the spare room like a crazy programmer so installed ubuntu on the wifes netbook so I could code in the front room. SOI hadf a play last night and I've done the usual 'Hello world' bits and also made a "Guess the number within 6 goes" game! Nothing cutting edge, but the fact it worked, and the fact I understood what the code was doing was good enough for me! I'll continue to crack on tonight!
  23. Actually I think the code shell above is only a few lines away from pong... Turn the platform on it's side Make two of them Get some user input to alter the vertical position of the platforms/bats each screen refresh Create a rectangle that moves in one direction unless it hits a bat Collision detection Scoring
  24. I thought @Rawns in another thread suggested a side scroller? I thought why not, I haven't tried one of them yet I'm pretty sure the main part of Civ IV is C++ with Python as an interface/scripting language for the AI? I think the same can be said for EVE - the GUI and graphics are almost likely C++ with Python used to talk back to the servers, etc. Back to the code. My starting point - trying to all procedual and creating functions for each main segment seems to be back firing some what when it come to variable handling. I'm finding myself having to make almost everything a global variable to pass data between code segments. I think I'm going to have to look at a more object-oriented approach, then I probably won't be fighting the language so much.
  25. For the assets side, ccmixter.org for Creative Commons licenced music. For Visual Studio users, PTVS 2.0 Alpha Plugin Python Tools for Visual Studio extension
  26. Also, "wrong language to be using"? EVE online is coded in python. And Civ IV.
  27.  
×
×
  • Create New...