You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pygame中随机选择并部署三种类型的敌方矩形

问题:实现Chrome恐龙式游戏的随机敌方块部署

我正在使用Python制作一款与Chrome恐龙游戏类似的游戏,将仙人掌替换为矩形敌方块。目前已实现玩家跳跃和敌方块移动功能,拥有normal(普通)、long(长型)、flying(飞行)三种敌方块类型,希望编写函数实现当前敌方块移动至屏幕一半位置时,随机选择三种类型之一进行部署。以下是我的代码,我已在move_enemy函数和random_enemy函数中尝试实现该功能:

import pygame
import sys
import random

pygame.init()

# measurement for the display screen
screen_width = 800
screen_height = 600


# colour variables
Red = (255,0,0)
Black = (0,0,0)
Blue = (0,0,255)
Cyan = (0,255,255)
White = (255,255,255)
Green = (0,255,0)

# main screen
screen = pygame.display.set_mode((screen_width,screen_height))
screen.fill(White)

# player variables
player_size = 50
player_x = 100
player_y = 510
stop_vel = 0
init_vel = 5
isjump = False
jumpcount = 12
can_jump = False
grounded = True

# enemy variables

enemy_x = screen_width + 10
enemy_y = 510
enemy_halt = True
enemy_pos = 0


Clock = pygame.time.Clock()
FPS = 60

pygame.display.set_caption("trials")

myfont = pygame.font.SysFont("timesnewroman",40)
welcome = myfont.render("This game was made by me",True,Green)
message = myfont.render("Press space to start",True,Green)

wellen = welcome.get_width()
meslen = message.get_width()


def welcome_screen(welcome,message):
    screen.blit(welcome,((screen_width-wellen)/2,100))
    screen.blit(message,((screen_width-meslen)/2,200))



def draw_normie(enemy_x,enemy_y):
    pygame.draw.rect(screen,Blue,(enemy_x, enemy_y,40,40))
def draw_longee(enemy_x,enemy_y):
    pygame.draw.rect(screen,Blue,(enemy_x, enemy_y,70,40))
def draw_air(enemy_x,enemy_y):
    pygame.draw.rect(screen,Blue,(enemy_x, enemy_y,40,20))


# main class consisting of most functions
class Mario():
    global screen_width,player_size,enemy_halt,init_vel,isjump,can_jump,stop_vel
    def __init__(self,x,y,enem_x,enem_y):
        self.x = x
        self.y = y
        self.e_x = enem_x
        self.e_y = enem_y
        self.offset = screen_width/2
        self.en_x = screen_width + 10
        self.ene_x = screen_width + 10
        self.ene_y = 500
        self.enemy_list = [1,2,3]
        self.deploy = False
        self.vel = 0
        self.choice = random.choice(self.enemy_list)
        self.normee = pygame.draw.rect(screen,Blue,(self.e_x, self.e_y,40,40))
        self.longee = pygame.draw.rect(screen,Blue,(self.en_x, self.e_y,65,40))
        self.air = pygame.draw.rect(screen,Blue,(self.ene_x, self.ene_y,40,20))
    
    def quiting(self):
        pygame.quit()
        sys.exit()
    
    def draw_player(self):
        pygame.draw.rect(screen,(255,0,0),(self.x,self.y,40,40))
    def draw_enemy(self):
        draw_normie(self.e_x,self.e_y)
        draw_longee(self.en_x,self.e_y)
        draw_air(self.ene_x,self.ene_y)

    def move_enemy(self):
        if enemy_halt == False:
            if self.choice == self.enemy_list[0]:
                self.e_x -= init_vel
                # a.random_enemy()
                if self.choice == self.enemy_list[1]:
                    self.en_x -= init_vel
                if self.choice == self.enemy_list[2]:
                    self.ene_x -= init_vel
            if self.choice == self.enemy_list[1]:
                self.en_x -= init_vel
            
    
    def deploy_enemy(self):
        if (screen_width > self.e_x and self.e_x<= self.offset) or (screen_width > self.en_x and self.en_x <= self.offset) or (screen_width > self.ene_x and self.ene_x <= self.offset):
            self.deploy = True


    def random_enemy(self):
        if self.deploy == True:
            if self.choice == self.enemy_list[0]:
                draw_normie(self.e_x,510)
                # self.choice = 0
                self.deploy = False
            elif self.choice == self.enemy_list[1]:
                draw_longee(self.en_x,510)
                # self.choice = 0
                self.deploy = False
            elif self.choice == self.enemy_list[2]:
                draw_air(self.ene_x,500)
                # self.choice = 0
                self.deploy = False
             



    def reset_pos(self):
        if self.e_x <= -40:
            self.e_x = screen_width/2
        if self.en_x <= -70:
            self.en_x = screen_width//3
        if self.ene_x <= -20:
            self.ene_x = screen_width - 100

    
    def move(self):
        
        if K[pygame.K_RIGHT]:
            if self.x>=100 and self.x<(screen_width-2*player_size):
                self.x+=5
        if K[pygame.K_LEFT]:
            if self.x>100 and self.x<=(screen_width-player_size*2):
                self.x-=5


    def jump(self):
        global jumpcount,can_jump,isjump
        if isjump == True:
            if jumpcount >= -12:
                neg = 1
                if jumpcount < 0:
                    neg = -1
                self.y -= (jumpcount**2) * 0.1 * neg
                jumpcount -= 1
            else:
                isjump = False
                jumpcount = 12
    def collision_check(self):
        pass

a = Mario(player_x,player_y,enemy_x,enemy_y)

play_game = True
while play_game:
    screen.fill(White)
    K = pygame.key.get_pressed()
    
    welcome_screen(welcome,message)
    a.move()
    a.jump()
    
    
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            a.quiting()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                enemy_halt = False
                isjump = True
    a.draw_enemy()
    a.move_enemy()
    a.deploy_enemy()
    a.random_enemy()
    # a.reset_pos() 
 
    a.draw_player()
    a.draw_enemy()
    
    pygame.draw.line(screen,Black,(0,(screen_height-player_size)),(screen_width,(screen_height-player_size)),5)
    pygame.display.update()
    Clock.tick(FPS)

内容的提问来源于stack exchange,提问作者Logan21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 09:55:24