1

我有一个来自 pygame 教程的程序:

import sys, pygame
pygame.init()

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit();
        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1] 

        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()

这是一个简单的弹跳球。我想要的是用一个圆圈替换球(ball.gif),该圆圈将使用 pygame 绘制,而无需对代码进行重大更改。我的意思是,我唯一想要改变的是球,用一个圆圈替换它,并用我用来为球设置动画的相同代码为那个圆圈设置动画。有什么办法可以做到吗?谢谢。

4

3 回答 3

3

我知道这很旧,但我很难弄清楚,但我终于做到了。

import pygame
pygame.init()

window_w = 800
window_h = 600

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

FPS = 120

window = pygame.display.set_mode((window_w, window_h))
pygame.display.set_caption("Game: ")
clock = pygame.time.Clock()


def game_loop():

    block_size = 20

    velocity = [1, 1]

    pos_x = window_w/2
    pos_y = window_h/2

    running = True

    while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        pos_x += velocity[0]
        pos_y += velocity[1]

        if pos_x + block_size > window_w or pos_x < 0:
            velocity[0] = -velocity[0]

        if pos_y + block_size > window_h or pos_y < 0:
            velocity[1] = -velocity[1]

        # DRAW
        window.fill(white)
        pygame.draw.rect(window, black, [pos_x, pos_y, block_size, block_size])
        pygame.display.update()
        clock.tick(FPS)


game_loop()
于 2017-06-12T03:16:51.917 回答
2

你只需要更换

screen.blit(ball, ballrect)

pygame.draw.circle(screen, color, ballrect.center, radius)

Pygame 的绘图模块也允许绘制其他形状。在上面的代码中,您可以填写colorradius使用您想要的值。 color是一个包含 0-255 范围内的三个整数的元组。

于 2013-10-15T01:37:33.043 回答
0

也许这就是您正在寻找的。从这里下载图像。

import pygame
import sys

WIDTH, HEIGHT = 600,600
ROWS, COLS = 8,8
SQUARE_SIZE = WIDTH//COLS
WIDTH, HEIGHT = SQUARE_SIZE*COLS, SQUARE_SIZE*ROWS
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
CLOCK = pygame.time.Clock()

class Ball:
    GRAVITY = 0.5
    def __init__(self, row, col):
        self.row = row
        self.col = col
        self.target_x = (SQUARE_SIZE*col)+SQUARE_SIZE/2
        self.target_y = (SQUARE_SIZE*row)+SQUARE_SIZE/2
        self.x = self.target_x
        self.y = 0
        self.vel = 10
        self.img = pygame.transform.scale(pygame.image.load("ball2.png"), (SQUARE_SIZE-20, SQUARE_SIZE-20))
        self.landed = False

    def draw(self):
        self.update()
        pygame.draw.circle(WIN, (0,255,0), (self.x, self.y), 15)
    
    def update(self):
        if not self.landed:
            self.vel += self.GRAVITY
            self.y += self.vel
            if self.y > self.target_y:
                self.vel = -(self.vel*0.70)
                if (abs(self.vel)<1):
                    self.landed = True
                self.y += self.vel


def draw_board(ball_list):
    WIN.fill((0,0,0))
    for row in range(ROWS):
        for col in range(row%2, COLS, 2):
            pygame.draw.rect(WIN, (255,0,0), (row*SQUARE_SIZE, col*SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))

def create_balls(ball_list):
    x = []
    for row in range(ROWS):
        for col in range(COLS):
            if ball_list[row][col] ==1: x.append(Ball(row, col))
    return x
    
def main():
    ball_list = [
        [0,0,0,1,0,0,0,1],
        [1,0,1,0,0,0,0,1],
        [0,0,0,1,0,1,0,0],
        [0,0,1,1,0,0,0,1],
        [1,0,0,0,0,1,0,0],
        [0,0,0,0,1,0,0,1],
        [0,1,0,0,0,1,0,0],
        [0,0,1,0,0,0,1,0],
    ]
    balls = create_balls(ball_list)

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


        draw_board(ball_list)
        for ball in balls:
            ball.draw()
        pygame.display.update()
        CLOCK.tick(60)

main()
于 2021-08-04T12:15:56.670 回答