我是第一次使用python pygame编写游戏。它基于飘扬的小鸟和游戏的主要思想是相同的。但是碰撞代码无法正常运行,有时游戏会停止,并且说我什么都没击中就输了。 这是我的代码片段:
import pygame
import random
import os
import time
import sys
import math
from datetime import datetime
pygame.init()
WIDTH = 500
HEIGHT = 600
Black = (0, 0, 0)
pygame.display.set_caption("FlappyBee")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
def lost(txt, x, y, size):
cz = pygame.font.SysFont("Times New Roman", size)
rend = cz.render(txt, 1, (255, 0, 0))
x = (WIDTH - rend.get_rect().width) / 2
y = (HEIGHT - rend.get_rect().height) / 2
screen.blit(rend, (x, y))
pygame.display.update()
class Bee:
ANIMATION_TIME = 5
MAX_ROTATION = 30
ROT_VEL = 20
BEE = BEE_IMG
def __init__(self, x, y):
self.x = x
self.y = y
self.tilt = 0
self.tick_count = 0
self.vel = 0
self.height = self.y
self.img_count = 0
self.img = self.BEE
self.hitbox = (self.x + 220, self.y + 220, 55, 55)
def jump(self):
self.vel = -9.5
self.tick_count = 0
self.height = self.y
def move(self):
self.tick_count += 1
d = self.vel * self.tick_count + 1.5 * self.tick_count ** 2
if d >= 16:
d = 16
if d < 0:
d -= 2
self.y = self.y + d
if d < 0 or self.y < self.height:
if self.tilt < self.MAX_ROTATION:
self.tilt = self.MAX_ROTATION
else:
if self.tilt > -90:
self.tilt -= self.ROT_VEL
def draw(self, screen):
self.img_count += 1
if self.img_count < self.ANIMATION_TIME:
self.img = self.BEE
self.img_count = 0
rotated_image = pygame.transform.rotate(self.img, self.tilt)
new_rect = rotated_image.get_rect(center=self.img.get_rect(topleft=(self.x, self.y)).center)
screen.blit(rotated_image, new_rect.topleft)
self.hitbox = (self.x + 220, self.y + 220, 55, 55)
# pygame.draw.rect(screen, (255, 0, 0), self.hitbox, 2)
def hit(self):
screen.fill(Black)
lost("you lost", 40, 100, 70)
time.sleep(3)
class Trunk:
GAP = 200
VEL = 5
def __init__(self, x):
self.x = x
self.height = 0
self.top = 0
self.bottom = 0
self.TOPTRUNK = pygame.transform.flip(TRUNK, False, True)
self.BOTTOMTRUNK = TRUNK
self.passed = False
self.set_height()
self.hitboxtop = (self.x + 80, self.top, 80, 490)
self.hitboxbottom = (self.x + 80, self.bottom + 10, 80, 490)
def set_height(self):
self.height = random.randrange(50, 400)
self.top = self.height - self.TOPTRUNK.get_height()
self.bottom = self.height + self.GAP
def move(self):
self.x -= self.VEL
def draw(self, screen):
screen.blit(self.TOPTRUNK, (self.x, self.top))
screen.blit(self.BOTTOMTRUNK, (self.x, self.bottom))
self.hitboxtop = (self.x + 80, self.top, 80, 490)
self.hitboxbottom = (self.x + 80, self.bottom + 10, 80, 490)
# pygame.draw.rect(screen, (255, 0, 0), self.hitboxtop, 2)
# pygame.draw.rect(screen, (255, 0, 0), self.hitboxbottom, 2)
def main():
bee = Bee(-15, -100)
trunks = [Trunk(400)]
run = True
clock = pygame.time.Clock()
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bee.jump()
draw_window(screen, bee, trunks)
add_trunk = False
rem = []
for trunk in trunks:
if trunk.x + trunk.TOPTRUNK.get_width() < 0:
rem.append(trunk)
if not trunk.passed and trunk.x < bee.x:
trunk.passed = True
add_trunk = True
trunk.move()
if add_trunk:
trunks.append(Trunk(400))
for r in rem:
trunks.remove(r)
for trunk in trunks:
if trunk.hitboxtop[0] - (bee.hitbox[0] + bee.hitbox[2]) < 0 and (trunk.hitboxtop[1] + trunk.hitboxtop[3]) > (bee.hitbox[1] + bee.hitbox[3]):
bee.hit()
if trunk.hitboxbottom[0] - (bee.hitbox[0] + bee.hitbox[2]) < 0 and trunk.hitboxbottom[1] < bee.hitbox[1]:
bee.hit()
if trunk.hitboxtop[0] < bee.hitbox[0] < trunk.hitboxtop[0] + trunk.hitboxtop[2]:
if bee.hitbox[1] - (trunk.hitboxtop[1] + trunk.hitboxtop[3]) < 0:
bee.hit()
if bee.hitbox[1] + bee.hitbox[3] - trunk.hitboxbottom[1] > 0:
bee.hit()
ground.move()
bee.move()
我感觉有时候,当屏幕上有两个树干时,蜜蜂不知道应该集中在哪一个上。我不知道我应该改变什么。 如果有人可以帮助我,那就太好了。