game.py

Created by eloidrai1

Created on April 12, 2022

3.88 KB

Jeu avec des boules de feu.


from kandinsky import *
from ion import *

import random
import time

WIDTH = 320
HEIGHT = 222

FLOOR_HEIGHT = 5
CANON_BASE_HEIGHT = 7  
CANON_BASE_WIDTH = 40
CANON_HEIGHT = 30 
CANON_WIDTH = 10

RADIUS_BALL = 3
SPEED_BALL = 4
SPEED_CANON = 8
SPEED_METEORITE_X = 3
SPEED_METEORITE_Y = 7
FRAME_DURATION = 0.05

RADIUS_METEORITE = 15

GREEN = (0, 160, 0)
BLUE = (10, 180, 255)
BROWN = (150, 115, 85)
GRAY = (100, 100, 100)
RED = (255, 0, 0)

class Canonball(object):
  def __init__(self, x):
    self.x = x
    self.y = HEIGHT - (CANON_BASE_HEIGHT + CANON_HEIGHT + FLOOR_HEIGHT) - 5
    
  def erase(self):
    fill_rect(self.x-RADIUS_BALL, self.y-RADIUS_BALL, 2*RADIUS_BALL+1, 2*RADIUS_BALL+1, BLUE)

  def rise(self):
    self.y -= SPEED_BALL
  
  def draw(self):
    for y in range(self.y-RADIUS_BALL, self.y+RADIUS_BALL+1):
      for x in range(self.x-RADIUS_BALL, self.x+RADIUS_BALL+1):
        if (self.x-x)**2 + (self.y-y)**2 <= 9:
          set_pixel(x, y, (0, 0, 0))
        
class Canon(object):
  def __init__(self):
    self.x = WIDTH // 2
    self.time_unarmed = 0

  def draw(self):
    fill_rect(self.x-CANON_BASE_WIDTH//2, HEIGHT-(FLOOR_HEIGHT+CANON_BASE_HEIGHT), CANON_BASE_WIDTH, CANON_BASE_HEIGHT, BROWN)
    fill_rect(self.x-CANON_WIDTH//2, HEIGHT-(FLOOR_HEIGHT+CANON_BASE_HEIGHT+CANON_HEIGHT), CANON_WIDTH, CANON_HEIGHT, GRAY)
  
  def erase(self):
    fill_rect(self.x-CANON_BASE_WIDTH//2, HEIGHT-(FLOOR_HEIGHT+CANON_BASE_HEIGHT+CANON_HEIGHT), CANON_BASE_WIDTH, CANON_BASE_HEIGHT+CANON_HEIGHT, BLUE)

class Meteorite(object):
  def __init__(self):
    self.x = random.randint(100, WIDTH)
    self.y = 0
    self.time_before_repaint = 0
    
  def draw(self):
    c = 0
    for x in range(self.x-RADIUS_METEORITE, self.x+RADIUS_METEORITE):
      for y in range(self.y-RADIUS_METEORITE, self.y+RADIUS_METEORITE):
        if (x-self.x)**2 + (y-self.y)**2 < (RADIUS_METEORITE+(x*y)%5-5)**2:
          set_pixel(x, y, RED)
        c += 1
  
  def fall(self):
    self.y += SPEED_METEORITE_Y
    self.x -= SPEED_METEORITE_X

  def erase(self):
    fill_rect(self.x-15, self.y-15, 30, 30, BLUE)

def main():
  fill_rect(0, HEIGHT-FLOOR_HEIGHT, WIDTH, FLOOR_HEIGHT, GREEN)
  fill_rect(0, 0, WIDTH, HEIGHT-FLOOR_HEIGHT, BLUE)

  canon = Canon()
  canon.draw()
  balls = []
  points = 0
  
  meteorites = []
  time_before_new_meteorite = 0
  
  while True:
    # Canon
    if keydown(KEY_LEFT):
      canon.erase()
      canon.x -= SPEED_CANON
      canon.draw()
    if keydown(KEY_RIGHT):
      canon.erase()
      canon.x += SPEED_CANON
      canon.draw()
    
    # Balls  
    if canon.time_unarmed == 0 and keydown(KEY_UP):
      balls.append(Canonball(canon.x))
      canon.time_unarmed = 10
    elif canon.time_unarmed > 0:
      canon.time_unarmed -= 1
    
    # Meteorites  
    for i, meteorite in enumerate(meteorites):
      if meteorite.time_before_repaint == 0:
        meteorite.erase()
        meteorite.fall()
        meteorite.draw()
        meteorite.time_before_repaint = 1
      else:
        meteorite.time_before_repaint -= 1
      # End of the game
      if meteorite.y > HEIGHT-FLOOR_HEIGHT-RADIUS_METEORITE:
        draw_string("GAME OVER",110,100)
        return
    if time_before_new_meteorite == 0:
      meteorites.append(Meteorite())
      time_before_new_meteorite = 30
    else:
      time_before_new_meteorite -= 1
    
    # Balls' motion and meteorite destruction
    for i, ball in enumerate(balls):
      ball.erase()
      ball.rise()
      ball.draw()
      if ball.y < 0:
        balls.pop(i).erase()
      for im, meteorite in enumerate(meteorites):
        if meteorite.x-RADIUS_METEORITE <= ball.x <= meteorite.x+RADIUS_METEORITE and meteorite.y-13 <= ball.y <= meteorite.y+13:
          meteorites.pop(im).erase()
          balls.pop(i).erase()
          points += 1
          draw_string(str(points) + (" pt." if points==1 else " pts."), 5, 5)
          break
    
    time.sleep(FRAME_DURATION)

main()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.