player_class_2.py
Created by
wperez274
Created on
May 02, 2025
4.94 KB
from ion import *
from kandinsky import *
from time import sleep
from math import sin , cos , radians
SW , SH = 320 , 222
black = ( 0 , 0 , 0 )
bg = ( 240 , 240 , 240 )
red = ( 255 , 0 , 0 )
brown = ( 120 , 80 , 40 )
level = [
[ 0 , 200 , SW , 22 , black ],
[ 100 , 160 , 60 , 10 , black ],
[ 200 , 130 , 40 , 10 , black ]
]
def F ( x , y , w , h , c ):
fill_rect ( int ( x ), int ( y ), int ( w ), int ( h ), c )
class Player :
def __init__ ( self ):
self . x = 50
self . y = 100
self . w = 12
self . h = 12
self . vx = 0
self . vy = 0
self . energy = 30
self . on_ground = False
self . rocks = []
self . facing = " right "
self . hit_timer = 0
def update ( self ):
if keydown ( KEY_LEFT ):
self . vx = - 2
self . facing = " left "
elif keydown ( KEY_RIGHT ):
self . vx = 2
self . facing = " right "
else :
self . vx = 0
if keydown ( KEY_UP ) and self . on_ground :
self . vy = - 5
self . vy += 0.2
self . x += self . vx
self . y += self . vy
self . on_ground = False
for rect in level :
if self . collide ( rect ):
if self . vy > 0 :
self . y = rect [ 1 ] - self . h
self . vy = 0
self . on_ground = True
elif self . vy < 0 :
self . y = rect [ 1 ] + rect [ 3 ]
self . vy = 0
self . shoot ()
for rock in self . rocks :
rock . update ()
self . rocks = [ r for r in self . rocks if not r . dead ]
if self . hit_timer > 0 :
self . hit_timer -= 1
def collide ( self , r ):
return ( self . x < r [ 0 ] + r [ 2 ] and self . x + self . w > r [ 0 ] and
self . y < r [ 1 ] + r [ 3 ] and self . y + self . h > r [ 1 ])
def shoot ( self ):
if keydown ( KEY_OK ):
direction = 60
if keydown ( KEY_UP ):
direction = 90
elif keydown ( KEY_RIGHT ):
direction = 45
elif keydown ( KEY_LEFT ):
direction = 135
elif self . facing == " left " :
direction = 135
elif self . facing == " right " :
direction = 45
speed = 4
vx = speed * cos ( radians ( direction ))
vy = - speed * sin ( radians ( direction ))
self . rocks . append ( Rock ( self . x + self . w // 2 , self . y + self . h // 2 , vx , vy ))
def draw ( self ):
color = ( 255 , 0 , 0 ) if self . hit_timer > 0 else ( 255 , 50 , 50 )
F ( self . x , self . y , self . w , self . h , color )
for rock in self . rocks :
rock . draw ()
class Rock :
def __init__ ( self , x , y , vx , vy ):
self . x = x
self . y = y
self . vx = vx
self . vy = vy
self . r = 4
self . dead = False
def update ( self ):
self . vy += 0.2
self . x += self . vx
self . y += self . vy
if self . y > SH :
self . dead = True
def draw ( self ):
F ( self . x , self . y , self . r , self . r , brown )
class Enemy :
def __init__ ( self , x , y , energy , color ):
self . x = x
self . y = y
self . w = 12
self . h = 12
self . vx = 1
self . energy = energy
self . hit_timer = 0
self . dead = False
self . color = color
def update ( self ):
if self . dead :
return
self . x += self . vx
if self . x < 100 or self . x > 160 - self . w :
self . vx *= - 1
if self . hit_timer > 0 :
self . hit_timer -= 1
def draw ( self ):
if self . dead :
return
color = ( 255 , 0 , 0 ) if self . hit_timer > 0 else self . color
F ( self . x , self . y , self . w , self . h , color )
def hit ( self , damage ):
self . energy -= damage
self . hit_timer = 5
if self . energy <= 0 :
self . dead = True
def collide_with_player ( self , player ):
return ( self . x < player . x + player . w and self . x + self . w > player . x and
self . y < player . y + player . h and self . y + self . h > player . y )
def draw_level ():
for rect in level :
F ( * rect )
def game_over_screen ():
fill_rect ( 0 , 0 , SW , SH , black )
draw_string ( " GAME OVER " , 100 , 100 , ( 255 , 255 , 255 ), black )
sleep ( 3 )
def main ():
p = Player ()
enemies = [
Enemy ( 100 , 188 , 10 , ( 0 , 0 , 255 )),
Enemy ( 200 , 120 , 20 , ( 0 , 100 , 255 ))
]
while True :
F ( 0 , 0 , SW , SH , bg )
draw_level ()
p . update ()
p . draw ()
for e in enemies :
e . update ()
e . draw ()
if not e . dead :
for r in p . rocks :
if ( r . x < e . x + e . w and r . x + r . r > e . x and
r . y < e . y + e . h and r . y + r . r > e . y ):
r . dead = True
e . hit ( 5 )
if e . collide_with_player ( p ):
p . energy -= 1
p . hit_timer = 5
if p . energy <= 0 :
game_over_screen ()
return
sleep ( 0.02 )
main ()