135 lines
4.6 KiB
HTML
135 lines
4.6 KiB
HTML
# Article-22351-Crazy-Scalper-CCanvas-Interactive-Template
| ||||
| ||||
This repository is an article-derived reference project based on the original MQL5 article.
| ||||
| ||||
## Overview
| ||||
| ||||
This project documents a minimal interactive MQL5 canvas application presented as Crazy Scalper, a game-like template running directly on a MetaTrader 5 chart.
| ||||
| ||||
The article explains how to build a reusable graphical skeleton for Expert Advisors using:
| ||||
| ||||
- CCanvas rendering
| ||||
- ARGB transparency
| ||||
- a millisecond timer-driven update loop
| ||||
- keyboard event handling with OnChartEvent
| ||||
- simple physics and AABB collision detection
| ||||
- a finite state machine for screen flow
| ||||
| ||||
The described result is a practical reference for building animated chart overlays, panels, and interactive UI components in MQL5.
| ||||
| ||||
## Original Article
| ||||
| ||||
- **Article ID:** 22351
| ||||
- **Author:** Dayana Cubillas Massana
| ||||
- **Publication date:** 2026.05.20
| ||||
- **Category:** Canvas
| ||||
- **URL:** https://www.mql5.com/es/articles/22351
| ||||
| ||||
## Repository Purpose
| ||||
| ||||
This repository serves as a reference/reconstruction project for the article<6C>s explained architecture and code excerpts.
| ||||
| ||||
Its purpose is to preserve the main implementation ideas behind the article<6C>s prototype:
| ||||
| ||||
- creating a transparent canvas over the chart
| ||||
- running rendering independently from market ticks
| ||||
- structuring logic with explicit game/application states
| ||||
- drawing vector objects directly in code
| ||||
- handling keyboard-driven interaction
| ||||
- using simple movement, obstacles, scoring, and collision rules
| ||||
| ||||
## Key Concepts
| ||||
| ||||
- `#include <Canvas\Canvas.mqh>`
| ||||
- CCanvas initialization with `CreateBitmapLabel`
| ||||
- `COLOR_FORMAT_ARGB_NORMALIZE` for alpha transparency
| ||||
- chart cleanup via `ChartSetInteger(0, CHART_SHOW, false)`
| ||||
- update loop using `EventSetMillisecondTimer(12)`
| ||||
- responsive canvas resizing with `ChartGetInteger` and `canvas.Resize`
| ||||
- state machine with:
| ||||
- `START_SCREEN`
| ||||
- `PLAYING`
| ||||
- `GAME_OVER`
| ||||
- vector rendering with:
| ||||
- `FillRectangle`
| ||||
- `FillTriangle`
| ||||
- `FillCircle`
| ||||
- line drawing methods
| ||||
- basic physics:
| ||||
- gravity
| ||||
- jump impulse
| ||||
- velocity-based movement
| ||||
- AABB collision detection
| ||||
- keyboard control through `CHARTEVENT_KEYDOWN`
| ||||
- dynamic difficulty scaling based on score
| ||||
- use of `PlaySound` for feedback
| ||||
| ||||
## Algorithm / Architecture Summary
| ||||
| ||||
The article describes the project as a single-chart interactive loop:
| ||||
| ||||
**1. Initialization**
| ||||
- Seed RNG with `MathSrand(GetTickCount())`
| ||||
- Hide the standard chart
| ||||
- Read chart dimensions in pixels
| ||||
- Create a bitmap canvas with ARGB support
| ||||
- Reset gameplay variables
| ||||
- Start a high-frequency timer
| ||||
| ||||
**2. Rendering / Update Loop**
| ||||
- `OnTimer()` acts as the main loop
| ||||
- Detect chart resize and resize the canvas
| ||||
- Clear the screen every frame
| ||||
- Update background parallax offset
| ||||
- Draw a grid background
| ||||
- Dispatch behavior by current state:
| ||||
- start screen
| ||||
- active gameplay
| ||||
- game over screen
| ||||
- Push the frame with `canvas.Update()`
| ||||
| ||||
**3. Gameplay State**
| ||||
- Player rocket position is tracked with `g_playerX`, `g_playerY`
| ||||
- Vertical movement uses gravity and jump velocity
| ||||
- Obstacles are stored in a fixed array of candle-like pipes
| ||||
- Obstacles move horizontally and recycle when leaving the screen
| ||||
- Score increments after passing obstacles
| ||||
- Difficulty increases every 10 points by adjusting speed, gap, and spacing
| ||||
| ||||
**4. Collision Logic**
| ||||
- Boundary collision against top/bottom screen limits
| ||||
- AABB overlap checks between player and each obstacle
| ||||
- On collision, switch to GAME_OVER, update best score, and play a sound
| ||||
| ||||
**5. Input Handling**
| ||||
- `OnChartEvent()` listens for keyboard input
| ||||
- Space or Up Arrow:
| ||||
- starts the game from the start screen
| ||||
- applies upward thrust during gameplay
| ||||
- returns to start screen after game over
| ||||
| ||||
## Files Included
| ||||
| ||||
- `Crazy_Scalper.mq5`: The complete, compilable source code for the prototype.
| ||||
- `README.md`: This documentation file.
| ||||
| ||||
## Statistics
| ||||
| ||||
- Code excerpts present: Yes
| ||||
- Complete source attachment available: Yes
| ||||
- Primary language of article text: Spanish
| ||||
- Main MQL5 topics: CCanvas, timers, chart events, vector drawing, physics, collisions
| ||||
| ||||
## Tags
| ||||
| ||||
`MQL5`, `MetaTrader5`, `CCanvas`, `Canvas`, `ARGB`, `ChartEvent`, `OnTimer`, `GameLoop`, `VectorGraphics`, `CollisionDetection`, `UI`, `InteractiveEA`
| ||||
| ||||
## Difficulty
| ||||
| ||||
**Intermediate**
| ||||
| ||||
The article is beginner-friendly in explanation, but the implementation combines multiple MQL5 topics at once: canvas rendering, event-driven logic, frame updates, input handling, and collision math.
| ||||
| ||||
## Reference
| ||||
| ||||
Original article page: https://www.mql5.com/es/articles/22351
|