1 回答
TA贡献1801条经验 获得超8个赞
如果要绘制一个半径为曲面的圆,则必须创建一个半径为两倍宽度和高度的曲面:
self.image = pygame.Surface([radius*2, radius*2])
为了让班级正常工作,您仍然必须设置 mebmer self.rect
:
self.rect = self.image.get_rect()
最后是表面blit
到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()
添加回答
举报