Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2005, 8:25 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,890
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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 character

Then 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
Sane is offline   Reply With Quote
Old Oct 28th, 2005, 1:15 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Laziness is a virtue in programming.
Arevos is offline   Reply With Quote
Old Oct 28th, 2005, 1:20 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Oct 28th, 2005, 2:37 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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":
      ...
The 'range' function appears to be redundant. A for-loop iterates over each item in a list or generator. A more "pythonic" approach could be:
for collision in collisions:
   if collision[0] == "rectangle":
      ...
Another point of redundancy are the "rectangle" and "point" strings. Why not filter on length? You know that a point has two values (x and y), and that a rectangle has four (x1, y1, x2 and y2):
for collision in collisions:
   if len(collision) == 4:
      # rectangle stuff
   elif len(collision) == 2:
      # point stuff
There's also a minor bug in your program:
for 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 collision
The function will only return 0 if and only if the last element of the sequence is not a rectangle or a point. What you probably want to do is place the c_o_l_l_i_s_i_o_n after the for-loop:
for 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 collision
This ensures that the function will return 0 if there are no collisions.

Another 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
Instead, try:
return 0
Or, better yet, instead of using 0 and 1, use False and True:
return False
Also, if statements; you don't need to embed them. You can use logical and:
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_n
So, after we done and removed these bits of redundancy in check_collision, we get this:
def 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 False
In the above function, collisions are either points of two numbers, ie. [x, y], or rectangles of three numbers, ie. [x1, y1, x2, y2].

But 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
Rect objects can be created like so: Rect(x, y, width, height). Once created, they have several handy collision functions. Why slave away when you can let pygame do the hard work for you?

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.
Arevos is offline   Reply With Quote
Old Oct 28th, 2005, 4:38 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,890
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Oct 28th, 2005, 5:20 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Then I must say that that's very good for a first attempt! :eek:
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC