Browse Source

some game idea

Aubort Jean-Baptiste 8 months ago
parent
commit
eb29d4fa10
2 changed files with 113 additions and 22 deletions
  1. BIN
      images/police.png
  2. 113 22
      slides.ipynb

BIN
images/police.png


+ 113 - 22
slides.ipynb

@@ -137,16 +137,26 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 73,
+   "execution_count": 1,
    "metadata": {
     "slideshow": {
      "slide_type": "subslide"
     }
    },
-   "outputs": [],
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "pygame 1.9.4\n",
+      "Hello from the pygame community. https://www.pygame.org/contribute.html\n"
+     ]
+    }
+   ],
    "source": [
     "import pygame\n",
     "from pygame import Vector2, Rect, Surface, sprite, Color\n",
+    "import random\n",
     "\n",
     "x, y = (800, 600)\n",
     "pos = Rect(x/2 - 107/2, 50, 107, 50)\n",
@@ -189,7 +199,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 74,
+   "execution_count": 2,
    "metadata": {
     "slideshow": {
      "slide_type": "subslide"
@@ -226,7 +236,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 75,
+   "execution_count": 3,
    "metadata": {
     "slideshow": {
      "slide_type": "subslide"
@@ -234,7 +244,7 @@
    },
    "outputs": [],
    "source": [
-    "def render(func=None):\n",
+    "def render():\n",
     "    global running, ground\n",
     "    init()\n",
     "    while running:\n",
@@ -254,11 +264,10 @@
     "        ground = pygame.draw.line(screen, pygame.Color('black'), (0, y), (x, y), 25)\n",
     "                    \n",
     "        # Custom function\n",
-    "        if func != None:\n",
-    "            func()\n",
+    "        drone.update()\n",
+    "        ui.update()\n",
     "        \n",
     "        # Drawing everything\n",
-    "        ui.update()\n",
     "        all.draw(screen)\n",
     "        pygame.display.flip()\n",
     "                                    \n",
@@ -319,7 +328,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 76,
+   "execution_count": 4,
    "metadata": {
     "slideshow": {
      "slide_type": "fragment"
@@ -342,7 +351,10 @@
     "        self.rect = pos.copy()\n",
     "\n",
     "    def reset(self):\n",
-    "        self.rect = pos.copy()"
+    "        self.rect = pos.copy()\n",
+    "        \n",
+    "    def update(self):\n",
+    "        pass"
    ]
   },
   {
@@ -385,7 +397,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 77,
+   "execution_count": 5,
    "metadata": {
     "slideshow": {
      "slide_type": "fragment"
@@ -411,7 +423,10 @@
     "    def reset(self):\n",
     "        self.velocity = Vector2(0, 0)\n",
     "        self.acceleration = Vector2(0, 1)\n",
-    "        super().reset()"
+    "        super().reset()\n",
+    "        \n",
+    "    def update(self):\n",
+    "        self.move()"
    ]
   },
   {
@@ -423,7 +438,7 @@
    },
    "source": [
     "drone = DroneBrick()\n",
-    "render(drone.move)"
+    "render()"
    ]
   },
   {
@@ -452,7 +467,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 78,
+   "execution_count": 6,
    "metadata": {
     "slideshow": {
      "slide_type": "fragment"
@@ -472,17 +487,15 @@
    ]
   },
   {
-   "cell_type": "code",
-   "execution_count": 79,
+   "cell_type": "raw",
    "metadata": {
     "slideshow": {
      "slide_type": "fragment"
     }
    },
-   "outputs": [],
    "source": [
     "drone = DroneThrust()\n",
-    "render(drone.move)"
+    "render()"
    ]
   },
   {
@@ -510,7 +523,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 80,
+   "execution_count": 7,
    "metadata": {
     "slideshow": {
      "slide_type": "fragment"
@@ -519,7 +532,7 @@
    "outputs": [],
    "source": [
     "class DronePitch(DroneThrust):\n",
-    "    \n",
+    "        \n",
     "    def move(self):\n",
     "        super().move()"
    ]
@@ -533,7 +546,83 @@
    },
    "source": [
     "drone = DronePitch()\n",
-    "render(drone.move)"
+    "render()"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "slideshow": {
+     "slide_type": "subslide"
+    }
+   },
+   "source": [
+    "### A game ?"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {
+    "slideshow": {
+     "slide_type": "notes"
+    }
+   },
+   "source": [
+    "- Let's have fun"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "slideshow": {
+     "slide_type": "fragment"
+    }
+   },
+   "outputs": [],
+   "source": [
+    "class DroneGame(DroneThrust):\n",
+    "    \n",
+    "    life = 100\n",
+    "    \n",
+    "    def move(self):\n",
+    "        super().move()\n",
+    "        \n",
+    "class Police(sprite.Sprite):\n",
+    "    \n",
+    "    def __init__(self):\n",
+    "        self.containers = all\n",
+    "        sprite.Sprite.__init__(self, self.containers)\n",
+    "        self.image = pygame.image.load('./images/police.png')\n",
+    "        self.rect = (random.randint(0, x), random.randint(0, y), 128, 50)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "slideshow": {
+     "slide_type": "fragment"
+    }
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "(579, 186, 128, 50)\n",
+      "(716, 146, 128, 50)\n",
+      "(392, 547, 128, 50)\n",
+      "(457, 313, 128, 50)\n",
+      "(464, 308, 128, 50)\n"
+     ]
+    }
+   ],
+   "source": [
+    "drone = DroneGame()\n",
+    "for i in range(5): \n",
+    "    all.add(Police())\n",
+    "render()"
    ]
   },
   {
@@ -588,7 +677,9 @@
     "* Complete guide: https://www.propwashed.com/drone-racing-guide/\n",
     "* Lift equation: https://wright.nasa.gov/airplane/lifteq.html\n",
     "* Physics of Drones: https://www.wired.com/2017/05/the-physics-of-drones/\n",
-    "* Drone icon https://www.flaticon.com/free-icon/drone_90909"
+    "* Drone icons\n",
+    " * https://www.flaticon.com/free-icon/drone_90909\n",
+    " * https://www.flaticon.com/free-icon/drone_90894"
    ]
   }
  ],