Python Rect值不更新x位置?
我正在创造一个汽车向你冲过来的游戏。我希望汽车每次离开屏幕时都能改变x_位置,但由于某些原因,值会改变,但汽车实际上不会移动。我不知道如何解决这个问题,我想知道我是否需要做些什么来改变它。如果你想测试我的代码,只需删除背景,其他一切都可以。谢谢Python Rect值不更新x位置?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在创造一个汽车向你冲过来的游戏。我希望汽车每次离开屏幕时都能改变x_位置,但由于某些原因,值会改变,但汽车实际上不会移动。我不知道如何解决这个问题,我想知道我是否需要做些什么来改变它。如果你想测试我的代码,只需删除背景,其他一切都可以。谢谢 import time, pygame, random import math pygame.init() #First Variables BLACK = (0, 0, 0) BLUE = (0, 0, 255) GREEN = (100, 255,
import time, pygame, random
import math
pygame.init()
#First Variables
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (100, 255, 100)
ORANGE = (255, 140, 0)
YELLOW = (155, 135, 12)
white = (255, 255, 255)
GOLD = (255, 215, 0)
screenwidth = 500
screenheight = 500
Lines = True
Space = True
color = random.sample(range(250), 3)
i = []
x = 247
y = - 20
y1 = 40
y2 = 100
y3 = 160
y4 = 220
y5 = 280
y6 = 340
y7 = 400
y8 = 460
#Display and caption
win = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption('Lil cary')
clock = pygame.time.Clock()
#Load in images
bkg = pygame.image.load('BG.jpg')
#Actual player class
class player():
def __init__(self):
self.color = (12, 124, 134)
self.vel = 5
self.grassdamage = 0
self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
self.rect = self.image.get_rect(center = (200, 390))
self.damage1 = False
self.damage2 = False
self.damage3 = False
self.damage4 = False
self.damage5 = False
self.damage6 = False
self.damage7 = False
self.damage8 = False
self.dead = False
self.score = 0
def draw (self, win):
#pygame.draw.rect(win, (self.color), (self.x, self.y, self.width, self.height))
win.blit(self.image, self.rect.topleft)
def moveback(self):
if self.rect.y < 390 and self.rect.y >= 0:
self.rect.y += 10
def grass(self):
if self.rect.x > -10 and self.rect.x < 150:
self.grassdamage += 1
self.vel = 3
elif self.rect.x > 300 and self.rect.x < 500:
self.vel =3
self.grassdamage += 1
else:
self.vel = 5
def grasshealth(self):
if self.grassdamage == 100:
self.damage1 = True
elif self.grassdamage == 200:
self.damage2 = True
elif self.grassdamage == 300:
self.damage3 = True
elif self.grassdamage == 400:
self.damage4 = True
elif self.grassdamage == 500:
self.damage5 = True
elif self.grassdamage == 600:
self.damage6 = True
elif self.grassdamage == 600:
self.damage6 = True
elif self.grassdamage == 700:
self.damage7 = True
elif self.grassdamage == 800:
self.damage8 = True
self.dead = True
def death(self):
if self.dead == True:
raise SystemExit ('YOU MOFO DIED')
def scorecount (self):
if self.score >= 15:
raise SystemExit ('YOU WIN OMG KIDS')
def scoredraw(self):
Text = pygame.font.Font('freesansbold.ttf', 25)
TextSurf, TextRect1 = text_objects("Score: " + str(self.score), Text)
TextRect1.center = ((50), (450))
win.blit(TextSurf, TextRect1)
#Text Setup
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
#Drawing damage bar
def drawbar ():
if player.damage1 == True and car.damage1 == False or car.damage1 == True:
pygame.draw.rect(win, (GOLD), (50, 50, 25, 25))
if player.damage2 == True and car.damage1 == False or car.damage1 == True:
pygame.draw.rect(win, (GOLD), (50, 75, 25, 25))
if car.damage1 == True and player.grassdamage <= 200:
player.grassdamage = 201
if player.damage3 == True and car.damage2 == False or car.damage2 == True:
pygame.draw.rect(win, (GOLD), (50, 100, 25, 25))
if player.damage4 == True and car.damage2 == False or car.damage2 == True:
pygame.draw.rect(win, (GOLD), (50, 125, 25, 25))
if car.damage2 == True and player.grassdamage <= 400:
player.grassdamage = 401
if player.damage5 == True and car.damage3 == False or car.damage3 == True:
pygame.draw.rect(win, (GOLD), (50, 150, 25, 25))
if player.damage6 == True and car.damage3 == False or car.damage3 == True:
pygame.draw.rect(win, (GOLD), (50, 175, 25, 25))
if car.damage3 == True and player.grassdamage <= 600:
player.grassdamage = 601
if player.damage7 == True and car.damage4 == False or car.damage4 == True:
pygame.draw.rect(win, (GOLD), (50, 200, 25, 25))
if player.damage8 == True and car.damage4 == False or car.damage4 == True:
pygame.draw.rect(win, (GOLD), (50, 225, 25, 25))
raise SystemExit ('YOU LOST')
if car.damage4 == True and player.grassdamage <= 800:
player.grassdamage = 800
pygame.draw.rect(win, (0,0,0), (50, 50, 25, 200), 3)
Text = pygame.font.Font('freesansbold.ttf', 20)
TextSurf, TextRect = text_objects("Damage", Text)
TextRect.center = ((65), (30))
win.blit(TextSurf, TextRect)
class car1():
ROAD_LEFT = 175
ROAD_RIGHT = 300
def __init__(self):
self.x_pos = random.randrange(car1.ROAD_LEFT, car1.ROAD_RIGHT)
self.color = random.sample(range(250), 3)
self.image = pygame.Surface([50, 100], pygame.SRCALPHA, 32)
pygame.draw.rect(self.image, (self.color), (0, 0, 50, 100))
self.rect = self.image.get_rect(topleft = (self.x_pos, -100))
self.carvel = random.randrange(8, 10)
self.damage = 0
self.damage1 = False
self.damage2 = False
self.damage3 = False
self.damage4 = False
def draw(self, win):
#pygame.draw.rect(win,(self.color),self.rect)
win.blit(self.image, self.rect.topleft)
def move(self):
if self.rect.y < 530:
self.rect.y += self.carvel
else:
self.rect.y = -100
player.score += 1
self.color = random.sample(range(250), 3)
self.carvel = random.randrange(6, 8)
self.x_pos = random.randrange(car1.ROAD_LEFT, car1.ROAD_RIGHT)
def collision_check(self, another_object):
if self.rect.colliderect(another_object):
print('collison')
self.rect.y = -100
self.damage += 1
def cardamage(self):
if self.damage == 1:
self.damage1 = True
player.damage1 = True
player.damage2 = True
if self.damage == 2:
self.damage2 = True
player.damage3 = True
player.damage4 = True
if self.damage == 3:
self.damage3 = True
player.damage5 = True
player.damage6 = True
if self.damage == 4:
self.damage4 = True
player.damage7 = True
player.damage8 = True
#Putting variables to the classes
player = player()
car = car1()
car_list = [ car ] # start with one car
car_add_time = 0
#Main drawing function
def redrawgamewindow():
win.blit(bkg, (0, 0))
pygame.draw.rect(win, (0, 0, 0), (150, 0, 200, 500))
pygame.draw.rect(win, (255, 255, 255), (x, (y), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y1), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y2), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y3), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y4), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y5), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y6), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y7), 10, 30))
pygame.draw.rect(win, (255, 255, 255), (x, (y8), 10, 30))
player.draw(win)
for car in car_list:
car.draw(win)
player.grasshealth()
for car in car_list:
car.cardamage()
player.grass()
player.death()
for car in car_list:
car.collision_check(player.rect)
car.move()
drawbar()
player.scorecount()
player.scoredraw()
pygame.display.update()
#MAINLOOP
run = True
while run:
#Making background and FPS
clock.tick(80)
#Quiting Funciton
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
time_now = pygame.time.get_ticks()
if (time_now - car_add_time > 5000):
new_car = car1()
car_list.append(new_car)
car_add_time = time_now
#Scrolling the lines
if Lines:
y += player.vel
y1 += player.vel
y2 += player.vel
y3 += player.vel
y4 += player.vel
y5 += player.vel
y6 += player.vel
y7 += player.vel
y8 += player.vel
if Lines:
if y >= 500:
y = -40
if y1 >= 500:
y1 = -40
if y2 >= 500:
y2 = -40
if y3 >= 500:
y3 = -40
if y4 >= 500:
y4 = -40
if y5 >= 500:
y5 = -40
if y6 >= 500:
y6 = -40
if y7 >= 500:
y7 = -40
if y8 >= 500:
y8 = -40
#User input
keys = pygame.key.get_pressed()
#Boost controller
if keys[pygame.K_SPACE]:
start_time = time.time()
if player.rect.y > 200:
player.rect.y -= 9
Space = True
else:
player.moveback()
end_time = time.time()
#Left movement
if keys[pygame.K_LEFT] and player.rect.x > 150:
player.rect.x -= 5
if keys [pygame.K_LEFT] and player.rect.x <= 150:
if player.rect.x > 0:
player.rect.x -=5
#Right movement
if keys[pygame.K_RIGHT] and player.rect.x < 300:
player.rect.x += 5
if keys[pygame.K_RIGHT]and player.rect.x >= 300:
if player.rect.x < 500 - player.rect.width:
player.rect.x += 5
#Grass and grass damage
#MAIN DRAW RECALL
redrawgamewindow()
导入时间,pygame,随机
输入数学
pygame.init()
#第一变量
黑色=(0,0,0)
蓝色=(0,0255)
绿色=(100255100)
橙色=(255140,0)
黄色=(155、135、12)
白色=(255,255,255)
黄金=(255,215,0)
屏幕宽度=500
屏幕高度=500
行=真
空格=真
颜色=随机样本(范围(250),3)
i=[]
x=247
y=-20
y1=40
y2=100
y3=160
y4=220
y5=280
y6=340
y7=400
y8=460
#显示和标题
win=pygame.display.set_模式((屏幕宽度、屏幕高度))
pygame.display.set_标题('Lil cary')
clock=pygame.time.clock()
#加载图像
bkg=pygame.image.load('BG.jpg')
#实际玩家等级
类播放器():
定义初始化(自):
self.color=(12124134)
self.vel=5
self.grassdamage=0
self.image=pygame.Surface([50100],pygame.SRCALPHA,32)
pygame.draw.rect(self.image,(self.color),(0,0,50,100))
self.rect=self.image.get_rect(中心=(200390))
self.damage1=错误
self.damage2=错误
self.damage3=错误
self.damage4=错误
self.damage5=错误
self.damage6=错误
self.damage7=错误
self.damage8=错误
self.dead=False
self.score=0
def抽签(自我,赢):
#pygame.draw.rect(win,(self.color),(self.x,self.y,self.width,self.height))
win.blit(self.image,self.rect.topleft)
def回移(自):
如果self.rect.y<390且self.rect.y>=0:
自校正y+=10
def grass(自我):
如果self.rect.x>-10且self.rect.x<150:
自我伤害+=1
self.vel=3
如果self.rect.x>300且self.rect.x<500:
self.vel=3
自我伤害+=1
其他:
self.vel=5
健康(自我):
如果self.grassdamage==100:
self.damage1=真
elif self.grassdamage==200:
self.damage2=正确
elif self.grassdamage==300:
self.damage3=正确
elif self.grassdamage==400:
self.damage4=正确
elif self.grassdamage==500:
self.damage5=正确
elif self.grassdamage==600:
self.damage6=正确
elif self.grassdamage==600:
self.damage6=正确
elif self.grassdamage==700:
self.damage7=正确
elif self.grassdamage==800:
self.damage8=正确
self.dead=True
死亡(自我):
如果self.dead==True:
升起系统出口('你死了')
def记分计数(自我):
如果self.score>=15:
升起系统出口(“你赢了我的孩子们”)
def scoredraw(自我):
Text=pygame.font.font('freesansbold.ttf',25)
TextSurf,TextRect1=文本对象(“分数:+str(self.Score),text)
TextRect1.center=(50),(450))
win.blit(TextSurf,TextRect1)
#文本设置
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
#拉拔损伤杆
def牵引杆():
如果player.damage1==真且car.damage1==假或car.damage1==真:
pygame.draw.rect(赢,(金),(50,50,25,25))
如果player.damage2==真且car.damage1==假或car.damage1==真:
pygame.draw.rect(赢,(金),(50,75,25,25))
如果car.damage1==真且player.grassdamage=500:
y2=-40
如果y3>=500:
y3=-40
如果y4>=500:
y4=-40
如果y5>=500:
y5=-40
如果y6>=500:
y6=-40
如果y7>=500:
y7=-40
如果y8>=500:
y8=-40
#用户输入
keys=pygame.key.get_pressed()
#升压控制器
如果键[pygame.K_SPACE]:
开始时间=time.time()
如果player.rect.y>200:
player.rect.y-=9
空格=真
其他:
player.moveback()
结束时间=time.time()
#左移
如果键[pygame.K_LEFT]和player.rect.x>150:
player.rect.x-=5
如果键[pygame.K_LEFT]和player.rect.x 0:
player.rect.x-=5
#右转
如果键[pygame.K_RIGHT]和player.rect.x<300:
player.rect.x+=5
如果键[pygame.K_RIGHT]和player.rect.x>=300:
如果player.rect.x<500-player.rect.width:
player.rect.x+=5
#草与草害
#主抽签召回
重画游戏窗口()
将随机值设置为self.x\u pos
是不够的。绘制“汽车”时,使用对象的位置self.rect
:
def draw(自我,赢):
win.blit(self.image,self.rect.topleft)
因此,您还必须更改self.rect.x:
class car1():
# [...]
def移动(自我):
如果自校正y<530:
self.rect.y+=self.carvel
其他:
self.rect.y=-100
player.score+=1
self.color=random.sample(范围(250),3)
self.carvel=random.randrange(6,8)
self.x_pos=random.randrange(car1.ROAD_左侧,car1.ROAD_右侧)
self.rect.x=self.x#请仅向我们显示相关代码,我们可以使用这些代码进行测试。而不是整个计划。