pygame detect click
Certainly! Here's an example of how you can use Pygame to detect a mouse click in the C programming language:
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
bool isClickDetected = false;
void handleEvent(SDL_Event event) {
if (event.type == SDL_MOUSEBUTTONDOWN) {
isClickDetected = true;
}
}
int main() {
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;
// Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
// Create a window and renderer
SDL_CreateWindowAndRenderer(800, 600, 0, &window, &renderer);
// Main loop
while (true) {
// Process events
while (SDL_PollEvent(&event)) {
handleEvent(event);
}
// Clear the screen
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
// Draw something on the screen (optional)
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_Rect rect = { 100, 100, 200, 200 };
SDL_RenderFillRect(renderer, &rect);
// Update the screen
SDL_RenderPresent(renderer);
// Check if a click is detected
if (isClickDetected) {
printf("Mouse click detected!\n");
isClickDetected = false;
}
}
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
This example sets up a basic Pygame window and renderer, and then enters a main loop where it continuously checks for mouse events. If a mouse button down event is detected, it sets the isClickDetected
variable to true. Inside the main loop, you can perform any necessary operations when a click is detected.
Please note that in order to compile and run this code, you'll need to have the SDL2 library installed and properly configured on your system.
I hope this example helps! Let me know if you have any further questions.