top of page

學習Python程式語言

學習Python程式是進入AI之門,是最重要的程式語言之一,以射擊遊戲為例,有以下好處:

  1. 提高邏輯思考能力:程式設計需要清晰的邏輯思考和問題解決能力,透過射擊遊戲的設計,學生可以學習如何設計和解決問題的邏輯思路,並提高邏輯思考的能力。

  2. 學習Python程式語言:Python是一種易學易用的程式語言,並且被廣泛應用於科學計算、人工智慧、數據分析等領域。透過射擊遊戲的設計,學生可以學習Python程式語言的基礎知識和應用技巧。

  3. 提高問題解決能力:射擊遊戲的設計需要解決許多問題,例如:如何設計遊戲畫面、如何控制遊戲角色、如何計算分數等。透過解決這些問題,學生可以提高自己的問題解決能力和創造力。

  4. 增強學生的興趣和動機:射擊遊戲是一種充滿挑戰和樂趣的遊戲,透過設計自己的遊戲,學生可以增強對程式設計的興趣和動機,並且感受到創造的樂趣和成就感。

  5. 增加學生的自信心和自我價值感:透過自己的努力設計和完成射擊遊戲,學生可以增加自己的自信心和自我價值感,並且激發自我學習的潛力和動機。

本文的射擊遊戲是由ChartGPT產生,再加以改進,用這方式來學習Python程式,相當有趣。



 

以下就是由ChartGPT產生的python程式:

import pygame

import random


# Initialize Pygame

pygame.init()


# Set up the display

screen_width = 800

screen_height = 600

screen = pygame.display.set_mode((screen_width, screen_height))

pygame.display.set_caption("Space Game")


# Load the images

spaceship_img = pygame.image.load("spaceship.png")

bullet_img = pygame.image.load("bullet.png")

rock_imgs = [

pygame.image.load("rock1.png"),

pygame.image.load("rock2.png"),

pygame.image.load("rock3.png")

]


# Set up the game objects

spaceship_x = screen_width // 2

spaceship_y = screen_height - 100

bullet_x = -1

bullet_y = -1

rock_x = random.randint(0, screen_width)

rock_y = -100

rock_img = random.choice(rock_imgs)

rock_speed = random.randint(1, 3)


# Set up the game loop

clock = pygame.time.Clock()

running = True

while running:

# Handle events

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

elif event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

spaceship_x -= 5

elif event.key == pygame.K_RIGHT:

spaceship_x += 5

elif event.key == pygame.K_SPACE:

if bullet_y < 0:

bullet_x = spaceship_x + 20

bullet_y = spaceship_y


# Move the objects

rock_y += rock_speed

if rock_y > screen_height:

rock_x = random.randint(0, screen_width)

rock_y = -100

rock_img = random.choice(rock_imgs)

rock_speed = random.randint(1, 3)

if bullet_y >= 0:

bullet_y -= 5

if bullet_y < 0:

bullet_x = -1


# Draw the objects

screen.fill((0, 0, 0))

screen.blit(spaceship_img, (spaceship_x, spaceship_y))

if bullet_y >= 0:

screen.blit(bullet_img, (bullet_x, bullet_y))

screen.blit(rock_img, (rock_x, rock_y))


# Check for collisions

if bullet_y >= 0 and rock_y >= 0:

bullet_rect = pygame.Rect(bullet_x, bullet_y, 10, 20)

rock_rect = pygame.Rect(rock_x, rock_y, 50, 50)

if bullet_rect.colliderect(rock_rect):

rock_x = random.randint(0, screen_width)

rock_y = -100

rock_img = random.choice(rock_imgs)

rock_speed = random.randint(1, 3)

bullet_x = -1

bullet_y = -1


# Update the display

pygame.display.update()


# Limit the frame rate

clock.tick(60)


# Clean up

pygame.quit()


 



Comentarios


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page