İlk olarak, deneye basit ve anlaşılır bir soruyla başladım. ChatGPT'ye "Pygame kullanarak benim için bir Astro Shooter oyunu kodlayabilir misin?" diye sordum.

ChatGPT bana bir parça temel kodla aşağıdaki gibi tereddüt etmeden cevap verdi:
import pygame
# Initialize the game engine
pygame.init()
# Set the screen size
screen = pygame.display.set_mode((800, 600))
# Set the title of the window
pygame.display.set_caption("Astro Shooter")
# Load the player image
player_image = pygame.image.load("player.png")
# Set the player starting position
player_x = 400
player_y = 500
# Load the enemy image
enemy_image = pygame.image.load("enemy.png")
# Set the enemy starting position
enemy_x = 50
enemy_y = 50
# Set the running flag to True
running = True
# Game loop
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Draw the player
screen.blit(player_image, (player_x, player_y))
# Draw the enemy
screen.blit(enemy_image, (enemy_x, enemy_y))
# Update the display
pygame.display.update()
# Quit the game engine
pygame.quit()ChatGPT tarafından verilen ilk kod parçası temelde sadece bir oyun tuvalidir. Kendi uzay geminiz ve bir düşman için bir yer tutucunuz olabilir ve başka hiçbir şeye sahip olamazsınız, buna oyun bile diyemezsiniz çünkü uzay gemisi tasarımı bile yokDaha sonra proje paketine eklenecek olan Itch.io'dan bazı basit uzay gemisi malzemeleri buldum.

Sadece bir tuvale sahip olmak yeterli değil, uzay gemimizi hareket ettirmek için ChatGPT'nin yeni kod yazmasına, daha fazla işlev çağırmasına izin vermeliyiz. Bu nedenle, ChatGPT'ye ikinci isteğimi önerdim: uzay gemisini ok tuşlarıyla sola ve sağa hareket ettirmek için bir işlev ekleyin.
Bu, oyun döngüsüne eklenen taşıma işlevidir, işte tam kod:
# Game loop
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= player_speed
if event.key == pygame.K_RIGHT:
player_x += player_speedChatGPT tarafından oluşturulan kodu çalıştırmaya çalışıyorum ancak ok tuşlarına bastığımda uzay gemisi çok hızlı hareket ediyor ve hatta sınırların dışına çıkıyor.
Nispeten net bir istekte bulunduktan sonra nihayet ideal bir oyun kodu aldım.
Her zamanki gibi çalışması için kodu kopyalayıp Lightly IDE'ye yapıştırın. Lightly IDE'nin, çoğu programlama projesini herhangi bir kurulum yapmadan tarayıcıda çalıştırmanıza izin veren gerçekten çok kullanışlı bir çevrimiçi düzenleyici olduğunu söylemeliyim.

Oyunun normal şekilde çalışması için ChatGPT'nin daha fazla kod oluşturmasına izin verin
ChatGPT'den daha fazla düşman yaratmak, uzay gemisinin füze atmasını sağlamak ve füze tarafından vurulduğunda düşman uzay gemisinin ortadan kaybolmasını sağlamak dahil olmak üzere daha fazla gereksinim istedim.
ChatGPT'de girdiğim bazı gereksinimler şunlardır:
- Oyunda on düşman daha görünsün.
- Boşluk çubuğuna basıldığında oyuncunun füzeleri ateşlemesine izin verir.
- Füze bir düşmana çarptığında, düşmanı ortadan kaldırın. Füze bir düşmana çarparsa oyuncu 100 puan alır.
Örnek: ChatGPT'den benim için daha fazla düşman oluşturmasını ilk kez istediğimde. Sadece bir grup düşmanı gösteriyor, hatta bazıları oyuncunun altında görünüyor ve bu da ateş edilmesi imkansız hale geliyor. Açıkçası, bu benim tasavvur ettiğim şey değildi.

düşmanı harekete geçirmek
Mevcut versiyonda, oyundaki hareket eden tek şey oyuncunun gemisi ve füzeleridir. Düşman gemisi, doğma noktasına tamamen demirlenmiş olarak hareketsiz kalır.
Bu yüzden, oyuncuya doğru ilerlemeye devam etmeleri için düşmanlara biraz hareket eklemem gerekiyor.
Programlamaya yeterince aşinaysanız, bunu başarmanın zor olmadığını anlamalısınız, ancak ChatGPT'ye sade bir dille komut vererek nispeten tatmin edici bir sonuç elde etmenin biraz zaman alacağını anlamalısınız.
İşte bir iletişim hatası örneği:

ChatGPT'nin size daha iyi ve daha doğru yanıtlar vermesini sağlamanın en iyi yolu, aşağıdaki gibi belirli sorular sormaktır:

ChatGPT size nelerin değiştirilebileceğini söyleyecek ve ilgili kod parçacıklarını verecektir. Burada, oluşturulan kodu doğrudan kopyalayıp yapıştırmak yerine kodu manuel olarak kontrol edebilir ve gerekenleri değiştirebilirsiniz.
Oyunu biraz daha iyi hale getirmek için ChatGPT'ye aşağıda birkaç soru daha sordum.
- Füzeyi beyaz bir dikdörtgene nasıl değiştirebilirim?
- Düşmanların oyuncunun altında görünmemesi için kodu nasıl değiştirebilirim?
- Düşmanların daha yavaş hareket etmesi için kodu nasıl değiştirebilirim?

Ana Kaynak Kodu:
final.py açın ve ekleyin
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((800, 600))
# Set up the title and icon
pygame.display.set_caption("Astro Shooter")
# icon = pygame.image.load("rocket.png")
# pygame.display.set_icon(icon)
# Set up the player
player_image = pygame.image.load("player.png")
player_x = 370
player_y = 480
player_x_change = 0
# Set up the enemies
enemy_image = pygame.image.load("enemy.png")
enemy_width, enemy_height = enemy_image.get_size()
enemies = []
for i in range(10):
enemy_x = random.randint(0, 736)
enemy_y = random.randint(50, 150)
enemy = [enemy_image, enemy_x, enemy_y, 0.2, 10]
enemies.append(enemy)
enemy_boundary = player_y - enemy_height # Set the vertical boundary
# Set up the bullet
bullet_width = 3
bullet_height = 9
bullet_color = (255, 255, 255)
bullet_rect = pygame.Rect(0, 0, bullet_width, bullet_height)
bullet_surface = pygame.Surface(bullet_rect.size)
bullet_surface.fill(bullet_color)
bullet_x = 0
bullet_y = 480
bullet_y_change = 2
bullet_state = "ready"
# Set up the font
font = pygame.font.Font("freesansbold.ttf", 32)
# Set up the score
score_value = 0
def show_score(x, y):
score = font.render("Score: " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
# Draw the player on the screen
def player(x, y):
screen.blit(player_image, (x, y))
# Draw the enemy on the screen
def enemy(x, y, image):
screen.blit(image, (x, y))
# Fire the bullet
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_surface, (x + 16, y + 10))
# Check for collision
def is_collision(enemy_x, enemy_y, bullet_x, bullet_y):
distance = ((enemy_x - bullet_x) ** 2 + (enemy_y - bullet_y) ** 2) ** 0.5
if distance < 27:
return True
else:
return False
def game_over(score):
font = pygame.font.Font("freesansbold.ttf", 64)
text = font.render("GAME OVER", True, (255, 255, 255))
score_text = font.render("Final Score: " + str(score), True, (255, 255, 255))
screen.blit(text, (200, 250))
screen.blit(score_text, (200, 320))
# Game loop
running = True
while running:
# Fill the screen with black
screen.fill((0, 0, 0))
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the player
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change = -0.2
elif event.key == pygame.K_RIGHT:
player_x_change = 0.2
elif event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_x = player_x
fire_bullet(bullet_x, bullet_y)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player_x_change = 0
# Move the player
player_x += player_x_change
if player_x < 0:
player_x = 0
elif player_x > 736:
player_x = 736
# Move the enemies
for enemy in enemies:
enemy_x = enemy[1]
enemy_y = enemy[2]
enemy_x += enemy[3]
if enemy_x < 0 or enemy_x > 736:
enemy[3] = -enemy[3]
enemy_y += enemy[4]
enemy[1] = enemy_x
enemy[2] = enemy_y
# Draw the enemy
screen.blit(enemy[0], (enemy_x, enemy_y))
# Check for collisions between bullets and enemies
collision = is_collision(enemy_x, enemy_y, bullet_x, bullet_y)
if collision:
bullet_y = 480
bullet_state = "ready"
score_value += 100
enemy_x = random.randint(0, 736)
enemy_y = random.randint(50, 150)
enemy = [enemy_image, enemy_x, enemy_y, 0.2, 10]
enemies.append(enemy)
# Move the bullet
if bullet_y <= 0:
bullet_y = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bullet_x, bullet_y)
bullet_y -= bullet_y_change
# Draw the player
player(player_x, player_y)
# Show the score
show_score(10, 10)
# Update the display
pygame.display.update()main.py açın ve ekleyinimport pygame
import random
# Initialize the game engine
pygame.init()
# Set the screen size
screen = pygame.display.set_mode((800, 600))
# Set the title of the window
pygame.display.set_caption("Astro Shooter")
# Load the player image
player_image = pygame.image.load("player.png")
# Load the missile image
missile_image = pygame.image.load("missile.png")
# Set the player starting position
player_x = 400
player_y = 500
# Set the screen width and height
screen_width = 800
screen_height = 600
# Set the player speed
player_speed = 0.1
# List to store the missile objects
missiles = []
# Set a flag to track if the left arrow key is being pressed
left_pressed = False
# Set a flag to track if the right arrow key is being pressed
right_pressed = False
# Load the enemy image
enemy_image = pygame.image.load("enemy.png").convert_alpha()
# List to store the enemy objects
enemies = []
# Create 10 initial enemies
for i in range(10):
enemy = pygame.Rect(
random.randint(0, screen_width - enemy_image.get_width()),
0 - enemy_image.get_height() * i,
enemy_image.get_width(),
enemy_image.get_height()
)
enemies.append(enemy)
# Timer to keep track of time elapsed since last move
last_move_down = pygame.time.get_ticks()
last_move_towards_player = pygame.time.get_ticks()
# Score
score = 0
# Set the running flag to True
running = True
# Game loop
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
left_pressed = True
if event.key == pygame.K_RIGHT:
right_pressed = True
if event.key == pygame.K_SPACE:
# Create a new missile object and add it to the missiles list
missile = pygame.Rect(
player_x + player_image.get_width() / 2,
player_y,
3, 9
)
missiles.append(missile)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_pressed = False
if event.key == pygame.K_RIGHT:
right_pressed = False
# Move the player left or right if the corresponding arrow key is pressed
if left_pressed:
player_x -= player_speed
if player_x < 0:
player_x = 0
if right_pressed:
player_x += player_speed
if player_x > screen_width - player_image.get_width():
player_x = screen_width - player_image.get_width()
# Move the missiles upward
for missile in missiles:
missile.y -= 0.1
# Remove the missiles that have left the screen
missiles = [missile for missile in missiles if missile.y > 0]
# Clear the screen
screen.fill((0, 0, 0))
# Draw the player
screen.blit(player_image, (player_x, player_y))
# Check if any enemy is hit by the missile
for missile in missiles:
for enemy in enemies:
if enemy.colliderect(missile):
enemies.remove(enemy)
score += 100
missiles.remove(missile)
break
else:
continue
break
# Check if it's time to add more enemies
if score > 0 and score % 1000 == 0:
# Add one more enemy for each 1000 points
for i in range(score // 1000):
enemy = pygame.Rect(
random.randint(0, screen_width - enemy_image.get_width()),
0 - enemy_image.get_height(),
enemy_image.get_width(),
enemy_image.get_height()
)
enemies.append(enemy)
# Get the current time
current_time = pygame.time.get_ticks()
# Check if 0.5 seconds have elapsed since the last move towards the player
if current_time - last_move_towards_player >= 500:
# Move the enemies towards the player
for enemy in enemies:
if enemy.x < player_x:
enemy.x += 2
elif enemy.x > player_x:
enemy.x -= 2
# Reset the timer
last_move_towards_player = current_time
# Check if 0.5 second has elapsed since the last move down
if current_time - last_move_down >= 500:
# Move the enemies down
for enemy in enemies:
enemy.y += 2
# Reset the timer
last_move_down = current_time
# Draw the enemies on the screen
for enemy in enemies:
if enemy.y >= 0 and enemy.y <= screen_height - enemy.height:
screen.blit(enemy_image, (enemy.x, enemy.y))
# Draw the missiles on the screen
for missile in missiles:
pygame.draw.rect(screen, (255, 255, 255), missile)
# Update the display
pygame.display.update()
# Quit the game engine
pygame.quit()tester.py açın ve ekleyinimport pygame
import random
# Initialize the game engine
pygame.init()
# Set the screen size
screen = pygame.display.set_mode((800, 600))
# Set the title of the window
pygame.display.set_caption("Astro Shooter")
# Load the player image
player_image = pygame.image.load("player.png")
# Set the player starting position
player_x = 400
player_y = 500
# Set the player speed
player_speed = 0.1
# Set the screen width and height
screen_width = 800
screen_height = 600
# Set a flag to track if the left arrow key is being pressed
left_pressed = False
# Set a flag to track if the right arrow key is being pressed
right_pressed = False
# Set the enemy starting position
enemy_x = 50
enemy_y = 50
# Set the running flag to True
running = True
# List to store the missile objects
missiles = []
# Load the enemy image
enemy_image = pygame.image.load("enemy.png").convert_alpha()
# List to store the enemy objects
enemies = []
# Create 10 initial enemies
for i in range(10):
enemy = pygame.Rect(
random.randint(0, screen_width - enemy_image.get_width()),
random.randint(0, screen_height // 2),
enemy_image.get_width(),
enemy_image.get_height()
)
enemies.append(enemy)
# Create 10 additional enemies
for i in range(10):
enemy = pygame.Rect(
random.randint(0, screen_width - enemy_image.get_width()),
random.randint(screen_height // 2, screen_height - enemy_image.get_height()),
enemy_image.get_width(),
enemy_image.get_height()
)
enemies.append(enemy)
# Game loop
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
left_pressed = True
if event.key == pygame.K_RIGHT:
right_pressed = True
if event.key == pygame.K_SPACE:
# Create a new missile object and add it to the missiles list
missile = pygame.Rect(
player_x + player_image.get_width() / 2,
player_y,
3, 9
)
missiles.append(missile)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
left_pressed = False
if event.key == pygame.K_RIGHT:
right_pressed = False
# Move the player left or right if the corresponding arrow key is pressed
if left_pressed:
player_x -= player_speed
if player_x < 0:
player_x = 0
if right_pressed:
player_x += player_speed
if player_x > screen_width - player_image.get_width():
player_x = screen_width - player_image.get_width()
# Move the missiles upward
for missile in missiles:
missile.y -= 1
# Remove the missiles that have left the screen
missiles = [missile for missile in missiles if missile.y > 0]
# Clear the screen
screen.fill((0, 0, 0))
# Draw the player
screen.blit(player_image, (player_x, player_y))
# Draw the enemies on the screen
for enemy in enemies:
screen.blit(enemy_image, (enemy.x, enemy.y))
# Draw the missiles on the screen
for missile in missiles:
pygame.draw.rect(screen, (255, 255, 255), missile)
# Update the display
pygame.display.update()
# Quit the game engine
pygame.quit()requirements.txt açın ve ekleyinpygame==2.1.2Resimler için
enemy.png

missile.png

player.png

