字符随键盘输入移动而不在屏幕上实际移动



所以我有一个问题,我的角色是在第一帧绘制的,我可以移动和收集"弹药;有了它,但我的性格是看不见的,我不知道为什么会发生这种事。还试图将某些内容从Vector更改为rect,但不起作用。

import pygame
from sys import exit
from pygame.math import Vector2
import random
pygame.init()
win = pygame.display.set_mode((800,600))
pygame.display.set_caption("Rectshooter")
clock = pygame.time.Clock()
game_active = True

class Player :
width = height = 50
def __init__(self,color=(255,0,0)):
self.body = Vector2(500,200)
self.color = color
self.direction = Vector2(0,0)
self.player_rect = pygame.Rect(self.body.x,self.body.y,self.width,self.height)

def draw(self):
#pygame.draw.rect(win,self.color,(self.body.x,self.body.y,self.width,self.height))
pygame.draw.rect(win, self.color, self.player_rect)
def move(self):
self.body += self.direction
class Player2 :
width = height = 50
def __init__(self,color=(255,0,0)):
self.body = Vector2(300,200)
self.color = color
self.direction = Vector2(0,0)


def draw(self):
pygame.draw.rect(win,self.color,(self.body.x,self.body.y,self.width,self.height))
def move(self):
self.body += self.direction
class Ammo:
width = height = 30
def __init__(self,x1,x2):
self.x = random.randint(x1,x2)
self.y = random.randint(50,550)
self.pos = Vector2(self.x,self.y)
self.ammo_rect = pygame.Rect(self.x,self.y,self.width,self.height)
def draw_ammo(self):
pygame.draw.ellipse(win,(255,100,00),self.ammo_rect)




player1 = Player()
player2 = Player2()
speed = 15 #player speed
ammo1 = Ammo(50,300)
ammo2 = Ammo(450,750)
print(ammo2.pos.x)
print(player1.body.x)

碰撞还有另一个问题,我为它写了一个很长的if语句,它运行良好,但我想用简单的"player1.player_rect.colderect(ammon2.ammon_rect(";if语句,但这只在弹药(随机生成(spwans在玩家的位置时触发,我指的是在第一帧生成的玩家的原始位置,而不是当前位置。我很困惑,我需要一点帮助。

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#player1 key pressing movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player1.direction = Vector2(0,-speed)
if event.key == pygame.K_DOWN:
player1.direction = Vector2(0,speed)
if event.key == pygame.K_LEFT:
player1.direction = Vector2(-speed,0)
if event.key == pygame.K_RIGHT:
player1.direction = Vector2(speed,0)
#player2 key pressing movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player2.direction = Vector2(0,-speed)
if event.key == pygame.K_s:
player2.direction = Vector2(0,speed)
if event.key == pygame.K_a:
player2.direction = Vector2(-speed,0)
if event.key == pygame.K_d:
player2.direction = Vector2(speed,0)

if game_active:
#player1 preventing from going out of the screen and the middle line
if player1.body.y < 0:#up
player1.body.y = 0
player1.direction = Vector2(0,0)
if player1.body.x < 410:#left
player1.body.x = 410
player1.direction = Vector2(0,0)
if player1.body.y > 550:#down
player1.body.y = 550
player1.direction = Vector2(0,0)
if player1.body.x > 750:#right
player1.body.x = 750
player1.direction = Vector2(0,0)
#player2 preventing from going out of the screen and the middle line
if player2.body.y < 0:#up
player2.body.y = 0
player2.direction = Vector2(0,0)
if player2.body.x < 0:#left
player2.body.x = 0
player2.direction = Vector2(0,0)
if player2.body.y > 550:#down
player2.body.y = 550
player2.direction = Vector2(0,0)
if player2.body.x > 341:#right
player2.body.x = 341
player2.direction = Vector2(0,0)
#collide p1 with ammo1
if ammo2.pos.x + 20 > player1.body.x - 20 and ammo2.pos.x - 20 < player1.body.x + 20 and ammo2.pos.y + 20 > player1.body.y - 20 and ammo2.pos.y - 20 < player1.body.y + 20:
ammo2 = Ammo(450,750)
ammo2.draw_ammo()
print("collide")
if player1.player_rect.colliderect(ammo2.ammo_rect):
print("collide with start position")
#print(player1.body.x)
#print(player1.body.y)


win.fill((0, 0, 0))
pygame.draw.line(win, (255, 255, 255), (400, 600), (400, 0), 20)
#p1
player1.draw()
player1.move()
#p2
player2.draw()
player2.move()
#ammo
ammo1.draw_ammo()
ammo2.draw_ammo()


pygame.display.update()
clock.tick(60)

玩家被绘制在player_rect属性中存储的位置。更改body属性时,矩形的位置不会神奇地更改。您需要更新矩形的位置:

class Player :
width = height = 50
def __init__(self,color = (255,0,0)):
self.body = Vector2(500, 200)
self.color = color
self.direction = Vector2(0, 0)
self.player_rect = pygame.Rect(self.body.x, self.body.y, self.width, self.height)
def draw(self):
pygame.draw.rect(win, self.color, self.player_rect)
def move(self):
self.body += self.direction
# update player_rect
self.player_rect.x = round(self.body.x)
self.player_rect.y = round(self.body.y)

最新更新