Here's a simple Python game with a graphical user interface (GUI) using the tkinter library. The game is called "Space Invaders," and it involves shooting incoming asteroids to protect your spaceship. This is a basic example, and you can get creative and expand upon it!
python
import tkinter as tk
from tkinter import messagebox
import random
class SpaceInvaders:
def __init__(self, root):
self.root = root
self.root.title("Space Invaders")
self.root.geometry("400x600")
self.canvas = tk.Canvas(root, width=400, height=500)
self.canvas.pack()
self.score = 0
self.lives = 3
self.asteroids_hit = 0
self.player_ship = self.canvas.create_rectangle(180, 470, 220, 490, fill="blue")
self.asteroid_list = []
self.asteroid_speed = 5
self.create_asteroids()
self.bullet = None
self.bullet_speed = 10
self.canvas.focus_set()
self.canvas.bind("<space>", self.shoot_bullet)
self.root.bind("<Left>", self.move_left)
self.root.bind("<Right>", self.move_right)
self.score_label = tk.Label(root, text=f"Score: {self.score}", font=("Arial", 12))
self.score_label.pack(side=tk.LEFT)
self.lives_label = tk.Label(root, text=f"Lives: {self.lives}", font=("Arial", 12))
self.lives_label.pack(side=tk.RIGHT)
self.update()
def create_asteroids(self):
for _ in range(5):
x = random.randrange(50, 350)
y = random.randrange(50, 150)
asteroid = self.canvas.create_oval(x, y, x+20, y+20, fill="gray")
self.asteroid_list.append(asteroid)
def move_asteroids(self):
for asteroid in self.asteroid_list:
x1, y1, x2, y2 = self.canvas.coords(asteroid)
x_move = random.choice([-1, 1]) * self.asteroid_speed
new_x = x1 + x_move
if new_x < 0 or new_x > 400:
x_move *= -1
new_x = x1 + x_move
self.canvas.move(asteroid, x_move, 0)
def shoot_bullet(self, event):
if self.bullet is None:
bullet_x = 200
bullet_y = 400
self.bullet = self.canvas.create_rectangle(bullet_x, bullet_y, bullet_x+10, bullet_y+10, fill="red")
def move_bullet(self):
if self.bullet is not None:
x, y, _, _ = self.canvas.coords(self.bullet)
self.canvas.move(self.bullet, 0, -self.bullet_speed)
if y < 0:
self.bullet = None
def move_player_ship(self, direction):
x1, y1, x2, y2 = self.canvas.coords(self.player_ship)
move_amount = 10
if direction == "left":
new_x1 = max(x1 - move_amount, 0)
new_x2 = max(x2 - move_amount, 0)
elif direction == "right":
new_x1 = min(x1 + move_amount, 400)
new_0x2 = min(x2 + move_amount, 400)
self.canvas.coords(self.player_ship, new_x1, y1, new_x2, y2)
def move_left(self, event):
self.move_player_ship("left")
def move_right(selfMultiplier:
self.move_player_ship("right")
def check_collisions(self):
bullet_coords = self.canvas.coords(self.bullet) if self.bullet else None
for asteroid in self.asteroid_list:
asteroid_coords = self.canvas.coords(asteroid)
if bullet_coords and self.overlaps(bullet_coords, asteroid_coords):
self.canvas.delete(self.bullet)
self.bullet = None
self.canvas.delete(asteroid)
self.asteroid_list.remove(asteroid)
self.asteroids_hit += 1
self.score += 100
if self.asteroids_hit == len(self.asteroid_list):
self.asteroids_hit = 0
self.create_asteroids()
player_ship_coords = self.canvas.coords(self.player_ship)
for asteroid in self.asteroid_list:
asteroid_coords = self.canvas.coords(asteroid)
if self.overlaps(player_ship_coords, asteroid_coords):
self.lives -= 1
if self.lives == 0:
messagebox.showinfo("Game Over", f"Final Score: {self.score}")
self.root.destroy()
else:
self.player_ship = self.canvas.create_rectangle(180, 470, 220, 490, fill="blue")
def overlaps(self, rect1, rect2):
return rect1[0] < rect2[2] and rect1[2] > rect2[0] and rect1[1] < rect2[3] and rect1[3] > rect2[1]
def update(self):
self.move_asteroids()
self.move_bullet()
self.check_collisions()
self.score_label.config(text=f"Score: {self.score}")
self.lives_label.config(text=f"Lives: {self.lives}")
self.root.after(50, self.update)
if __name__ == "__main__":
root = tk.Tk()
game = SpaceInvaders(root)
root.mainloop()
In this game, the player controls a spaceship at the bottom of the screen and can move it left and right using the arrow keys. Asteroids fall from the top of the screen, and the player can shoot bullets at them by pressing the spacebar. The goal is to destroy the asteroids before they hit the spaceship. The game keeps track of the player's score and lives.
Feel free to modify and expand upon this game by adding more features, levels, power-ups, and improving the graphics!
“code a game with a gui on python about whatever you want, be creative.” Conversation with command-r-plus, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m851epden4newh0rt2ndn40s