【发布时间】:2019-06-19 07:17:48
【问题描述】:
我正在广场的四面打一个 4 人乒乓球,但我正在思考如何在每个单独的桨之间添加碰撞,这样它们就不会在角落里相互相撞
import pygame
pygame.init()
### Colors
WHITE = (255, 255, 255)
BLACK = (0,0,0)
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("GNOP")
x = 485
y = 75
x2 = 5
y2 = 75
x3 = 250
y3 = 5
y4 = 485
x4 = 250
width = 10
height = 90
vel = 5
run = True
while run:
print(x4,y4)
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
key= pygame.key.get_pressed()
if key [pygame.K_UP] and y>0:
y -= vel
if key [pygame.K_DOWN]and y<420:
y += vel
if key [pygame.K_w] and y2>0:
y2 -= vel
if key [pygame.K_s]and y2<420:
y2 += vel
if key [pygame.K_c] and x3>0:
x3 -= vel
if key [pygame.K_v] and x3<420:
print(1)
x3 += vel
if key [pygame.K_n] and x4>0:
x4 -= vel
if key [pygame.K_m] and x4<420:
x4 += vel
def ball (self):
# Create the image of the ball
self.image = pygame.Surface([10, 10])
# Color the ball
self.image.fill(BLACK)
# Get a rectangle object that shows where our image is
self.rect = self.image.get_rect()
# Get attributes for the height/width of the screen
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
# Speed in pixels per cycle
self.speed = 0
# Floating point representation of where the ball is
self.x = 0
self.y = 0
# Direction of ball in degrees
self.direction = 0
# Height and width of the ball
self.width = 10
self.height = 10
# Set the initial ball speed and position
self.reset()
win.fill((230,230,230))
pygame.draw.rect(win,(0, 0, 0), (x, y, width, height))
pygame.draw.rect(win,(255,50,0), (x2, y2, width, height))
pygame.draw.rect(win,(150,15,200), (x3, y3, height, width))
pygame.draw.rect(win,(0,100,255), (x4, y4, height, width))
pygame.display.update()
pygame.quit()
ball()
我需要桨叶相互检测并碰撞而不是相互碰撞,但我已经看过几个代码,我还没有学会如何单独为每个代码做到这一点,这是至关重要的,因为我无法拿到球没有它也能工作
【问题讨论】:
-
...也就是说——一个问题应该只有最短的代码来重现一个特定的、狭窄的错误。一个理想的问题将对其他有相同问题的人有所帮助——这意味着它需要关于一个特定的、孤立的问题;不仅仅是一个人的个人代码。