from cmu_graphics import * import ctypes ctypes.windll.user32.ShowCursor(False) screen_width = ctypes.windll.user32.GetSystemMetrics(0) screen_height = ctypes.windll.user32.GetSystemMetrics(1) app.width = screen_width app.height = screen_height # Background bg = Rect(0,0,screen_width,screen_height) # Animation variables ar = 0 arcs = Group() animation_done = False # Draw a 5x5 grid of arcs for x in range(40, screen_width, 80): for y in range(40, screen_height, 80): arcs.add(Arc(x, y, 40, 40, 0, 1, fill=None, border='lime')) # Game variables (will be initialized later) player = None enemy = None dy = 0 gravity = 1 jump_strength = -15 player_on_ground = False enemy_speed = 3 enemy_dy = 0 enemy_chasing = True ground = None respawn_label = None # Label to show when player dies def setup_game(): global player, enemy, ground, dy, player_on_ground, enemy_chasing, enemy_dy, respawn_label # Ground ground = Rect(0, screen_height/1.4, screen_width, screen_height/3, fill=None, border='lime') # Player player = Rect(screen_width/2, 0, 20, 20, fill='lime') # Enemy enemy = Rect(screen_width/2 + 100, ground.top - 20, 20, 20, fill='red') dy = 0 enemy_dy = 0 player_on_ground = False enemy_chasing = True # Respawn label (hidden at start) respawn_label = Label('Press R to respawn', screen_width/2, screen_height/2, visible=False, fill='red', size=30) def onKeyHold(keys): global dy if animation_done and player.visible: # Horizontal movement if 'a' in keys: player.centerX -= 7 if 'd' in keys: player.centerX += 7 # Jump while holding space or W if ('space' in keys or 'w' in keys) and player.bottom >= ground.top: dy = jump_strength def onKeyPress(key): global dy, player_on_ground, enemy_chasing if animation_done and key == 'r' and not player.visible: # Respawn player at top player.centerX = screen_width/2 player.centerY = 0 player.visible = True dy = 0 player_on_ground = False enemy_chasing = True respawn_label.visible = False # Hide respawn message def onStep(): global ar, animation_done, dy, enemy_dy, player_on_ground, enemy_chasing if not animation_done: for arc in arcs: arc.sweepAngle += 10 if arc.sweepAngle >= 350: arc.sweepAngle = 1 ar += 1 if ar >= 600: # End animation arcs.visible = False animation_done = True setup_game() else: if player.visible: # Player gravity dy += gravity player.centerY += dy if player.bottom > ground.top: player.bottom = ground.top dy = 0 player_on_ground = True # Enemy gravity enemy_dy += gravity enemy.centerY += enemy_dy if enemy.bottom > ground.top: enemy.bottom = ground.top enemy_dy = 0 # Enemy chases player only if player hit the ground and enemy hasn't caught player if player.visible and player_on_ground and enemy_chasing: if enemy.centerX < player.centerX: enemy.centerX += enemy_speed elif enemy.centerX > player.centerX: enemy.centerX -= enemy_speed # Collision: player disappears, enemy stops, show respawn message if player.visible and player.hitsShape(enemy): player.visible = False enemy_chasing = False respawn_label.visible = True cmu_graphics.run()
from cmu_graphics import * import ctypes ctypes.windll.user32.ShowCursor(False) screen_width = ctypes.windll.user32.GetSystemMetrics(0) screen_height = ctypes.windll.user32.GetSystemMetrics(1) app.width = screen_width app.height = screen_height # Background bg = Rect(0,0,screen_width,screen_height) # Animation variables ar = 0 arcs = Group() animation_done = False # Draw a 5x5 grid of arcs for x in range(40, screen_width, 80): for y in range(40, screen_height, 80): arcs.add(Arc(x, y, 40, 40, 0, 1, fill=None, border='lime')) # Game variables (will be initialized later) player = None enemy = None dy = 0 gravity = 1 jump_strength = -15 player_on_ground = False enemy_speed = 3 enemy_dy = 0 enemy_chasing = True ground = None respawn_label = None # Label to show when player dies def setup_game(): global player, enemy, ground, dy, player_on_ground, enemy_chasing, enemy_dy, respawn_label # Ground ground = Rect(0, screen_height/1.4, screen_width, screen_height/3, fill=None, border='lime') # Player player = Rect(screen_width/2, 0, 20, 20, fill='lime') # Enemy enemy = Rect(screen_width/2 + 100, ground.top - 20, 20, 20, fill='red') dy = 0 enemy_dy = 0 player_on_ground = False enemy_chasing = True # Respawn label (hidden at start) respawn_label = Label('Press R to respawn', screen_width/2, screen_height/2, visible=False, fill='red', size=30) def onKeyHold(keys): global dy if animation_done and player.visible: # Horizontal movement if 'a' in keys: player.centerX -= 7 if 'd' in keys: player.centerX += 7 # Jump while holding space or W if ('space' in keys or 'w' in keys) and player.bottom >= ground.top: dy = jump_strength def onKeyPress(key): global dy, player_on_ground, enemy_chasing if animation_done and key == 'r' and not player.visible: # Respawn player at top player.centerX = screen_width/2 player.centerY = 0 player.visible = True dy = 0 player_on_ground = False enemy_chasing = True respawn_label.visible = False # Hide respawn message def onStep(): global ar, animation_done, dy, enemy_dy, player_on_ground, enemy_chasing if not animation_done: for arc in arcs: arc.sweepAngle += 10 if arc.sweepAngle >= 350: arc.sweepAngle = 1 ar += 1 if ar >= 600: # End animation arcs.visible = False animation_done = True setup_game() else: if player.visible: # Player gravity dy += gravity player.centerY += dy if player.bottom > ground.top: player.bottom = ground.top dy = 0 player_on_ground = True # Enemy gravity enemy_dy += gravity enemy.centerY += enemy_dy if enemy.bottom > ground.top: enemy.bottom = ground.top enemy_dy = 0 # Enemy chases player only if player hit the ground and enemy hasn't caught player if player.visible and player_on_ground and enemy_chasing: if enemy.centerX < player.centerX: enemy.centerX += enemy_speed elif enemy.centerX > player.centerX: enemy.centerX -= enemy_speed # Collision: player disappears, enemy stops, show respawn message if player.visible and player.hitsShape(enemy): player.visible = False enemy_chasing = False respawn_label.visible = True cmu_graphics.run()