from typing import Any
import pygame
import random
pygame.init()
FPS = 60
WIDTH = 500
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED=(255,0,0)
YELLOW=(255,255,0)
BLACK=(0,0,0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption('第一個遊戲')
class Player(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.image = pygame.Surface((50, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom=HEIGHT - 10
self.speed=8
def update(self):
key_pressed=pygame.key.get_pressed()
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_LEFT]:
self.rect.x -=self.speed
if self.rect.right>WIDTH:
self.rect.right=WIDTH
if self.rect.left<0:
self.rect.left=0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
class Rock(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x=random.randrange(0,WIDTH-self.rect.width)
self.rect.y=random.randrange(-100,-40)
self.speedy=random.randrange(2,10)
self.speedx=random.randrange(-3,3)
def update(self):
self.rect.y += self.speedy
self.rect.x += self.speedx
if self.rect.top>HEIGHT or self.rect.left>WIDTH or self.rect.right<0:
self.rect.x=random.randrange(0,WIDTH-self.rect.width)
self.rect.y=random.randrange(-100,-40)
self.speedy=random.randrange(2,10)
self.speedx=random.randrange(-3,3)
class Bullet(pygame.sprite.Sprite):
def init(self, x, y):
pygame.sprite.Sprite.init(self)
self.image = pygame.Surface((10, 20))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
rock=Rock()
all_sprites.add(rock)
for i in range(8):
r=Rock()
all_sprites.add(r)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.update()
pygame.quit()
from typing import Any
import pygame
import random
pygame.init()
FPS = 60
WIDTH = 500
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED=(255,0,0)
YELLOW=(255,255,0)
BLACK=(0,0,0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption('第一個遊戲')
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom=HEIGHT - 10
self.speed=8
def update(self):
key_pressed=pygame.key.get_pressed()
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_LEFT]:
self.rect.x -=self.speed
if self.rect.right>WIDTH:
self.rect.right=WIDTH
if self.rect.left<0:
self.rect.left=0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
class Rock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x=random.randrange(0,WIDTH-self.rect.width)
self.rect.y=random.randrange(-100,-40)
self.speedy=random.randrange(2,10)
self.speedx=random.randrange(-3,3)
def update(self):
self.rect.y += self.speedy
self.rect.x += self.speedx
if self.rect.top>HEIGHT or self.rect.left>WIDTH or self.rect.right<0:
self.rect.x=random.randrange(0,WIDTH-self.rect.width)
self.rect.y=random.randrange(-100,-40)
self.speedy=random.randrange(2,10)
self.speedx=random.randrange(-3,3)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
rock=Rock()
all_sprites.add(rock)
for i in range(8):
r=Rock()
all_sprites.add(r)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.update()
pygame.quit()
a88 請直接編輯你發的文...
結果不知道為何改成按down鍵子彈就射出來了
chatGPT 修正後程式如下
from typing import Any
import pygame
import random
pygame.init()
FPS = 60
WIDTH = 500
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption('第一個遊戲')
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50, 40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speed = 8
def update(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_RIGHT]:
self.rect.x += self.speed
if key_pressed[pygame.K_LEFT]:
self.rect.x -= self.speed
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
class Rock(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((30, 40))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(2, 10)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.y += self.speedy
self.rect.x += self.speedx
if self.rect.top > HEIGHT or self.rect.left > WIDTH or self.rect.right < 0:
self.rect.x = random.randrange(0, WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(2, 10)
self.speedx = random.randrange(-3, 3)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((10, 20))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
rock = Rock()
all_sprites.add(rock)
for i in range(8):
r = Rock()
all_sprites.add(r)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.update()
pygame.quit()
修正主要涉及以下内容:
在 Player 类的 init 方法中,使用 super().init() 调用父类的构造函数。
在 Rock 类的 init 方法中,使用 super().init() 调用父类的构造函数。
在 Bullet 类的 init 方法中,使用 super().init() 调用父类的构造函数。
创建 all_sprites 组时,需要将 all_sprites = pygame.sprite.Group() 放在 Player、Rock 和 Bullet 实例创建之后,以确保所有精灵对象都被添加到组中。通过这些修正,黄色的子弹应该能够正常射出。