iT邦幫忙

0

黃色子彈射不出來

a88 2023-07-12 16:20:142437 瀏覽
  • 分享至 

  • xImage

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()

看更多先前的討論...收起先前的討論...
win895564 iT邦研究生 5 級 ‧ 2023-07-12 16:25:13 檢舉
光你的排版 我連pygame主程式都射不出來
望空 iT邦新手 1 級 ‧ 2023-07-13 09:45:48 檢舉
不是,你射不出來要去找專業的,不是隨便點邀請回答阿
我懷疑望空大在開車,可是我沒有證據XD。
望空 iT邦新手 1 級 ‧ 2023-07-13 14:40:59 檢舉
你看他問的問題啊 這種就算不想開車還是會被塞車鑰匙的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

6
尼克
iT邦大師 1 級 ‧ 2023-07-12 17:01:37

https://ithelp.ithome.com.tw/upload/images/20230712/20011825jPjMsHKong.png

看更多先前的回應...收起先前的回應...
iT邦新手 3 級 ‧ 2023-07-12 17:04:25 檢舉

這個答案差點讓我鍵盤洗澡XD

a88 iT邦新手 5 級 ‧ 2023-07-12 17:05:01 檢舉
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()
Hans陳 iT邦新手 5 級 ‧ 2023-07-13 05:40:52 檢舉

a88 請直接編輯你發的文...

a88 iT邦新手 5 級 ‧ 2023-07-13 13:15:53 檢舉

結果不知道為何改成按down鍵子彈就射出來了

0
海綿寶寶
iT邦大神 1 級 ‧ 2023-07-13 08:28:04

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 实例创建之后,以确保所有精灵对象都被添加到组中。

通过这些修正,黄色的子弹应该能够正常射出。

菩薩慈悲:還好您不是在StackOverflow,否則敏感字眼一定會被關愛的眼神煞到的。/images/emoticon/emoticon08.gif 感恩感恩 讚歎讚歎 南無阿彌陀佛

我要發表回答

立即登入回答