that coder 32b is a bit better than gpt4o !
#1
by
mirek190
- opened
I created galaxian game in 5 min ... quite advanced version!
Qwen 32b q4km
code
import pygame
import random
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Galaxian Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Player
player_size = 30
player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT - 2 * player_size]
player_speed = 5
# Enemy
enemy_size = 25
enemy_list = []
enemy_speed = 2
# Bullet
bullet_size = 5
bullet_list = []
bullet_speed = 10
# Stars
num_stars = 100
star_positions = [(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) for _ in range(num_stars)]
star_sizes = [random.choice([1, 2]) for _ in range(num_stars)] # Randomly choose size for each star
star_visibility = [random.choice([True, False]) for _ in range(num_stars)] # Initial visibility
# Game loop
game_over = False
clock = pygame.time.Clock()
def drop_enemies(enemy_list):
delay = random.random()
if len(enemy_list) < 10 and delay < 0.1:
x_pos = random.randint(0, SCREEN_WIDTH - enemy_size)
y_pos = 0
color_transition = 0 # Initial color transition value
enemy_list.append([x_pos, y_pos, color_transition])
def draw_hexagon(screen, color, center, size):
points = [
(center[0] + size * 0.5, center[1]),
(center[0] + size * 0.25, center[1] - size * 0.433),
(center[0] - size * 0.25, center[1] - size * 0.433),
(center[0] - size * 0.5, center[1]),
(center[0] - size * 0.25, center[1] + size * 0.433),
(center[0] + size * 0.25, center[1] + size * 0.433)
]
pygame.draw.polygon(screen, color, points)
def draw_enemies(enemy_list):
for enemy in enemy_list:
x, y, color_transition = enemy
# Calculate the color based on color_transition
green_component = int(255 * (1 - color_transition))
blue_component = int(255 * color_transition)
color = (0, green_component, blue_component)
draw_hexagon(screen, color, (x + enemy_size // 2, y + enemy_size // 2), enemy_size)
def update_enemy_positions(enemy_list, score):
for idx, enemy in enumerate(enemy_list):
x, y, color_transition = enemy
if y >= 0 and y < SCREEN_HEIGHT:
y += enemy_speed
color_transition += 0.01 # Increment color transition
if color_transition >= 1:
color_transition = 0 # Reset color transition
enemy_list[idx] = [x, y, color_transition]
else:
enemy_list.pop(idx)
score += 1
return score
def update_bullet_positions(bullet_list):
for bullet in bullet_list[:]: # Iterate over a copy of the list
if bullet[1] > 0:
bullet[1] -= bullet_speed
else:
bullet_list.remove(bullet)
def collision_check(enemy_list, bullet_list):
for bullet in bullet_list[:]: # Iterate over a copy of the list
for enemy in enemy_list[:]: # Iterate over a copy of the list
x, y, _ = enemy
if (bullet[0] >= x and bullet[0] <= x + enemy_size) and \
(bullet[1] >= y and bullet[1] <= y + enemy_size):
enemy_list.remove(enemy)
bullet_list.remove(bullet)
return True
return False
def draw_bullets(bullet_list):
for bullet in bullet_list:
pygame.draw.circle(screen, RED, (bullet[0], bullet[1]), bullet_size)
def player_collision(enemy_list, player_pos):
for enemy in enemy_list:
x, y, _ = enemy
if detect_collision(player_pos, (x, y, enemy_size)):
return True
return False
def detect_collision(player_pos, enemy_pos):
p_x = player_pos[0]
p_y = player_pos[1]
e_x = enemy_pos[0]
e_y = enemy_pos[1]
e_size = enemy_size
if (e_x >= p_x - player_size // 2 and e_x < (p_x + player_size // 2)) or (p_x - player_size // 2 >= e_x and p_x - player_size // 2 < (e_x + e_size)):
if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + e_size)):
return True
return False
def draw_player(player_pos):
points = [
(player_pos[0], player_pos[1]), # Tip of the triangle
(player_pos[0] - player_size // 2, player_pos[1] + player_size), # Left base
(player_pos[0] + player_size // 2, player_pos[1] + player_size) # Right base
]
pygame.draw.polygon(screen, WHITE, points)
def draw_stars(star_positions, star_sizes, star_visibility):
for i, (x, y) in enumerate(star_positions):
if star_visibility[i]:
size = star_sizes[i]
pygame.draw.circle(screen, WHITE, (x, y), size)
def flicker_stars(star_visibility):
for i in range(len(star_visibility)):
star_visibility[i] = random.choice([True, False])
def show_end_screen(score):
screen.fill(BLACK)
font = pygame.font.SysFont("monospace", 32)
end_text = "Game Over! Your Score: " + str(score)
text = font.render(end_text, 1, WHITE)
screen.blit(text, (SCREEN_WIDTH // 2 - text.get_width() // 2, SCREEN_HEIGHT // 2 - text.get_height() // 2))
play_again_text = "Press 'R' to play again or 'Q' to quit"
play_again_label = font.render(play_again_text, 1, WHITE)
screen.blit(play_again_label, (SCREEN_WIDTH // 2 - play_again_label.get_width() // 2, SCREEN_HEIGHT // 2 + text.get_height() // 2 + 20))
pygame.display.update()
def reset_game():
global player_pos, enemy_list, bullet_list, score, star_visibility, star_positions, star_sizes
player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT - 2 * player_size]
enemy_list = []
bullet_list = []
score = 0
star_positions = [(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) for _ in range(num_stars)]
star_sizes = [random.choice([1, 2]) for _ in range(num_stars)]
star_visibility = [random.choice([True, False]) for _ in range(num_stars)]
# Main game loop
score = 0
while True:
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bullet_pos = [player_pos[0], player_pos[1]]
bullet_list.append(bullet_pos)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_pos[0] > player_size // 2:
player_pos[0] -= player_speed
if keys[pygame.K_RIGHT] and player_pos[0] < SCREEN_WIDTH - player_size // 2:
player_pos[0] += player_speed
screen.fill(BLACK)
# Draw stars
draw_stars(star_positions, star_sizes, star_visibility)
flicker_stars(star_visibility)
drop_enemies(enemy_list)
score = update_enemy_positions(enemy_list, score)
update_bullet_positions(bullet_list) # Update bullet positions
# Draw score
text = "Score: " + str(score)
label = pygame.font.SysFont("monospace", 32).render(text, 1, WHITE)
screen.blit(label, (SCREEN_WIDTH // 2 - label.get_width() // 2, 10)) # Center the score at the top
if collision_check(enemy_list, bullet_list):
score += 1
draw_enemies(enemy_list)
draw_bullets(bullet_list)
if player_collision(enemy_list, player_pos):
game_over = True
break
draw_player(player_pos)
clock.tick(30)
pygame.display.update()
show_end_screen(score)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
reset_game()
game_over = False
if event.key == pygame.K_q:
pygame.quit()
quit()