python和pygame射击

时间:2013-11-10 10:12:22

标签: python class pygame

我正在使用pygame进行游戏,到目前为止玩家可以四处走动,屏幕上随机产生50个蓝色块,玩家可以四处走动并射击它们,但玩家只能射击一个问题,我希望玩家向鼠标射击但是在执行此操作时遇到了一些麻烦。

这是我的代码

import pygame
from pygame import *
import random


black = ( 0, 0, 0)
white = ( 255, 255, 255)
red = ( 255, 0, 0)
blue = ( 0, 0, 255)

player_x, player_y = 0, 0
move_player_x, move_player_y = 0, 0


class Block(pygame.sprite.Sprite):

    def __init__(self, color):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20, 15])
        self.image.fill(color)

        self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([20,20])
        self.image.fill(red)

        self.rect = self.image.get_rect()
    def update(self):

        pos = pygame.mouse.get_pos()


        self.rect.x = player_x
        self.rect.y = player_y

class Bullet(pygame.sprite.Sprite):

    def __init__(self):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([4, 10])
        self.image.fill(black)

        self.rect = self.image.get_rect()
    def update(self):



        self.rect.y -= 5


pygame.init()


screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width,screen_height])


all_sprites_list = pygame.sprite.Group()


block_list = pygame.sprite.Group()


bullet_list = pygame.sprite.Group()


for i in range(50):

    block = Block(blue)


    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(350)

    block_list.add(block)
    all_sprites_list.add(block)


player = Player()
all_sprites_list.add(player)


done = False


clock = pygame.time.Clock()

score = 0
player.rect.y = 370

# -------- Main Program Loop -----------
while not done:

    # --- Event Processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.MOUSEBUTTONDOWN:

            bullet = Bullet()

            bullet.rect.x = player.rect.x
            bullet.rect.y = player.rect.y

            all_sprites_list.add(bullet)
            bullet_list.add(bullet)

    if event.type== pygame.KEYDOWN:    
        if event.key==K_a:
                move_player_x=-1
        elif event.key==K_d:
                move_player_x=+1
        elif event.key==K_w:
                move_player_y=-1
        elif event.key==K_s:
                move_player_y=+1
    if event.type== pygame.KEYUP:
        if event.key==K_a:
                move_player_x=0
        elif event.key==K_d:
                move_player_x=0
        elif event.key==K_w:
                move_player_y=0
        elif event.key==K_s:
                move_player_y=0

    # --- Game logic




    all_sprites_list.update()

    player_x += move_player_x
    player_y += move_player_y



    for bullet in bullet_list:


        block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)


        for block in block_hit_list:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)
            score += 1
            print( score )


        if bullet.rect.y < -10:
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)




    screen.fill(white)


    all_sprites_list.draw(screen)


    pygame.display.flip()


    clock.tick(20)

pygame.quit()

所以有代码,非常感谢任何帮助

1 个答案:

答案 0 :(得分:3)

这是我提出的,我从RPG改变了一些代码,而朋友正在制作

将您的子弹类代码更改为:

class Bullet(pygame.sprite.Sprite):

    def __init__(self, mouse, player):

        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([4, 10])
        self.image.fill(black)

        self.mouse_x, self.mouse_y = mouse[0], mouse[1]
        self.player = player

        self.rect = self.image.get_rect()
    def update(self):

        speed = 4.
        range = 200
        distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
        norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
        direction = [distance[0] / norm, distance[1 ] / norm]
        bullet_vector = [direction[0] * speed, direction[1] * speed]


        self.rect.x -= bullet_vector[0]
        self.rect.y -= bullet_vector[1]

现在它需要玩家pos和鼠标点击pos作为类的参数

数学有点令人困惑,但它基本上从两点获取向量并获得方向并以此方式发送子弹

当您创建新的项目符号实例时,请按以下方式调用它:

 bullet = Bullet(pygame.mouse.get_pos(), [player.rect.x, player.rect.y])

当单击鼠标按钮时,类会获得两个点!

当你运行它时,你可能会注意到子弹的方向与鼠标点击相反,这会增加一些挑战。如果需要,您可以更改

P.S。 speed变量可以调整!哦,需要导入数学模块