复制具有相同功能的精灵

Duplicating sprites with the same function

我一直在制作一个 pygame 游戏,其中 2 名玩家尝试将球击入球网(以下代码的简化版本)。我添加了助推垫,当您收集助推垫时,助推秒数会增加。我想要在不同位置的地图周围有几个,但不需要多次复制和粘贴代码。我之前的一个问题 () 展示了如何使用 self,但同时使用 classes。我对 python 不是很有经验,也没有教自己如何做一些(对我来说)不太有趣的事情,比如如何正确使用 类。我希望有人复制 boostpad,同时保持代码与现在相同的格式(没有 类 因为我在 python :( ) 是垃圾并且没有多次复制和 psting 代码. 这可能吗?

import pygame
from pygame.math import Vector2
import time, datetime
import sys

pygame.font.init()
pygame.init()


WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()


REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    REDCAR_ORIGINAL, (255, 0, 0), [(0, 30), (50, 20), (50, 10), (0, 0)])
redangle = 180
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
redangle = 180
booster = pygame.Surface((50, 150), pygame.SRCALPHA)
booster.fill(pygame.color.Color("yellow"))
booster1 = booster.get_rect(topleft=(0, 340))
boostermask = pygame.mask.from_surface(booster)


redspeed = 5

dx = 0
dy = 0
x = 800
y = 500
redscore = 0
boostpadtimer = 3


pos_red = Vector2(x,y)
vel_red = Vector2(-redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
pos_blue = Vector2(275,300)


# Masks.
mask_red = pygame.mask.from_surface(redcar)


def redboosts():
    global vel_red
    global timer
    global dt
    if timer > 0:
        vel_red.scale_to_length(10)
        timer -= dt



def slowdown():
    vel_red.scale_to_length(5)






timer = 3
dt = 1
boost_recieved = False
timerr = False









run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
          if event.key == pygame.K_r:
              # Reset the car to the start position.
              redangle = 180
              redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
              pos_red = Vector2(800, 500)
              vel_red = Vector2(-redspeed, 0)
              redrect = redcar.get_rect(center=pos_red)
              mask_red = pygame.mask.from_surface(redcar)
          #elif event.key == pygame.K_s:
          #    vel_blue.scale_to_length(2)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                vel_red.scale_to_length(5)
            elif event.key == pygame.K_DOWN:
              vel_red.scale_to_length(5)


    #print(boostpadtimer)
    print(timer)

    if boost_recieved:
        if boostpadtimer == 3:
            timer += 3
            booster.fill(pygame.color.Color("grey"))
            boost_recieved = False
    if timerr:
        boostpadtimer -= dt

    if boostpadtimer <= 0:
        timerr = False
        boostpadtimer = 3

    if boostpadtimer == 3:
        booster.fill(pygame.color.Color("yellow"))




    #if timer < 0:
    #    timer = 3


    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
              redangle += 5
              vel_red.rotate_ip(-5)
              redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
              redrect = redcar.get_rect(center=redrect.center)
              # We need a new mask after the rotation.
              mask_red = pygame.mask.from_surface(redcar)
    elif keys[pygame.K_RIGHT]:
              redangle -= 5
              vel_red.rotate_ip(5)
              redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
              redrect = redcar.get_rect(center=redrect.center)
              mask_red = pygame.mask.from_surface(redcar)


    if keys[pygame.K_UP]:
              redboosts()
    if keys[pygame.K_DOWN]:
              vel_red.scale_to_length(2)
    if keys[pygame.K_s]:
              vel_blue.scale_to_length(2)

    if y <0:
        y = 10
    if y > 450:
        y = 440
    if x > 480:
        x = 470


    if redrect.top < 0 and vel_red.y < 0:
        redrect.top = 0
        pos_red = Vector2(redrect.center)
    elif redrect.bottom > screen.get_height() and vel_red.y > 0:
        redrect.bottom = screen.get_height()
        pos_red = Vector2(redrect.center)
    if redrect.left < 0 and vel_red.x < 0:
        redrect.left = 0
        pos_red = Vector2(redrect.center)
    elif redrect.right > screen.get_width() and vel_red.x > 0:
        redrect.right = screen.get_width()
        pos_red = Vector2(redrect.center)


    pos_red += vel_red
    redrect.center = pos_red


    offset = booster1[0] - redrect[0], booster1[1] - redrect[1]
    collectboost = mask_red.overlap(boostermask, offset)

    if timer <= 0:
        timer = 0
        vel_red.scale_to_length(5)
        slowdown()

    if collectboost:
        boost_recieved = True
        timerr = True


    screen.fill((50,200,50))
    screen.blit(redcar, redrect)
    screen.blit(booster, booster1)


    pygame.display.flip()
    dt = clock.tick(60)/1000
pygame.quit()

回答你的问题:如果不使用 类,我认为你想要的是不可能的。

它们是实现诸如增强板之类的东西的好方法。

简单示例:

class BoostPad(pygame.Surface):

    def __init__(color='red',time_value=3,x_size=0,y_size=0,size_or_position=(0,0)):
        self.color = color
        self.time_value=time_value
        self.time = time_value

        super(pygame.Surface, self).__init__((x_size, y_size, pygame.SRCALPHA)
        self.fill(pygame.color.Color(color))
        # Not sure if this is a size or position
        self.size_or_position = self.get_rect(topleft=size_or_position)
        self.mask = pygame.mask.from_surface(booster)


    def set_color(color):
        self.fill(pygame.color.Color(color))


    def boost_triggered(self):
        if self.time is self.time_value:
            self.time = 0
            self.set_color('grey')
            return self.time_value


    def add_time(self, dt):
        self.time += dt
        if self.time is self.time_value:
            self.set_color(self.color)

等等等等

我不完全确定我能弄清楚你的游戏是如何运行的,因为我从未使用过pygame但应用程序应该是相似的。