69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
pong_object.py
|
|
|
|
Pandemic Pong Objects logic (i.e. ball ;)
|
|
|
|
"""
|
|
|
|
from random import random # For random start data ...
|
|
from playsound import playsound # Play some sounds
|
|
import pong_constants as pgc # Pong global constants
|
|
import pong_globalvars as pgv # Pong global variables
|
|
|
|
class PongObject:
|
|
def __init__(self, x, y, w, h, delta_x, delta_y):
|
|
"""Initialize non-player object (i.e. 'ball' - but maybe more objects later!)"""
|
|
|
|
self.x = x # Current position
|
|
self.y = y
|
|
self.w = w # Size of object
|
|
self.h = h
|
|
self.delta_x = delta_x # Current movement
|
|
self.delta_y = (random() - 0.5) * 10 # TODO: Move logic to caller ...
|
|
self.color = pgc.COL_WHITE
|
|
self.delay = 0
|
|
|
|
def reinit(self, delta_x, delta_y):
|
|
"""Re-Initialize object for next game"""
|
|
|
|
self.x = pgv.GAMEAREA_MAX_X/2 - self.w/2
|
|
self.y = pgv.GAMEAREA_MAX_Y/2 - self.h/2
|
|
self.delta_x = delta_x
|
|
self.delta_y = delta_y
|
|
self.color = pgc.COL_WHITE
|
|
self.delay = 0
|
|
|
|
def eval_object(self):
|
|
"""Object movement w/ sound support"""
|
|
|
|
bChanged = 0
|
|
sum_x = self.x + self.delta_x
|
|
sum_y = self.y + self.delta_y
|
|
|
|
if sum_x < pgv.GAMEAREA_MIN_X:
|
|
#sum_x = GAMEAREA_MIN_X
|
|
#self.delta_x = -self.delta_x
|
|
bChanged = -1 # Miss left
|
|
elif sum_x + self.w > pgv.GAMEAREA_MAX_X:
|
|
#sum_x = GAMEAREA_MAX_X - self.w
|
|
#self.delta_x = -self.delta_x
|
|
bChanged = 1 # Miss right
|
|
|
|
if sum_y < pgv.GAMEAREA_MIN_Y:
|
|
sum_y = pgv.GAMEAREA_MIN_Y
|
|
self.delta_y = -self.delta_y
|
|
playsound(pgc.wall_contact)
|
|
elif sum_y + self.h > pgv.GAMEAREA_MAX_Y:
|
|
sum_y = pgv.GAMEAREA_MAX_Y - self.h
|
|
self.delta_y = -self.delta_y
|
|
playsound(pgc.wall_contact)
|
|
|
|
self.x = sum_x
|
|
self.y = sum_y
|
|
return bChanged
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("***** pong_object has no function, call 'pandemic_pong.py'!") |