0

我正在阅读这本关于 Python 的书,名为“为绝对初学者准备的更多 Python 编程”,在第 4 章中,您将创建这个炸弹捕捉游戏。我正在尝试修改它,以便您向炸弹或敌人射击。

我希望能够使用

pygame.draw.circle()

所以当你点击鼠标右键时它会射击,然后它会击中敌人并增加你的分数。顺便说一句 - 我已经知道如何在我只需要帮助拍摄形状时添加分数 :) 如果您想查看原始游戏源文件,请访问 - http://www.delmarlearning.com/ partners/content/1435459806/relatedfiles/index.asp?isbn=1435459806 它在第 4 章“BombCatcher”中

原始源代码-

# Bomb Catcher Game
# Chapter 4

import sys, random, time, pygame
from pygame.locals import *

def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))


#main program begins
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Bomb Catching Game")
font1 = pygame.font.Font(None, 24)
pygame.mouse.set_visible(False)
white = 255,255,255
red = 220, 50, 50
yellow = 230,230,50
black = 0,0,0

lives = 3
score = 0
clock_start = 0
game_over = True
mouse_x = mouse_y = 0

pos_x = 300
pos_y = 460

bomb_x = random.randint(0,500)
bomb_y = -50
vel_y = 0.7

#repeating loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:
            mouse_x,mouse_y = event.pos
            move_x,move_y = event.rel
        elif event.type == MOUSEBUTTONUP:
            if game_over:
                game_over = False
                lives = 3
                score = 0

    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

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

    if game_over:
        print_text(font1, 100, 200, "<CLICK TO PLAY>") 
    else:
        #move the bomb
        bomb_y += vel_y

        #has the player missed the bomb?
        if bomb_y > 500:
            bomb_x = random.randint(0, 500)
            bomb_y = -50
            lives -= 1
            if lives == 0:
                game_over = True

        #see if player has caught the bomb
        elif bomb_y > pos_y:
            if bomb_x > pos_x and bomb_x < pos_x + 120:
                score += 10
                bomb_x = random.randint(0, 500)
                bomb_y = -50

        #draw the bomb
        pygame.draw.circle(screen, black, (bomb_x-4,int(bomb_y)-4), 30, 0)
        pygame.draw.circle(screen, yellow, (bomb_x,int(bomb_y)), 30, 0)

        #set basket position
        pos_x = mouse_x
        if pos_x < 0:
            pos_x = 0
        elif pos_x > 500:
            pos_x = 500
        #draw basket
        pygame.draw.rect(screen, black, (pos_x-4,pos_y-4,120,40), 0)
        pygame.draw.rect(screen, red, (pos_x,pos_y,120,40), 0)

    #print # of lives
    print_text(font1, 0, 0, "LIVES: " + str(lives))

    #print score
    print_text(font1, 500, 0, "SCORE: " + str(score))

    pygame.display.update()
4

1 回答 1

0

要检查一个按钮是否被按下,你应该检查一个event.type : MOUSEBUTTONDOWN,然后检查人民币。

为了射击,您需要某种列表,以便可能有很多子弹。您可以创建一个简单的 Bullet 类,该类将具有绘制和移动功能。一个例子:

class Bullet:
    def __init__(self,position):
        self.x,self.y = position
    def move(self):
        self.position += 5
    def draw(self,screen):
        pygame.draw.circle(screen, black, x, y, 30, 0)

这涵盖了子弹的绘图和运动。除此之外,您应该检查项目符号是否不会离开屏幕,如果确实如此,请将其从列表中删除。

碰撞可以通过多种方式完成。Pygame 有一个名为 collide_rect 的内置方法,它检查 2 个矩形是否重叠。但由于你有 2 个圆圈,所以方法有点不同。

由于您有 2 个圆圈,因此它们之间存在距离。它由这个等式给出:

D = sqrt((xa-xb)^2+(ya-yb)^2))

如果距离小于R1 + R2,则圆圈重叠。然后,您可以继续移除子弹和炸弹。

我们可以将其简化为:

(R1+R2)^2 > (xa-xb)^2+(ya-yb)^2)

于 2013-06-05T22:06:13.597 回答