小白求助!求助如何让pygame中的一个图形动起来
import randomimport pygame, sys
from pygame import *
from point类 import Point
pygame.init()
screen = pygame.display.set_mode((600,500))
#病患的患病程度数据监测
v_commonly_recover = -2
v_sick_beside = 0.5
v_get_worse = 1
#病患的三种健康程度颜色表示
healthy_green = 0,255,27
slightly_ill_yellow = 255,215,0
need_first_aid_red = 255,0,0
seriously_sick = set()#创建一个存储重病号位置的集合
#定义一个新的病人函数
class Patient():
def __init__(self, pos_x, pos_y,sick_percent = 65):
self.pos_bed = pos_x, pos_y, 90, 200 #这是画病床的位置
self.patient_vel_x = 0
self.patient_vel_y = 0
self.patient_pos_x = pos_x + 45
if pos_y < 200:
self.patient_pos_y = pos_y +18
self.patient_point =Point(self.patient_pos_x, self.patient_pos_y)
self.pos_patient = self.patient_pos_x, self.patient_pos_y #这是那个病人圆圈的位置
if pos_y > 200:
self.patient_pos_y = pos_y +200 -18
self.patient_point =Point(self.patient_pos_x, self.patient_pos_y)
self.pos_patient = self.patient_pos_x, self.patient_pos_y
self.sp = sick_percent
self.sp_flag = True
self.beside_sick_flag = False
self.x = pos_x + 95
self.y = pos_y + 5
self.pos_instruction = self.x, self.y, 5, 50#这里是在画那个指示疾病指数的条的外框
self.sick_beside_number = 0 #这个是监测旁边的病人是否为重症的标志
def patient_change(self):
if self.beside_sick_flag:
i_sp_change = random.randint(1,4)
else:
i_sp_change = random.randint(1,7)
if self.sp <= 10:
self.sp_flag = False #设置的意义是为了使得患者在病情指数达到10以下后,就出院,因此会长期保持绿色
if self.sp_flag andi_sp_change == 1: #这是随机病重控制
for times in range(1,6):
self.sp = self.sp + v_get_worse
self.draw_a_patient()
pygame.time.delay(100)
elif self.sp_flag:
self.sp = self.sp + v_commonly_recover #这是标准恢复速度
self.draw_a_patient()
pygame.time.delay(100)
pygame.draw.line(screen,(255,255,255),(self.x+2,self.y+48), (self.x+2,self.y+3),3) #这是在刷新框里的颜色,否则进度条只增不减
pygame.draw.line(screen, (176,196,222),(self.x+2,self.y+50), (self.x+2,self.y+50-(self.sp//2)),3) #这是在描绘动态的指示条
def draw_a_patient(self):
if self.sp <= 10:
color = healthy_green
pygame.draw.circle(screen, color, self.pos_patient, 15, 0)
elif 10 < self.sp < 70:
color = slightly_ill_yellow
pygame.draw.circle(screen, color, self.pos_patient, 15, 0)
elif self.sp>= 70:
color = need_first_aid_red
pygame.draw.circle(screen, color, self.pos_patient, 15, 0)
def draw_a_bed(self): #这里是在画病床和指示条的框
blue = 176,196,222
pygame.draw.rect(screen, blue, self.pos_bed, 3)
pygame.draw.rect(screen, blue, self.pos_instruction, 1)
pygame.draw.line(screen, healthy_green, (self.x +7, self.y+50-5), (self.x + 10, self.y+50-5))
pygame.draw.line(screen, need_first_aid_red, (self.x +7, self.y+50-35), (self.x +10, self.y+50-35))
def watch_patient_beside(self):#这个函数就是用来监测一定距离内的病人是否为重症,主要想法就是如果这个病人指标达到重症,
#那么它的坐标就会被加入到一个集合中,同时关于这个病人也会判断在围绕着这个病人的一定的
#范围内,有没有其他重症患者,如果有,那么“self.beside_sick_flag”就会变成True,反馈到上面的函数中
self.sick_beside_number = 0
if self.sp>= 70 and self.patient_point not in seriously_sick:
seriously_sick.add(self.patient_point)
if self.sp < 70 and self.patient_point in seriously_sick:
seriously_sick.remove(self.patient_point)
for patient_pos in seriously_sick:
if 0 < self.patient_point.distance(patient_pos) < 301:
self.beside_sick_flag = True
self.sick_beside_number +=1
if self.sick_beside_number ==0 :
self.beside_sick_flag = False
def patient_move(self):
if self.patient_pos_x < pos_nurse_station_x:
self.patient_vel_x = 5
elif self.patient_pos_x > pos_nurse_station_x:
self.patient_vel_x = -5
if self.patient_pos_y < pos_nurse_station_y:
self.patient_vel_x = 5
elif self.patient_pos_y > pos_nurse_station_y:
self.patient_vel_x = -5
self.patient_pos_x +=self.patient_vel_x
self.patient_pos_y +=self.patient_vel_y
screen.fill((255,255,255))
pos_nurse_station = 186, 210, 228, 80
pos_nurse_station_x = 186
pos_nurse_station_y = 210
patient_1 = Patient(40,0)
patient_2 = Patient(40+138,0)
patient_3 = Patient(40+138*2,0)
patient_4 = Patient(40+138*3,0)
patient_5 = Patient(40,300)
patient_6 = Patient(40+138,300)
patient_7 = Patient(40+138*2,300)
patient_8 = Patient(40+138*3,300)
patient_1.draw_a_bed()
patient_2.draw_a_bed()
patient_3.draw_a_bed()
patient_4.draw_a_bed()
patient_5.draw_a_bed()
patient_6.draw_a_bed()
patient_7.draw_a_bed()
patient_8.draw_a_bed()
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
pygame.quit()
sys.exit()
pygame.draw.rect(screen,(135,206,250), pos_nurse_station, 0 )
pygame.draw.rect(screen,(255,0,0),(191,220,13,3),0)
pygame.draw.rect(screen,(255,0,0),(196,215,3,13),0)
myfont = pygame.font.Font(None, 46)
nurse_station = myfont.render("Nurse Station", True, (65,105,225))
screen.blit(nurse_station, (195,236))
patient_1.patient_move()
patient_2.patient_move()
patient_3.patient_move()
patient_4.patient_move()
patient_5.patient_move()
patient_6.patient_move()
patient_7.patient_move()
patient_8.patient_move()
patient_1.patient_change()
patient_2.patient_change()
patient_3.patient_change()
patient_4.patient_change()
patient_5.patient_change()
patient_6.patient_change()
patient_7.patient_change()
patient_8.patient_change()
patient_1.watch_patient_beside()
patient_2.watch_patient_beside()
patient_3.watch_patient_beside()
patient_4.watch_patient_beside()
patient_5.watch_patient_beside()
patient_6.watch_patient_beside()
patient_7.watch_patient_beside()
patient_8.watch_patient_beside()
pygame.display.update()
以上是代码,我想让表示病人的那几个圆圈动起来,为此设置了patient_move的函数,也没有报错,但是那个图形就是动不起来,请问这是为什么呢
非常感谢!
给你个思路
import sys, random, pygame
from pygame.locals import *
HEIGHT = 400
WIDTH = 400
S_F_SIZE = 10 # 食物和蛇一格的大小
SCREEN_SIZE = (HEIGHT, WIDTH) # 屏幕尺寸
MOVE_PS = 15 # 每秒刷新次数,模拟fps
DIR = 0 # 蛇移动的方向,0123分别为上右下左
FOOD = # 食物位置
SNAKE = [] # 蛇
# 屏幕设置,第一个参数分辨率,第二个参数模式(不需要全屏则置0,若需要全屏则为FULLSCREEN),第三个参数色深
SCREEN = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
SCORE = 0
def run():
global DIR
global SCREEN
global SCORE
for event in pygame.event.get():
# 退出
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
# 按键
elif event.type == pygame.KEYDOWN:
if (event.key == K_ESCAPE):# 终止程序
pygame.quit()
sys.exit(0)
# 上下左右改变方向,不能去反方向
elif (event.key == K_LEFT and DIR != 1):
DIR = 3
elif (event.key == K_RIGHT and DIR != 3):
DIR = 1
elif (event.key == K_UP and DIR != 2):
DIR = 0
elif (event.key == K_DOWN and DIR != 0):
DIR = 2
# 蛇头接下来的位置
if (DIR == 0):
next_head = , SNAKE-1]
elif (DIR == 1):
next_head = +1, SNAKE]
elif (DIR == 2):
next_head = , SNAKE+1]
elif (DIR == 3):
next_head = -1, SNAKE]
# 判断蛇是否会死
if (next_head >= WIDTH/S_F_SIZE or next_head < 0 or next_head > HEIGHT/S_F_SIZE or next_head < 0) or ((next_head in SNAKE) and (next_head != SNAKE[-1])):
return 5
# 更新蛇的位置和形态
SNAKE.insert(0, next_head)
if (next_head == FOOD):
create_food()
SCORE+=1
print(SCORE)
else:
SNAKE.pop()
SCREEN.fill((255, 255, 255))
draw_snake()
draw_food()
# 刷新界面
pygame.display.update()
# 时钟对象用于控制界面刷新(即蛇移动频率)
pygame.time.Clock().tick(MOVE_PS)
def draw_snake():
global SCREEN
for coord in SNAKE:
y = coord * S_F_SIZE
x = coord * S_F_SIZE
rect = pygame.Rect(x, y, S_F_SIZE, S_F_SIZE)
pygame.draw.rect(SCREEN, (0, 0, 255), rect)
def draw_food():
global SCREEN
rect = pygame.Rect(FOOD*S_F_SIZE, FOOD*S_F_SIZE, S_F_SIZE, S_F_SIZE)
pygame.draw.rect(SCREEN, (255, 0, 0), rect)
def create_food():
while True:
y_food = random.randint(1, HEIGHT/S_F_SIZE - 1)
x_food = random.randint(1, WIDTH/S_F_SIZE - 1)
FOOD = y_food
FOOD = x_food
if FOOD not in SNAKE:
break
def init():
global SCREEN
# 初始生成蛇和食物
# 蛇开始位置,不能太靠边
y_snake = random.randint(10, HEIGHT/S_F_SIZE-10)
x_snake = random.randint(10, WIDTH/S_F_SIZE-10)
SNAKE.append()
SNAKE.append()
SNAKE.append()
print(SNAKE)
# 食物开始位置
create_food()
# 填充白色背景
SCREEN.fill((255, 255, 255))
# 画蛇和食物
draw_snake()
draw_food()
print("ss")
# 显示
pygame.display.update()
def main():
global SCREEN
# pygame初始化
pygame.init()
# 初始化
init()
# 游戏主体,包括操作、刷新等逻辑控制
while True:
if run() == 5:
break;
if __name__ == '__main__':
main()
页:
[1]