我正在尝试在pygame上制作一个空气曲棍球比赛,并且我遇到了矢量和碰撞的问题。 我是编程和pygame的新手,对sprite不太自信,所以我尝试使用距离进行碰撞。这是我的代码:(我不知道如何发布图像,但基本上播放器有右半边,电脑有左半边,中间是350.我的另一个问题是电脑播放器移动得太快)
import pygame, sys
from random import randint
from pygame.locals import *
import time
#defines the colours
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
blue = (0,0,255)
yellow= (255,242,0)
pygame.init()
size = 700,500
#where the computers mallet is drawn
xPos = randint(1,320)
yPos = randint(30,500)
aPos = 350
bPos = 250
xSpeed = 0
ySpeed = 0
score=0
OppScore=0
diffx = 0
diffy = 0
OppDiffx = 0
OppDiffy = 0
vdiffx = 0
vdiffy = 0
LastPosx = 0
LastPosy = 0
moving = 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Electro Air Hockey")
done = False
CompletedAll = False
clock = pygame.time.Clock()
def PlayerMallets():
global x,y
global Mallet
global xSpeed
global xPos
global vdiffx
global LastPosx
global vdiffy
global LastPosy
global moving
pygame.mouse.set_visible(False)
BackFrame = pygame.image.load("C:\Users\DOAA\Desktop\Coding\Python\ElectroAirHockey\E_A_H_Background.png").convert_alpha()
Mallet = pygame.image.load("C:\Users\DOAA\Desktop\Coding\Python\ElectroAirHockey\Mallet_Trans.png").convert_alpha()
OppMallet=pygame.image.load("C:\Users\DOAA\Desktop\Coding\Python\ElectroAirHockey\OppMallet_Trans.png").convert_alpha()
#the players mallet position is the same as the mouse position
x,y = pygame.mouse.get_pos()
x -= Mallet.get_width()/2
y -= Mallet.get_height()/2
#$sets the boundaries for the player
if x <350:
x = 350
if x > 670:
x = 670
if y <0:
y=0
if y >470:
y=470
screen.blit(BackFrame, (0,0))
screen.blit(Mallet, (x,y))
yPos = bPos
xPos = xPos+randint(-2,2)
screen.blit(OppMallet, (xPos, yPos))
#this is the part where I started attempting to use vectors by seeing if the player is moving in the same direction that he was in last time
#xSpeed = xSpeed + 1
if vdiffx ==0 and LastPosx==0 and moving== 0 and vdiffy==0 and LastPosy==0:
LastPosx = x
LastPosy = y
moving = 1
if moving == 1:
vdiffx = x-LastPosx
vdiffy = y-LastPosy
def Puck():
global aPos
global bPos
global xSpeed
global ySpeed
global score
global OppScore
if aPos < 0:
#xSpeed = 0-xSpeed
#ySpeed = 0-ySpeed
if bPos>= 118 and bPos<=386:
xSpeed = 0
aPos = 350
bPos=250
ySpeed=0
score = score + 1
#xSpeed = 0-xSpeed
#ySpeed = 0-ySpeed
elif aPos < 0:
if bPos<118 or bPos>386:
if xSpeed<0:
xSpeed=xSpeed+1
xSpeed = 0-xSpeed
ySpeed = 0-ySpeed
if xSpeed>0:
xSpeed=xSpeed-1
#ySpeed = 0-ySpeed
if aPos > 640:
if bPos>= 118 and bPos<=386:
xSpeed = 0
aPos = 350
bPos=250
ySpeed=0
OppScore = OppScore + 1
#xSpeed = 0-xSpeed
#ySpeed = 0-ySpeed
elif aPos > 680:
if bPos<118 or bPos>386:
xSpeed = 0-xSpeed
#ySpeed = 0-ySpeed
if bPos < 0:
#xSpeed = 0-xSpeed
ySpeed = 0-ySpeed
if bPos >470:
#xSpeed = 0-xSpeed
ySpeed = 0-ySpeed
aPos=aPos+xSpeed
bPos=bPos-ySpeed
pygame.draw.circle(screen, yellow, [aPos, bPos], 15)
def Collision():
global xSpeed
global ySpeed
global xPos
global diffx
global diffy
global OppDiffx
global OppDiffy
#if x== aPos and y==bPos and xSpeed == 0 and x>=350:
#if pygame.sprite.collide_circle(Puck, Mallet):
# is_a_collision = pygame.sprite.collide_mask(Mallet, Puck)
# if is_a_collision == True:
# this part checks if they are in the same range because they have a radius of 15
if x>aPos:
diffx=x-aPos
if x<aPos:
diffx=aPos-x
if y>bPos:
diffy=y-bPos
if y<bPos:
diffy=bPos-y
if diffx <= 15 and diffy <= 15:
#xSpeed = xSpeed+1
#xSpeed = randint(-5,5)
xSpeed = -5
#ySpeed = ySpeed+1
#ySpeed = randint(-5,5)
ySpeed = 5
xPos = xPos+randint(-2,2)
elif aPos == x and bPos == y and xSpeed != 0:
xSpeed=0-xSpeed
ySpeed=0-ySpeed
xPos = xPos+randint(-2,2)
if xPos>aPos:
OppDiffx=xPos-aPos
if xPos<aPos:
OppDiffx=aPos-xPos
if OppDiffx <= 15:
#if xPos==bPos-5:
#xSpeed = randint(-5,5)
#ySpeed = randint(-5,5)
xSpeed = 5
ySpeed = 5
elif aPos == xPos and bPos == yPos and xSpeed != 0:
xSpeed=0-xSpeed
ySpeed=0-ySpeed
def Score():
#this part makes the players have seven lives
LivesLeft = 7 - OppScore
OppLives = 7 - score
font = pygame.font.SysFont("Arial", 45)
text = font.render(str(OppScore), True, white)
screen.blit(text, [50,5])
font = pygame.font.SysFont("Arial", 45)
text = font.render(str(score), True, white)
screen.blit(text, [650,5])
if LivesLeft == 0:
GameOver()
if OppLives == 0:
YouWin()
def GameOver():
global CompletedAll
font = pygame.font.SysFont("Arial", 75)
text = font.render("Game over", True, green)
screen.blit(text, [150, 250])
time.sleep(2)
CompletedAll = True
def YouWin():
global CompletedAll
font = pygame.font.SysFont("Arial", 75)
text = font.render("You won!", True, blue)
screen.blit(text, [150, 250])
time.sleep(2)
CompletedAll = True
while not done:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if CompletedAll == True:
done = True
screen.fill(white)
PlayerMallets()
Puck()
Collision()
Score()
#I suspect that the reason the computer moves very quickly is because of the line underneath which permanently redraws their mallet at another
#random position but for some reason I can't use a time delay or clock.tick because that causes other problems. I also tried to put it in a while
#loop so that it redraws every 10 times but that causes notepad c++ not to run it
xPos = randint(0,350)
pygame.display.update()