RoomObject Module
RoomObject
Base class for all objects that can exist in a game level (Room).
Provides core functionality for position, movement, image handling, collision detection, event handling, and interaction with the game environment.
Attributes:
Name | Type | Description |
---|---|---|
room |
Level
|
The level or room this object belongs to. |
depth |
int
|
Drawing order depth. |
x |
int
|
X-coordinate of the object. |
y |
int
|
Y-coordinate of the object. |
rect |
Rect
|
The rectangle representing the object's position and size. |
prev_x |
int
|
Previous X-coordinate (for movement/collision). |
prev_y |
int
|
Previous Y-coordinate (for movement/collision). |
width |
int
|
Width of the object. |
height |
int
|
Height of the object. |
image |
The current image surface of the object. |
|
image_orig |
The original image surface (for rotation). |
|
curr_rotation |
int
|
Current rotation angle. |
x_speed |
float
|
Current speed in the X direction. |
y_speed |
float
|
Current speed in the Y direction. |
gravity |
float
|
Gravity applied to the object. |
handle_key_events |
bool
|
Whether the object handles key events. |
handle_mouse_events |
bool
|
Whether the object handles mouse events. |
angle |
int
|
Current angle for movement or rotation. |
collision_object_types |
set
|
Set of object type names to check collisions against. |
collision_objects |
list
|
List of objects to check for collisions. |
Source code in GameFrame/RoomObject.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
__init__(room, x, y)
Initializes a RoomObject with position and default properties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
room
|
Level
|
The level or room this object belongs to. |
required |
x
|
int
|
Initial X-coordinate. |
required |
y
|
int
|
Initial Y-coordinate. |
required |
Source code in GameFrame/RoomObject.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
blocked()
Stops movement and resets position if the object is blocked.
Source code in GameFrame/RoomObject.py
256 257 258 259 260 261 262 263 264 |
|
bounce(other)
Reverses speed and position if this object bounces off another object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
RoomObject
|
The object to bounce off. |
required |
Source code in GameFrame/RoomObject.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
check_collisions()
Checks for collisions with registered collision objects and handles them.
Source code in GameFrame/RoomObject.py
151 152 153 154 155 156 157 158 |
|
clicked(button_number)
Handles mouse click events. Override in subclasses for custom click handling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
button_number
|
int
|
The mouse button number clicked. |
required |
Source code in GameFrame/RoomObject.py
214 215 216 217 218 219 220 221 222 |
|
collides_at(obj, x, y, collision_type)
Checks if moving an object to (x, y) would cause a collision with a given type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
RoomObject
|
The object to check. |
required |
x
|
int
|
X offset. |
required |
y
|
int
|
Y offset. |
required |
collision_type
|
str
|
The class name of the object type to check against. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if a collision would occur, False otherwise. |
Source code in GameFrame/RoomObject.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
delete_object(obj)
Removes an object from the level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
RoomObject
|
The object to remove. |
required |
Source code in GameFrame/RoomObject.py
117 118 119 120 121 122 123 124 |
|
get_direction_coordinates(angle, speed)
Returns the X and Y coordinates for a given angle and speed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle
|
int
|
The angle in degrees. |
required |
speed
|
int
|
The speed value. |
required |
Returns:
Type | Description |
---|---|
Tuple[int, int]
|
Tuple[int, int]: The X and Y coordinates. |
Source code in GameFrame/RoomObject.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
handle_collision(other, other_type)
Handles a collision with another object. Override in subclasses for custom collision behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
RoomObject
|
The other object collided with. |
required |
other_type
|
str
|
The class name of the other object. |
required |
Source code in GameFrame/RoomObject.py
182 183 184 185 186 187 188 189 190 191 |
|
joy_pad_signal(p1_buttons, p2_buttons)
Handles joystick/gamepad input. Override in subclasses for custom joystick handling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p1_buttons
|
List[int]
|
Player 1 button/axis states. |
required |
p2_buttons
|
List[int]
|
Player 2 button/axis states. |
required |
Source code in GameFrame/RoomObject.py
203 204 205 206 207 208 209 210 211 212 |
|
key_pressed(key)
Handles key press events. Override in subclasses for custom key handling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
The pygame key state array. |
required |
Source code in GameFrame/RoomObject.py
193 194 195 196 197 198 199 200 201 |
|
load_image(file_name)
staticmethod
Returns the full path to an image file in the Images directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
Filename of the image. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Full path to the image file. |
Source code in GameFrame/RoomObject.py
69 70 71 72 73 74 75 76 77 78 79 80 |
|
mouse_event(mouse_x, mouse_y, button_left, button_middle, button_right)
Handles mouse movement and button events. Override in subclasses for custom mouse handling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mouse_x
|
int
|
Mouse X position. |
required |
mouse_y
|
int
|
Mouse Y position. |
required |
button_left
|
bool
|
Left mouse button state. |
required |
button_middle
|
bool
|
Middle mouse button state. |
required |
button_right
|
bool
|
Right mouse button state. |
required |
Source code in GameFrame/RoomObject.py
224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
prestep()
Optional: Called before the main step/update logic. Override in subclasses for custom behavior.
Source code in GameFrame/RoomObject.py
137 138 139 140 141 142 |
|
register_collision_object(collision_object)
Registers a type of object for collision detection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collision_object
|
str
|
The class name of the object type to check collisions with. |
required |
Source code in GameFrame/RoomObject.py
98 99 100 101 102 103 104 105 |
|
remove_object(obj)
Removes an object from this object's collision list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
RoomObject
|
The object to remove from collision_objects. |
required |
Source code in GameFrame/RoomObject.py
126 127 128 129 130 131 132 133 134 135 |
|
rotate(angle)
Rotates the object's image by the given angle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle
|
int
|
The angle to rotate by (in degrees). |
required |
Source code in GameFrame/RoomObject.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
|
rotate_to_coordinate(mouse_x, mouse_y)
Rotates the object to face a coordinate (e.g., mouse position).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mouse_x
|
int
|
The X coordinate to face. |
required |
mouse_y
|
int
|
The Y coordinate to face. |
required |
Source code in GameFrame/RoomObject.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
set_direction(angle, speed)
Sets the object's movement direction and speed based on an angle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
angle
|
int
|
The angle in degrees. |
required |
speed
|
int
|
The speed value. |
required |
Source code in GameFrame/RoomObject.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
set_image(image, width, height)
Loads and sets the object's image, scaling to the given width and height.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
str
|
Path to the image file. |
required |
width
|
int
|
Width to scale the image to. |
required |
height
|
int
|
Height to scale the image to. |
required |
Source code in GameFrame/RoomObject.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
set_timer(ticks, function_call)
Sets a timed event to call a function after a number of ticks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ticks
|
int
|
Number of frames to wait before calling the function. |
required |
function_call
|
Callable
|
The function to call. |
required |
Source code in GameFrame/RoomObject.py
266 267 268 269 270 271 272 273 274 |
|
step()
Optional: Called for the main step/update logic. Override in subclasses for custom behavior.
Source code in GameFrame/RoomObject.py
144 145 146 147 148 149 |
|
update()
Updates the object's position based on speed and gravity.
Source code in GameFrame/RoomObject.py
107 108 109 110 111 112 113 114 115 |
|