Hey guys I'm attempting to make a super mario like platform game and need help with making images move. Like instead of moving the little man, just having the background moving with the corresponding button movement.
Can anyone hint at how to do this?
Or is moving the little man easier than the background?
Um... you provided absolutely no information. So as of right now, I can't answer your question. What are you using as a 2D engine for Python? etc.
Here is an example using the module pygame:
# experiments with module pygame # free from: http://www.pygame.org/ # load, display and move an image import pygame as pg # initialize pygame pg.init() # pick an image you have (.bmp .jpg .png .gif) # if the image file is not in the working folder, # use the full pathname like "C:/Images/gifs/Pooh.gif" image_file = "Pooh.gif" # RGB color tuple used by pygame white = (255, 255, 255) # create a 300x300 white screen screen = pg.display.set_mode((300,300)) screen.fill(white) # load the image from a file # convert() unifies the pixel format for faster blit image = pg.image.load(image_file).convert() # draw image, position the image ulc at x=50, y=20 screen.blit(image, (50, 20)) # get the rectangle the image occupies # rec(x, y, w, h) start_rect = image.get_rect() # set up the timed event loop x = 0 y = 0 clock = pg.time.Clock() keepGoing = True while keepGoing: # set rate of move clock.tick(30) for event in pg.event.get(): # quit when user clicks on window x if event.type == pg.QUIT: keepGoing = False # move the image ... x += 1 y += 1 # stop when x exceeds limit if x > 270: keepGoing = False image_rect = start_rect.move(x, y) # this erases the old sreen with white screen.fill(white) # put the image on the screen at new location screen.blit(image, image_rect) # update screen pg.display.flip()
ok thanks alot sneekula. This has helped. I didnt even know where to start. Hopefully I can work it out from here. I thought I might have to try with Tkinter or something but I dont have that much time
Thanks I'll let you know how it goes... or when I need more help
Hey guys, I have downloaded Pygame but I cant get it to run. I ran both WingIDE and the normal IDE and neither of them can find the module pygame...any ideas?? Thanks alot :)
That's weird.... so
import pygame is causing the "module not found" error?
Other than trying to install the wrong version of pygame for your python installation, I don't know what could be wrong.
What OS are you using? A GNU/Linux distro or Windows?
You might have to add the PyGame directory, or the python directory to the systems PATH variable, just google "Windows PATH variable" and you should be able to find out how.
You would just add the filepath to it, like:
"c:\python25\" to it, if thats how your drive is mapped.
Hey guys, I have tried both installing on Windows and on my laptop which is Linux. The windows one comes up with "module not found" and yes i double checked I had the right version for python. We use 2.6 at school and thats the version I downloaded.
Other than that I have no idea :(
Ok guys I'm back.
I got pygame working on my laptop thankfully.
I've been writing some code to create the screen and get both my little hero dude and the background on the screen. I also found some code to help incorporate user input ...like keys and stuff but I can't seem to get it to work...
import pygame from pygame.locals import * screen = pygame.display.set_mode((1024, 768)) background = pygame.image.load('marioscreendesign.bmp') man = pygame.image.load('superdudepic.bmp') screen.blit(background, (0,0)) screen.blit(man,(0,0)) pygame.display.flip() for i in range(100): screen.fill((0,0,0)) screen.blit(background, (0,0)) screen.blit(man,(i,0)) pygame.display.flip() pygame.time.delay(50) while 1: # USER INPUT clock.tick(30) for event in pygame.event.get(): if not hasattr(event, 'key'): continue down = event.type == KEYDOWN # key down or up? if event.key == K_RIGHT: k_right = down * 5 elif event.key == K_LEFT: k_left = down * 5 elif event.key == K_UP: k_up = down * 2 elif event.key == K_DOWN: k_down = down * 2 elif event.key == K_ESCAPE: sys.exit(0) # quit the game screen.fill(BLACK)
invalid syntax (pygame2.py, line 24)
Traceback (most recent call last):
File "/usr/bin/drpython", line 648, in CheckSyntax
compile(ctext, fn, 'exec')
<type 'exceptions.SyntaxError'>: invalid syntax (pygame2.py, line 24)
this is the error I'm getting and I've tried everything to fix it...but it doesn't want to work. Thanks for the help guys. 'Im new to this and my teacher can't help
Hi again :D
I don't know why, but it wasn't accepting the negative values in the event handling. I just made the "down" values negative and multiplied positive values instead, like this:
import pygame from pygame.locals import * screen = pygame.display.set_mode((1024, 768)) background = pygame.image.load('marioscreendesign.bmp') man = pygame.image.load('superdudepic.bmp') screen.blit(background, (0,0)) screen.blit(man,(0,0)) pygame.display.flip() for i in range(100): screen.fill((0,0,0)) screen.blit(background, (0,0)) screen.blit(man,(i,0)) pygame.display.flip() pygame.time.delay(50) while 1: # USER INPUT clock.tick(30) for event in pygame.event.get(): if not hasattr(event, 'key'): continue down = event.type == KEYDOWN # key down or up? # I just made the "down" value negative and it accepted that. if event.key == K_RIGHT: k_right = -down * 5 elif event.key == K_LEFT: k_left = down * 5 elif event.key == K_UP: k_up = down * 2 elif event.key == K_DOWN: k_down = -down * 2 # end elif event.key == K_ESCAPE: sys.exit(0) # quit the game screen.fill(BLACK)
Change your conditional statements so that they're not 1-liners, which fixes it:
# ... # excerpt from your code, making the IFs indented while 1: # USER INPUT clock.tick(30) for event in pygame.event.get(): if not hasattr(event, 'key'): continue down = event.type == KEYDOWN # key down or up? if event.key == K_RIGHT: k_right = down * -5 elif event.key == K_LEFT: k_left = down * 5 elif event.key == K_UP: k_up = down * 2 elif event.key == K_DOWN: k_down = down * -2 elif event.key == K_ESCAPE: sys.exit(0) # quit the game screen.fill(BLACK)
Thank you :) Shadwickman
Hey guys I'm back
I've got everything going sort of smoothly.
I've got my character moving across the screen, working on collision detection and now trying to work on jumping.
But I have no idea how to start this. I've looked for examples but none work. Any ideas?
For collision detection, tomtetlaw had a similar question, so I'll give you the same answer I gave him. This is for simplistic levels:
Why not try making a black and white image for your level, where white is free space, and black is blocked space (or vice-versa)? Then you can load this image and read the colour values of it using the Python Imaging Library (PIL), and save this data into a list. Or even better, save it into a Numpy array which is faster as far as I know.
Then, when you call the function to move the player, take the coordinates that the player would be at if you allowed the move. Compare these coordinates to the corresponding spot in your black and white map list, and if it has white space, allow the player to move there. If it's black, then end the function without moving the player. You'd also need to take into account the width and/or height of the player, and check that whole area on your collision map, so that you ensure the whole player would be moving into free/available space.
Obviously this isn't good for loading huge levels at a time, but it's ok for simple platformers. If it's for a larger map, you'll want to come up with some sort of optimized loading system for keeping the memory usage down by only loading applicable map segments at a time.
It's also even better if your game can be split into tiles or a grid, of spaces... say 15 x 15 pixels or something. Then your list will maybe be something 30 rows by 30 rows, instead of containing a value for every single pixel. And finally, if you are using a JPG image for the B&W collision map, then you can save it in Greyscale, instead of RGB. That way, each pixel would contain a single value (shade - 0 to 255), instead of a full tuple of RGB integer values. This should also reduce memory usage quite a bit.
There are probably much better ways of doing this, but this seems like a suitable system to me for small games, tile- or grid-based games, or if you can make a really-well optimized version of this idea.
Hope that gave you a sort of abstract idea :P
If you want to get really advanced, you could have it only check for collisions on player pixels that aren't transparent. This would be for cases where your player is, say a circle, but its bounding box is still square.
I was wondering what would be the best way to implement an algorythm to figure out what side a circle has collided with a rectangle?
After check to make sure ...
I am currently practicing my java skills by doing this program called car rental management system. I am creating a login page where when the user login it ...
Hi. I have a .raw file and I want to display it as hex. why? because i want to try and replicate what 010 Editor can do which is read ...