Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Simple graphics game in Python - Step by Step Guide

How to create the Catch the Dot game using Python: Step by step guide and full source code

We're going to create a fun and simple game using Python's Turtle graphics and Tkinter library. This game is called 'Catch the Dot,' and you'll learn how to build it step-by-step. Let's get started!"

First, let's set up our environment. Open your favorite Python IDE, and make sure you have latest Python installed. We'll be using the `turtle` and `tkinter` libraries for this project.

Start by importing the necessary libraries. We'll import `turtle`, `random`, `time`, and `tkinter`.


```python
import turtle
import random
import time
import tkinter as tk
```


Next, let's create the main window for our game using Tkinter. We'll set up two frames: one for the Turtle graphics and one for the timer and level display.

```python
# Create the main window
root = tk.Tk()
root.title("Catch the Dot Game")

# Create a frame for the turtle game
game_frame = tk.Frame(root)
game_frame.pack(side=tk.LEFT)

# Create a frame for the timer and level display
info_frame = tk.Frame(root)
info_frame.pack(side=tk.RIGHT, padx=20)
```


Now, let's set up the canvas for the Turtle graphics within the game frame.


```python
# Create a canvas for the turtle game and add it to the game frame
canvas = turtle.ScrolledCanvas(game_frame, width=600, height=600)
canvas.pack()

# Setup turtle screen
screen = turtle.TurtleScreen(canvas)
screen.bgcolor("white")
```

With the canvas ready, let's create the player turtle and the dot it needs to catch.



```python
# Setup player turtle
player = turtle.RawTurtle(screen)
player.shape("turtle")
player.color("blue")
player.penup()
player.speed(0)

# Setup dot
dot = turtle.RawTurtle(screen)
dot.shape("circle")
dot.color("red")
dot.penup()
dot.speed(0)
dot.goto(random.randint(-290, 290), random.randint(-290, 290))
```

To display the timer and level, we'll add labels to the info frame.


```python
# Setup timer and level labels
timer_label = tk.Label(info_frame, text="Time: 30", font=("Arial", 16))
timer_label.pack(pady=10)
level_label = tk.Label(info_frame, text="Level: 1", font=("Arial", 16))
level_label.pack(pady=10)
```

Next, we'll define functions to control the player turtle's movement and update the timer and level display.


```python
# Player movement functions
def go_up():
    y = player.ycor()
    if y < 290:
        player.sety(y + 20)

def go_down():
    y = player.ycor()
    if y > -290:
        player.sety(y - 20)

def go_left():
    x = player.xcor()
    if x > -290:
        player.setx(x - 20)

def go_right():
    x = player.xcor()
    if x < 290:
        player.setx(x + 20)

# Keyboard bindings
screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
screen.onkey(go_right, "Right")
```


Now, let's create functions to update the timer and level.


```python
# Function to update the timer
def update_timer(time_left):
    timer_label.config(text=f"Time: {time_left}")

# Function to update the level
def update_level(level):
    level_label.config(text=f"Level: {level}")
```

Finally, we'll write the main game loop to handle the game logic and start the game.


```python
# Main game loop
level = 1
time_limit = 30

def game_loop():
    global level, time_limit
    
    start_time = time.time()
    caught = False
    
    while time.time() - start_time < time_limit:
        screen.update()
        
        # Check for collision with dot
        if player.distance(dot) < 20:
            dot.goto(random.randint(-290, 290), random.randint(-290, 290))
            caught = True
            break
        
        time_left = time_limit - int(time.time() - start_time)
        update_timer(time_left)
        
    if not caught:
        timer_label.config(text="Time's up! Game Over")
        return
    
    level += 1
    time_limit = max(5, time_limit - 5)  # Reduce time limit but not below 5 seconds
    update_level(level)
    root.after(1000, game_loop)

# Start the game loop
root.after(1000, game_loop)

# Start the Tkinter event loop
root.mainloop()
```

And that's it! We've successfully created a simple 'Catch the Dot' game using Python's Turtle and Tkinter libraries. You can customize this game further by adding more features or enhancing the graphics.




caa July 04 2024 165 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 11
Newest Member: Jhilam