Niveau Intermédiaire [Rendre Les Objets Intelligents Grâce à Python]

le jeu de bataille navale - Niveau intermédiaire

Dans cette simulation de jeu de bataille navale, nous utiliserons deux cartes. La première sera l'ordinateur et contiendra deux bateaux placés aléatoirement et la seconde sera pour le joueur qui essayera de couler les bateaux. La partie se termine quand les bateaux sont coulés.

La liaison radio est mise à contribution pour la communication du début de la partie, de la fin de partie, les tirs et les résultats des tirs.

Ce projet commence à être ambitieux pour une réalisation dans le cadre de SNT.

Méthode : Version ordinateur

  • Le bouton A permet de décider un autre placement

  • Le bouton B permet de démarrer la partie

Des bruitages sont joués lorsqu'un haut-parleur est connecté sur pin0.

1# Olivier Lecluse2# 8 mars 20193# version ordi45from random import randint6from microbit import *7import radio,music89radio.config(group=1)10radio.on()1112boats = []13gameOn = False1415def placer():16 global boats17 boats=[]18 h=randint(0,1)19 if h==0: # bateau de 3 horizontal20 x3=randint(0,2)21 y3=randint(0,4)22 boats.append((x3,y3))23 boats.append((x3+1,y3))24 boats.append((x3+2,y3))25 else: # bateau de 3 vertical26 x3=randint(0,4)27 y3=randint(0,2)28 boats.append((x3,y3))29 boats.append((x3,y3+1))30 boats.append((x3,y3+2))3132 cont = True33 while cont :34 if h==0: # bateau de 2 verrtical35 x2=randint(0,4)36 y2=randint(0,3)37 if (x2,y2) not in boats and (x2, y2+1) not in boats:38 boats.append((x2,y2))39 boats.append((x2,y2+1))40 cont=False41 else: # bateau de 2 horizontal42 x2=randint(0,3)43 y2=randint(0,4)44 if (x2,y2) not in boats and (x2+1, y2) not in boats:45 boats.append((x2,y2))46 boats.append((x2+1,y2))47 cont=False4849def afficher():50 display.clear()51 for b in boats:52 display.set_pixel(b[0],b[1],9)5354placer()55afficher()56while True:57 if button_a.was_pressed() and not gameOn:58 placer()59 afficher()60 if button_b.was_pressed() and not gameOn:61 gameOn = True62 radio.send("GO")63 music.play(music.POWER_UP)64 if gameOn:65 # la partie est en cours6667 # Gestion de l'arrivee d'un coup68 incoming = radio.receive()69 if incoming:70 try:71 hx,hy = incoming.split(',')72 hit=(int(hx),int(hy))73 except:74 continue75 if (hit) in boats:76 boats.remove(hit)77 radio.send("HIT")78 music.pitch(1000, 500)79 color=080 else:81 radio.send("MISS")82 music.pitch(200, 500)83 color=284 display.set_pixel(hit[0], hit[1], color)8586 # Fin de la partie87 if len(boats)==0:88 radio.send("GAME OVER")89 music.play(music.FUNERAL)90 gameOn=False91# Olivier Lecluse # 8 mars 2019 # version ordi from random import randint from microbit import * import radio,music radio.config(group=1) radio.on() boats = [] gameOn = False def placer(): global boats boats=[] h=randint(0,1) if h==0: # bateau de 3 horizontal x3=randint(0,2) y3=randint(0,4) boats.append((x3,y3)) boats.append((x3+1,y3)) boats.append((x3+2,y3)) else: # bateau de 3 vertical x3=randint(0,4) y3=randint(0,2) boats.append((x3,y3)) boats.append((x3,y3+1)) boats.append((x3,y3+2)) cont = True while cont : if h==0: # bateau de 2 verrtical x2=randint(0,4) y2=randint(0,3) if (x2,y2) not in boats and (x2, y2+1) not in boats: boats.append((x2,y2)) boats.append((x2,y2+1)) cont=False else: # bateau de 2 horizontal x2=randint(0,3) y2=randint(0,4) if (x2,y2) not in boats and (x2+1, y2) not in boats: boats.append((x2,y2)) boats.append((x2+1,y2)) cont=False def afficher(): display.clear() for b in boats: display.set_pixel(b[0],b[1],9) placer() afficher() while True: if button_a.was_pressed() and not gameOn: placer() afficher() if button_b.was_pressed() and not gameOn: gameOn = True radio.send("GO") music.play(music.POWER_UP) if gameOn: # la partie est en cours # Gestion de l'arrivee d'un coup incoming = radio.receive() if incoming: try: hx,hy = incoming.split(',') hit=(int(hx),int(hy)) except: continue if (hit) in boats: boats.remove(hit) radio.send("HIT") music.pitch(1000, 500) color=0 else: radio.send("MISS") music.pitch(200, 500) color=2 display.set_pixel(hit[0], hit[1], color) # Fin de la partie if len(boats)==0: radio.send("GAME OVER") music.play(music.FUNERAL) gameOn=False

Méthode : Version Joueur

  • Le bouton A déplace la cible verticalement

  • Le bouton B déplace la cible horizontalement

  • Une secousse déclenche le tir

1# Olivier Lecluse2# 8 mars 20193# version joueur45from microbit import *6import radio78radio.config(group=1)9radio.on()1011nbHits = 012gameOn = False13movePixColor = 514missPixColor = 215hitPixColor = 91617def deplacement():18 global x, y, lastX, lastY, lastPix19 # On rétablit l'ancien pixel20 display.set_pixel(lastX, lastY, lastPix)21 22 lastPix = display.get_pixel(x,y)23 lastX, lastY=x, y24 display.set_pixel(x,y,movePixColor)252627while True:28 if not gameOn:29 # en atttente du placement des bateaux30 incoming = radio.receive()31 if incoming == "GO":32 gameOn = True33 display.show("Go")34 sleep(500)35 display.clear()36 nbHits=037 x,y = 0,0 # position du tir38 lastPix = 039 lastX, lastY=0,040 display.set_pixel(0,0,movePixColor)41 else :42 # jouer la partie43 if button_b.was_pressed():44 x = (x+1)%545 deplacement()46 if button_a.was_pressed():47 y = (y+1)%548 deplacement()49 if accelerometer.was_gesture("shake"):50 message = "{},{}".format(x, y)51 radio.send(message)52 response = False53 while not response:54 incoming = radio.receive()55 if incoming == "HIT":56 display.set_pixel(x,y,hitPixColor)57 lastPix = hitPixColor58 response = True59 nbHits += 160 elif incoming == "MISS":61 display.set_pixel(x,y,missPixColor)62 lastPix = missPixColor63 response = True64 elif incoming == "GAME OVER":65 display.set_pixel(x,y,hitPixColor)66 lastPix = hitPixColor67 display.show("WIN")68 gameOn = False69 response = True70 elif incoming:71 response = True72 if nbHits == 5:73 gameOn = False74 display.show("WIN")75# Olivier Lecluse # 8 mars 2019 # version joueur from microbit import * import radio radio.config(group=1) radio.on() nbHits = 0 gameOn = False movePixColor = 5 missPixColor = 2 hitPixColor = 9 def deplacement(): global x, y, lastX, lastY, lastPix # On rétablit l'ancien pixel display.set_pixel(lastX, lastY, lastPix) lastPix = display.get_pixel(x,y) lastX, lastY=x, y display.set_pixel(x,y,movePixColor) while True: if not gameOn: # en atttente du placement des bateaux incoming = radio.receive() if incoming == "GO": gameOn = True display.show("Go") sleep(500) display.clear() nbHits=0 x,y = 0,0 # position du tir lastPix = 0 lastX, lastY=0,0 display.set_pixel(0,0,movePixColor) else : # jouer la partie if button_b.was_pressed(): x = (x+1)%5 deplacement() if button_a.was_pressed(): y = (y+1)%5 deplacement() if accelerometer.was_gesture("shake"): message = "{},{}".format(x, y) radio.send(message) response = False while not response: incoming = radio.receive() if incoming == "HIT": display.set_pixel(x,y,hitPixColor) lastPix = hitPixColor response = True nbHits += 1 elif incoming == "MISS": display.set_pixel(x,y,missPixColor) lastPix = missPixColor response = True elif incoming == "GAME OVER": display.set_pixel(x,y,hitPixColor) lastPix = hitPixColor display.show("WIN") gameOn = False response = True elif incoming: response = True if nbHits == 5: gameOn = False display.show("WIN")
  • Précédent
  • Suivant
  • Introduction
  • Adafruit ? CircuitPython ?
  • Découverte de CircuitPython avec edublocks
  • de l'Arduino vers CircuitPython
  • projets CPX
  • Projets divers autour de CircuitPython
  • Graphisme sous CircuitPython - utilisation de displayio
  • Fabriquer sa console de jeu - Niveau avancé
  • Objets connectés (IoT) - MicroPython sur ESP8266 et ESP32
  • Micropython sur la carte BBC micro::bit
    • Introduction
    • Utiliser l'éditeur Mu
    • kit de survie de la carte microbit sous MicroPython
    • Activités de prise en main de la micro:bit
    • Mini-projets sur Microbit
      • Réaliser un compteur - niveau facile
      • Jeu de Pierre Feuille Ciseaux
      • Robot Maqueen eviteur d'obstacles - Niveau débutant
      • rob_hot : le robot guidé à la chaleur - niveau intermédiaire
      • Robot Maqueen télécommandé - Niveau intermédiaire
      • Alerte canicule ! - niveau facile
      • Simulation d'un feu de chantier pour la circulation automobile
      • Mesurer la vitesse du son
      • Lire un GPS avec la carte micro:bit - Niveau intermédiaire
      • le jeu de bataille navale - Niveau intermédiaire
      • Jeu de tir : scudFighter - Niveau intermédiaire
      • La roue codeuse - Niveau intermédiaire
      • Effet stroboscopique - Niveau intermédiaire
  • Accueil
  • Module

Tag » Code Bataille Navale Python