![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
My RPG Field Map Generator
This is for people who might want more script to examine and learn off of. I actually think this is very innovative and iffecient, I surprised myself greatly with this original script:
area_1.py from character_move import * ##import code shown in character_move.py
from game_settings import * ##import video settings and image display functions
def area_1():
character_location = ['',''] ##put where you want the character to start
collision_1 = ['','','','',''] ##put the co-ordinates of the collision points in the format (rectangle or point), (x1), (y1), (x2), (y2) exclude the last two if you make the first one point
collision_2 = ['','','','','']
collision_3 = ['','','']
collision_4 = ['','','']
collision_5 = ['','','']
collisions = [collision_1, collision_2, collision_3, collision_4, collision_5] ##if there are more collisions, add them in
screen = video_settings() ##load the video settings to screen from the game_settings.py
pygame.draw.rect(screen, (255, 255, 255), ( (250, 250), (550, 450) ), 0) ##create a white field in the space of the window
character_location = character_move(screen, "area_map_1.bmp", character_location) ##enter the name of the picture for the areamap under "area_map_1.bmp"
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN:
c_o_l_l_i_s_i_o_n = check_collision(collisions, character_location, 0)
if c_o_l_l_i_s_i_o_n == 0:
if event.key == K_UP:
character_location[1] = character_location[1] + 5 ##replace the values with the increments you want the character to move
elif event.key == K_DOWN:
character_location[1] = character_location[1] - 5
elif event.key == K_LEFT:
character_location[0] = character_location[0] + 5
elif event.key == K_RIGHT:
character_location[0] = character_location[0] - 5
c_o_l_l_i_s_i_o_n = check_collision(collisions, character_location, 0)
if c_o_l_l_i_s_i_o_n == 1:
if event.key == K_UP:
character_location[1] = character_location[1] - 5
elif event.key == K_DOWN:
character_location[1] = character_location[1] + 5
elif event.key == K_LEFT:
character_location[0] = character_location[0] - 5
elif event.key == K_RIGHT:
character_location[0] = character_location[0] + 5
character_location = character_move(screen, "area_map_1.bmp", character_location)
#print character_location
#area_1()character_movement.py from game_settings import *
def check_collision(collisions, character_location, c_o_l_l_i_s_i_o_n):
for collision in range( 0, len(collisions) ):
if collisions[collision][0] == "rectangle": ##check for collision points
if character_location[0] > (collisions[collision][1]):
if character_location[0] < (collisions[collision][3]):
if character_location[1] > (collisions[collision][2]):
if character_location[1] < (collisions[collision][4]):
c_o_l_l_i_s_i_o_n = 1
return c_o_l_l_i_s_i_o_n
elif collisions[collision][0] == "point":
if character_location[0] > (collisions[collision][1]-5):
if character_location[0] < (collisions[collision][1]+5):
if character_location[1] > (collisions[collision][2]-5):
if character_location[1] < (collisions[collision][2]+5):
c_o_l_l_i_s_i_o_n = 1 ##1 means a collision happened
return c_o_l_l_i_s_i_o_n
else:
if collision == len(collisions)-1:
c_o_l_l_i_s_i_o_n = 0
return c_o_l_l_i_s_i_o_n ##0 means no collision
def character_move(screen, area_map, character_location):
map_draw = pygame.image.load(area_map)
screen.blit(map_draw, character_location)
pygame.draw.rect(screen, 0, ( (0, 0), (250, 700) ), 0) #create a black screen around the window (not through to prevent flicker)
pygame.draw.rect(screen, 0, ( (250, 0), (800, 250) ), 0)
pygame.draw.rect(screen, 0, ( (550, 250), (800, 700) ), 0)
pygame.draw.rect(screen, 0, ( (250, 450), (550, 700) ), 0)
pygame.display.flip()
clock = pygame.time.Clock()
clock.tick(60)
return character_location ##return the location of the characterThen you just add a character sprite/animation to the very center of the screen, and any ending conditions/battle conditions. And you're set! Man, I feel like a professional. :p |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Laziness is a virtue in programming.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
I'm not to sharp on Python;however, the code looks good... but naming a variable "c_o_l_l_i_s_i_o_n"? Come on man.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
That's some pretty fine programming - but don't take it the wrong way if I offer some constructive criticism.
In check_collision(), you have a for-loop like this: for collision in range( 0, len(collisions) ):
if collisions[collision][0] == "rectangle":
...for collision in collisions:
if collision[0] == "rectangle":
...for collision in collisions:
if len(collision) == 4:
# rectangle stuff
elif len(collision) == 2:
# point stufffor collision in range( 0, len(collisions) ):
if collisions[collision][0] == "rectangle":
...
elif collisions[collision][0] == "point":
...
else:
if collision == len(collisions)-1:
c_o_l_l_i_s_i_o_n = 0
return c_o_l_l_i_s_i_o_n ##0 means no collisionfor collision in range( 0, len(collisions) ):
if collisions[collision][0] == "rectangle":
...
elif collisions[collision][0] == "point":
...
c_o_l_l_i_s_i_o_n = 0
return c_o_l_l_i_s_i_o_n ##0 means no collisionAnother redundancy is your use of the c_o_l_l_i_s_i_o_n variable. You don't need it. Instead of: c_o_l_l_i_s_i_o_n = 0 return c_o_l_l_i_s_i_o_n return 0 return False if collisions[collision][0] == "rectangle":
if character_location[0] > (collisions[collision][1]) and \
character_location[0] < (collisions[collision][3]) and \
character_location[1] > (collisions[collision][2]) and \
character_location[1] < (collisions[collision][4]):
c_o_l_l_i_s_i_o_n = 1
return c_o_l_l_i_s_i_o_ndef check_collision(collisions, character_location):
for collision in collisions:
if len(collision) == 4 and \
if character_location[0] > collision[0] and \
character_location[0] < collision[2] and \
character_location[1] > collision[1] and \
character_location[1] < collision[3]:
return True
elif len(collision) == 2 and \
if character_location[0] > (collision[0] - 5) and \
character_location[0] < (collision[0] + 5) and \
character_location[1] > (collision[1] - 5) and \
character_location[1] < (collision[1] + 5):
return True
return FalseBut we can get it even smaller still, by using pygame's Rect class. The code below defines collisions in a slightly different fashion. (x, y) is a point, whilst [x, y, width, height] is a rectangle: from pygame import Rect
def check_collision(collisions, (x, y)):
character_rect = Rect(x - 5, y - 5, 10, 10)
for collision in collisions:
if len(collision) == 4:
if character_rect.colliderect(Rect(*collision)):
return True
if len(collision) == 2:
if character_rect.collidepoint(*collision):
return True
return False![]() Indeed, if you wanted to take a more object-orientated approach, you could wrap your character in a Sprite class - but if I go on this way, we'll be here forever, so I'll cut my post short. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
lol, I don't want to seem all unappreciative. But my programming in Python has changed quite. This is no longer reflective of me.
This was the VERY FIRST program I made in Python, so of course it wasn't very good. I hadn't even read any Tutorials (and still haven't). I now see all the things I could have improved on, and know many better ways I could have done my code. But I've moved on to bigger things since. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Then I must say that that's very good for a first attempt! :eek:
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|