Pygame球随时间增大

2024-09-29 18:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用pygame做一个小游戏。我不知道如何使球随着时间的推移而增加,并且使球的繁殖速度增加。 它将是这样的: https://www.youtube.com/watch?v=DCQp1Q8ANCM (0:31)

#ball.py
import pygame
import random
YELLOW = (225, 225, 0)

class balls:
    def draw_ball(screen, tickrate, i):
        x = random.randint(0,500)
        y = random.randint(0,500)
        first_range = 10
        range = 10

        print(i)
        if i >= 100 :
            ball = pygame.draw.circle(screen, YELLOW, (x, y), range)
#Main.py
import pygame
import os
import random
import ball
import threading
import sys
from timeit import Timer

pygame.init()
game_screen = pygame.display.set_mode((800, 600))
x = 100
y = 100
os.environ['Sp_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
size = [500, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Reaction")
background = pygame.image.load("images\\background.jpg")
background_rect = background.get_rect(bottomright = (500,500))
background.set_colorkey((255,255,255)) #прозрачный слой фона
screen.blit(background,background_rect)
pygame.display.update()
run_game = True #флаг игрового цикла
clock = pygame.time.Clock()
FPS = 60
starttime=pygame.time.get_ticks()
#timer = Timer(0.05, ball.balls.draw_ball(screen)) # 50 миллисекунд
i = 1

def quit():
            rungame = False
            pygame.quit()
            sys.exit()

while run_game: #игровой цикл
    #timer.start()
    tickrate = clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
    ball.balls.draw_ball(screen,tickrate,i)
    pygame.display.update()
    if i == 100:
        i = 0
    i+=1
pygame.display.flip()

Tags: rectimportgameifdisplayrangerandomscreen
3条回答

您没有提供任何代码,因此对任何想要回答您的问题的人几乎没有帮助

我假设你有一个像这样的球对象:

class Ball:
    def __init__(self, radius):
        self.radius = radius

您的pygame while循环可以如下所示

ball = Ball(some_radius)
while 1:
    # Do pygame stuff

    ball.radius += some_number
    pygame.draw.circle(screen, color, pos, ball.radius)

当然,这是假设您对某些半径、某些编号、屏幕、颜色和位置具有适当的值

下面是我要做的:

首先,使用变量x、y和size定义一个类:

class GreenBall:               # Your class
    def __init__(self, x, y, size):  # The function that creates a ball at a given x and y position
        self.x = x
        self.y = y 
        self.size = size

    def grow():                   # Every time this function is called, your ball will grow
        self.size += 1

    def render():
        pygame.draw.circle(gameSurface, (0,255,0), (self.x,self.y), size)


每次要创建新的绿球时,都可以调用如下函数:

ballList = []

def newBall(x, y):       # Initialize a new greenBall at x and y
    global ballList
    ballList.append( GreenBall( x, y, 1 ) )

现在,要绘制每个刻度的圆,我们需要让它们自己渲染

for i in ballList:
    i.render()

希望这些代码块能有所帮助


编辑: 在胡闹了一会儿之后,我想出了这个计划:

import pygame
import random
import math
import sys

pygame.init()

clock = pygame.time.Clock()

gameSize = (1200,800)

gameSurface = pygame.display.set_mode(gameSize)
pygame.display.set_caption('Green Balls!')



class GreenBall:               # Your class
    def __init__(self, x, y, size):  # The function that creates a ball at a given x and y position
        self.x = x
        self.y = y 
        self.size = size

    def grow(self):        # Every time this function is called, your ball will grow
        self.size += 1

    def render(self):
        pygame.draw.circle(gameSurface, (0,255,0), (self.x,self.y), (self.size))



ballList = []

def newBall(x, y):       # Initialize a new greenBall at x and y
    global ballList
    ballList.append( GreenBall( x, y, 1 ))





timer = 0

spawnBallTime = 30

while True:      #game loop
    gameSurface.fill((0,0,0))

    for i in ballList:
        i.grow()
        i.render()

    if timer == spawnBallTime:
        newBallX = random.randint(100, gameSize[0]-100)
        newBallY = random.randint(100, gameSize[1]-100)

        newBall(newBallX, newBallY)

        spawnBallTime -= 1
        timer = 0

    timer += 1

    clock.tick(20)

    pygame.display.update()

    event = pygame.event.get()
    for e in event:
        if e.type == pygame.QUIT:
            pygame.display.quit()
            sys.exit()

我不想破坏你编写有趣游戏的乐趣,但这里是我创建的一个基本设置。请随时向我提问,这意味着什么:D

创建一个类Ball。该类有3个属性。位置(self.xself.y)和当前大小(self.size)。该调用必须调用方法change_size更改球的大小并draw_ball绘制球:

class Ball:
    def __init__(self):
        self.x = random.randint(0,500)
        self.y = random.randint(0,500)
        self.size = 1

    def change_size(self):
        self.size += 1
        if self.size > 100:
            self.size = 1

    def draw_ball(self, screen):
        pygame.draw.circle(screen, YELLOW, (self.x, self.y), self.size)

创建Ball的实例,更改球的大小并在应用程序循环中绘制球:

ball = Ball()
run_game = True
while run_game:

    tickrate = clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

    screen.blit(background,background_rect)
    ball.change_size()
    ball.draw_ball(screen)
    pygame.display.update()

相关问题 更多 >

    热门问题