我无法在PyGame中更改班级形状

时间:2019-02-14 21:39:02

标签: python pygame geometry

所以我不能将玩家角色的形状从矩形更改为圆形。之前它说缺少一个论点,所以我们加了它,然后说有太多论点。现在它说了些不同的内容,我不记得了,请问任何机构可以仔细查看提供的代码。

感谢您的帮助!

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()    

1 个答案:

答案 0 :(得分:1)

如果要在表面上绘制半径为半径的圆,则不能使用半径的宽度和高度加倍来创建表面:

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

要保持该类正常运行,您仍然必须设置Mebmer self.rect

self.rect = self.image.get_rect()

最后,表面blitscreen表面:

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()