我不能改变我班在皮格姆的形状

2024-04-26 11:47:08 发布

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

所以我不能把玩家角色的形状从矩形改成圆形。之前它说有一个参数丢失,所以我们添加了它,然后它说有一个太多。现在它说了一些不同的东西,我已经不记得了,请任何人查看提供的代码。你知道吗

谢谢你的帮助!你知道吗

import pygame    
import turtle    
import time    
import math    
import random    
import sys    
import os    
pygame.init()    


WHITE = (255,255,255)    
GREEN = (0,255,0)    
BGColor = (117,168,55)    
RED = (255,0,0)    
BLUE = (0,0,255)    
BLACK = (0,0,0)    
MOVE = 2.5    


size = (1200, 620)    
screen = pygame.display.set_mode(size)    
pygame.display.set_caption("Zombie Game")    


class Char(pygame.sprite.Sprite):    
    def __init__(self, color, pos, radius, width):    
        super().__init__()    
        self.image = pygame.Surface([radius, width])    
        self.image.fill(WHITE)    
        self.image.set_colorkey(WHITE)    
        pygame.draw.circle(self.image, color, [0, 0], radius, width)    
        self.circle = self.image.get_circle()    


    def moveRight(self, pixels):    
        self.rect.x += pixels    

    def moveLeft(self, pixels):    
        self.rect.x -= pixels    

    def moveUp(self, pixels):    
        self.rect.y -= pixels    

    def moveDown(self, pixels):    
        self.rect.y += pixels    


all_sprites_list = pygame.sprite.Group()    

playerChar = Char(BLUE, [0, 0], 30, 0)    
playerChar.rect.x = 0    
playerChar.rect.y = 0    

all_sprites_list.add(playerChar)    

carryOn = True    
clock = pygame.time.Clock()    

while carryOn:    
    for event in pygame.event.get():    
        if event.type==pygame.QUIT:    
            carryOn=False    
        elif event.type==pygame.KEYDOWN:    
            if event.key==pygame.K_x:    
                carryOn=False    

    keys = pygame.key.get_pressed()    
    if keys[pygame.K_a]:    
        playerChar.moveLeft(MOVE)    
    if keys[pygame.K_d]:    
        playerChar.moveRight(MOVE)    
    if keys[pygame.K_w]:    
        playerChar.moveUp(MOVE)    
    if keys[pygame.K_s]:    
        playerChar.moveDown(MOVE)    

    screen.fill(BGColor)    
    pygame.display.flip()    
    clock.tick(60)    
pygame.quit()    

Tags: rectimageimportselfeventmoveifinit
1条回答
网友
1楼 · 发布于 2024-04-26 11:47:08

如果要绘制半径为曲面的圆,则不必创建半径宽度和高度加倍的曲面:

self.image = pygame.Surface([radius*2, radius*2]) 

要使类继续工作,仍然必须设置mebmerself.rect

self.rect = self.image.get_rect()

最后,将^{}面转换为screen面:

screen.blit(playerChar.image,playerChar.rect)

请参见示例,我将建议应用于原始代码:

import pygame    
import turtle    
import time    
import math    
import random    
import sys    
import os    
pygame.init()    

WHITE = (255,255,255)    
GREEN = (0,255,0)    
BGColor = (117,168,55)    
RED = (255,0,0)    
BLUE = (0,0,255)    
BLACK = (0,0,0)    
MOVE = 2.5    

size = (1200, 620)    
screen = pygame.display.set_mode(size)    
pygame.display.set_caption("Zombie Game")    

class Char(pygame.sprite.Sprite):    
    def __init__(self, color, pos, radius, width):    
        super().__init__()    
        self.image = pygame.Surface([radius*2, radius*2])    
        self.image.fill(WHITE)    
        self.image.set_colorkey(WHITE)    
        pygame.draw.circle(self.image, color, [radius, radius], radius, width)   
        self.rect = self.image.get_rect()    

    def moveRight(self, pixels):    
        self.rect.x += pixels
        pass    

    def moveLeft(self, pixels):    
        self.rect.x -= pixels
        pass    

    def moveUp(self, pixels):    
        self.rect.y -= pixels
        pass    

    def moveDown(self, pixels):    
        self.rect.y += pixels
        pass    


all_sprites_list = pygame.sprite.Group()    

playerChar = Char(BLUE, [0, 0], 30, 0)    
playerChar.rect.x = 0    
playerChar.rect.y = 0    

all_sprites_list.add(playerChar)    

carryOn = True    
clock = pygame.time.Clock()    

while carryOn:    
    for event in pygame.event.get():    
        if event.type==pygame.QUIT:    
            carryOn=False    
        elif event.type==pygame.KEYDOWN:    
            if event.key==pygame.K_x:    
                carryOn=False    

    keys = pygame.key.get_pressed()    
    if keys[pygame.K_a]:    
        playerChar.moveLeft(MOVE)    
    if keys[pygame.K_d]:    
        playerChar.moveRight(MOVE)    
    if keys[pygame.K_w]:    
        playerChar.moveUp(MOVE)    
    if keys[pygame.K_s]:    
        playerChar.moveDown(MOVE)    

    screen.fill(BGColor) 
    screen.blit(playerChar.image,playerChar.rect)
    pygame.display.flip()    
    clock.tick(60)    
pygame.quit()    

相关问题 更多 >