Python 有人能修复我的Pygame冲突代码吗?

Python 有人能修复我的Pygame冲突代码吗?,python,python-3.x,pygame,flappy-bird-clone,Python,Python 3.x,Pygame,Flappy Bird Clone,作为总结,我在寒假期间为Pygame做了一个学校项目。我做了这个游戏的基础(Flappy Bird),但问题是碰撞没有正常工作。问题是,它将穿过管道视为碰撞 你们能检查一下我的密码并把它修好吗 import pygame import sys import math import random from pygame.locals import * a=320 b=240 da=1 db=0 x=25 y=25 e=50 da2=0 c=200 d=10 c_change=0 d_change

作为总结,我在寒假期间为Pygame做了一个学校项目。我做了这个游戏的基础(Flappy Bird),但问题是碰撞没有正常工作。问题是,它将穿过管道视为碰撞

你们能检查一下我的密码并把它修好吗

import pygame
import sys
import math
import random
from pygame.locals import *
a=320

b=240
da=1
db=0
x=25
y=25
e=50
da2=0
c=200
d=10
c_change=0
d_change=0
clock = pygame.time.Clock()
bg=(36,38,82)
wood=(253,197,136)
green=(79, 255, 101)
pipe=(152,228,86)
end=(137, 226, 57)
bg1=(40,42,86)
gold=(219,178,58)
golden=(254, 197, 34)
golder=(255, 206, 63)
black=(0,0,0)
red=(255, 47, 47)
white=(255,255,255)
pygame.init()
screen = pygame.display.set_mode((1400,700))
class Wall(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        super().__init__()


        self.image = pygame.Surface([width, height])
        self.image.fill(GREY)


        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

crashFlag=0

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_SPACE:
                c_change=-1
                d_change=1
        if event.type==pygame.KEYUP:
            if event.key==pygame.K_SPACE:
                c_change=1
        if event.type==pygame.KEYDOWN:
            crashFlag==0


    c+=c_change
    d+=d_change

    screen.fill(bg)
    pygame.draw.rect(screen,bg1, (0,350,1400,350), 0)


    pygame.draw.circle(screen,white, (d,c),5)
    #for f in range(195, 390 ,5):
    if d>200 and d<275:
        if c>340:
            crashFlag=1

    pygame.draw.rect(screen,end, (195,300,80,40), 0)
    pygame.draw.rect(screen,pipe, (200,0,70,300), 0)
    pygame.draw.rect(screen,pipe, (350,0,70,450), 0)
    pygame.draw.rect(screen,end, (345,420,80,40), 0)
    pygame.draw.rect(screen,pipe, (490,0,70,480), 0)
    pygame.draw.rect(screen,end, (485,480,80,40), 0)
    pygame.draw.rect(screen,pipe, (630,0,70,450), 0)
    pygame.draw.rect(screen,end, (625,450,80,40), 0)
    pygame.draw.rect(screen,pipe, (770,0,70,430), 0)
    pygame.draw.rect(screen,end, (765,420,80,40), 0)
    pygame.draw.rect(screen,pipe, (910,0,70,400), 0)
    pygame.draw.rect(screen,end, (905,400,80,40), 0)
    pygame.draw.rect(screen,pipe, (1050,0,70,470), 0)
    pygame.draw.rect(screen,end, (1045,470,80,40), 0)
    pygame.draw.rect(screen,pipe, (1190,0,70,430), 0)
    pygame.draw.rect(screen,end, (1185,430,80,40), 0)
    pygame.draw.rect(screen,gold, (1330,0,70,410), 0)
    pygame.draw.rect(screen,golder, (1350,0,70,410), 0)
    pygame.draw.rect(screen,golden, (1325,410,80,40), 0)
    pygame.draw.rect(screen,pipe, (200,400,70,240), 0)
    #lower pipes
    pygame.draw.rect(screen,end, (195,400,80,40), 0)
    pygame.draw.rect(screen,pipe, (350,520,70,500), 0)
    pygame.draw.rect(screen,end, (345,515,80,40), 0)
    pygame.draw.rect(screen,pipe, (490,570,70,100), 0)
    pygame.draw.rect(screen,end, (485,570,80,40), 0)
    pygame.draw.rect(screen,pipe, (630,570,70,100), 0)
    pygame.draw.rect(screen,end, (625,540,80,40), 0)
    pygame.draw.rect(screen,pipe, (770,550,70,120), 0)
    pygame.draw.rect(screen,end, (765,510,80,40), 0)
    pygame.draw.rect(screen,pipe, (910,530,70,220), 0)
    pygame.draw.rect(screen,end, (905,490,80,40), 0)
    pygame.draw.rect(screen,pipe, (1050,560,70,220), 0)
    pygame.draw.rect(screen,end, (1045,560,80,40), 0)
    pygame.draw.rect(screen,pipe, (1190,530,70,220), 0)
    pygame.draw.rect(screen,end, (1185,530,80,40), 0)
    pygame.draw.rect(screen,gold, (1330,530,70,220), 0)
    pygame.draw.rect(screen,golder, (1350,530,70,220), 0)
    pygame.draw.rect(screen,golden, (1325,510,80,40), 0)
    pygame.draw.rect(screen, wood, (0,650,1400,50), 0)
    pygame.draw.rect(screen,green, (0,640,1400,10), 0)

    if crashFlag==1:
        pygame.draw.rect(screen,white, (0,0,1400,700), 0)
        font = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False)

        text = font.render("You Lost", True, black)
        screen.blit(text, (500, 100))
        pygame.draw.rect(screen,green, (600,400,190,60), 0)
        font = pygame.font.SysFont("Aharoni", 50, True, False)

        text = font.render("RESET", True, white)
        screen.blit(text, (630, 410))



    pygame.display.update()
    clock.tick(150)
pygame.quit()
导入pygame
导入系统
输入数学
随机输入
从pygame.locals导入*
a=320
b=240
da=1
db=0
x=25
y=25
e=50
da2=0
c=200
d=10
c_变化=0
d_变化=0
clock=pygame.time.clock()
bg=(36,38,82)
木材=(253197136)
绿色=(79255101)
管道=(152228,86)
结束=(137、226、57)
bg1=(40,42,86)
黄金=(219178,58)
黄金=(254、197、34)
戈尔德=(25520663)
黑色=(0,0,0)
红色=(255,47,47)
白色=(255255)
pygame.init()
screen=pygame.display.set_模式((1400700))
类墙(pygame.sprite.sprite):
定义初始值(自、x、y、宽度、高度):
super()。\uuuu init\uuuuu()
self.image=pygame.Surface([宽度,高度])
self.image.fill(灰色)
self.rect=self.image.get_rect()
自校正y=y
self.rect.x=x
崩溃滞后=0
完成=错误
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_空间:
c_变化=-1
d_变化=1
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_空间:
c_变化=1
如果event.type==pygame.KEYDOWN:
crashFlag==0
c+=c_变化
d+=d_变化
屏幕填充(bg)
pygame.draw.rect(屏幕,背景1,(03501400350),0)
pygame.draw.circle(屏幕,白色,(d,c),5)
#对于范围内的f(195、390、5):
如果d>200和d340:
碰撞滞后=1
pygame.draw.rect(屏幕,结束,(195300,80,40),0)
pygame.draw.rect(屏幕,管道,(200,0,70300),0)
pygame.draw.rect(屏幕,管道,(350,0,70450),0)
pygame.draw.rect(屏幕,结束,(345420,80,40),0)
pygame.draw.rect(屏幕,管道,(490,0,70480),0)
pygame.draw.rect(屏幕,结束,(485480,80,40),0)
pygame.draw.rect(屏幕,管道,(630,0,70450),0)
pygame.draw.rect(屏幕,结束,(625450,80,40),0)
pygame.draw.rect(屏幕,管道,(770,0,70430),0)
pygame.draw.rect(屏幕,结束,(765420,80,40),0)
pygame.draw.rect(屏幕,管道,(910,0,70400),0)
pygame.draw.rect(屏幕,结束,(905400,80,40),0)
pygame.draw.rect(屏幕,管道,(1050,0,70470),0)
pygame.draw.rect(屏幕,结束,(1045470,80,40),0)
pygame.draw.rect(屏幕,管道,(1190,0,70430),0)
pygame.draw.rect(屏幕,结束,(1185430,80,40),0)
pygame.draw.rect(屏幕,金色,(1330,0,70410),0)
pygame.draw.rect(屏幕,戈尔德,(1350,0,70410),0)
pygame.draw.rect(屏幕,金色,(1325410,80,40),0)
pygame.draw.rect(屏幕,管道,(200400,70240),0)
#下水管
pygame.draw.rect(屏幕,结束,(195400,80,40),0)
pygame.draw.rect(屏幕,管道,(35052070500),0)
pygame.draw.rect(屏幕,结束,(345515,80,40),0)
pygame.draw.rect(屏幕,管道,(490570,70100),0)
pygame.draw.rect(屏幕,结束,(485570,80,40),0)
pygame.draw.rect(屏幕,管道,(630570,70100),0)
pygame.draw.rect(屏幕,结束,(625540,80,40),0)
pygame.draw.rect(屏幕,管道,(77055070120),0)
pygame.draw.rect(屏幕,结束,(765510,80,40),0)
pygame.draw.rect(屏幕,管道,(910530,70220),0)
pygame.draw.rect(屏幕,结束,(905490,80,40),0)
pygame.draw.rect(屏幕,管道,(1050560,70220),0)
pygame.draw.rect(屏幕,结束,(1045560,80,40),0)
pygame.draw.rect(屏幕,管道,(1190530,70220),0)
pygame.draw.rect(屏幕,结束,(1185530,80,40),0)
pygame.draw.rect(屏幕,金色,(1330530,70220),0)
pygame.draw.rect(屏幕,戈尔德,(1350530,70220),0)
pygame.draw.rect(屏幕,金色,(1325510,80,40),0)
pygame.draw.rect(屏幕,木头,(06501400,50),0)
pygame.draw.rect(屏幕,绿色,(06401400,10),0)
如果crashFlag==1:
pygame.draw.rect(屏幕,白色,(0,01400700),0)
font=pygame.font.SysFont(“Berlin Sans FB Demi”,100,真,假)
text=font.render(“您丢失了”,真,黑色)
屏幕显示(文本,(500100))
pygame.draw.rect(屏幕,绿色,(600400190,60),0)
font=pygame.font.SysFont(“Aharoni”,50,真,假)
text=font.render(“重置”,真,白色)
屏幕。blit(文本,(630410))
pygame.display.update()
时钟滴答(150)
pygame.quit()

更改太多,无法全部描述

我将所有管道作为
(END,pygame.Rect(195300,80,40))
保存在列表中
所有管道上,然后我可以使用
for
循环来绘制它们

    for pipe_color, pipe_rect in all_pipes:
        pygame.draw.rect(screen, pipe_color, pipe_rect, 0)
并检查与玩家的碰撞

    for pipe_color, pipe_rect in all_pipes:
        if pipe_rect.collidepoint(player_x, player_y):
            state = STATE_GAMEOVER
            break # no need to check other
完整代码

import pygame

# --- constants --- (UPPER_CASE_NAMES)

# - colors -

BACKGROUND_0 = (36, 38, 82)
BACKGROUND_1 = (40, 42, 86)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 47, 47)
GREEN = (79, 255, 101)

WOOD = (253, 197, 136)
PIPE = (152, 228, 86)
END = (137, 226, 57)
GOLD = (219, 178, 58)
GOLDEN = (254, 197, 34)
GOLDER = (255, 206, 63)

# - states -

STATE_INTRO    = 1
STATE_GAME     = 2
STATE_GAMEOVER = 3

# --- classes --- (CamelCaseNames)

#class Wall(pygame.sprite.Sprite):
#
#   def __init__(self, x, y, width, height):
#        super().__init__()
#
#        self.image = pygame.Surface((width, height))
#        self.image.fill(GREY)
#
#        self.rect = self.image.get_rect()
#        self.rect.y = y
#        self.rect.x = x

# --- functions --- (lower_case_names)

# empty

# --- main ---

# - init -

pygame.init()
screen = pygame.display.set_mode((1400,700))

font1 = pygame.font.SysFont("Berlin Sans FB Demi", 100, True, False)
text1 = font1.render("You Lost", True, BLACK)
font2 = pygame.font.SysFont("Aharoni", 50, True, False)
text2 = font2.render("RESET", True, WHITE)

# - objects -

player_y = 200
player_x = 10

x_change = 5
y_change = 0

all_pipes = [
    #upper pipes
    (END, pygame.Rect(195,300,80,40)), 
    (PIPE, pygame.Rect(200,0,70,300)), 
    (PIPE, pygame.Rect(350,0,70,450)), 
    (END, pygame.Rect(345,420,80,40)), 
    (PIPE, pygame.Rect(490,0,70,480)), 
    (END, pygame.Rect(485,480,80,40)), 
    (PIPE, pygame.Rect(630,0,70,450)), 
    (END, pygame.Rect(625,450,80,40)), 
    (PIPE, pygame.Rect(770,0,70,430)), 
    (END, pygame.Rect(765,420,80,40)), 
    (PIPE, pygame.Rect(910,0,70,400)), 
    (END, pygame.Rect(905,400,80,40)), 
    (PIPE, pygame.Rect(1050,0,70,470)), 
    (END, pygame.Rect(1045,470,80,40)), 
    (PIPE, pygame.Rect(1190,0,70,430)), 
    (END, pygame.Rect(1185,430,80,40)), 
    (GOLD, pygame.Rect(1330,0,70,410)), 
    (GOLDER, pygame.Rect(1350,0,70,410)), 
    (GOLDEN, pygame.Rect(1325,410,80,40)), 
    (PIPE, pygame.Rect(200,400,70,240)), 
    #lower pipes
    (END, pygame.Rect(195,400,80,40)), 
    (PIPE, pygame.Rect(350,520,70,500)), 
    (END, pygame.Rect(345,515,80,40)), 
    (PIPE, pygame.Rect(490,570,70,100)), 
    (END, pygame.Rect(485,570,80,40)), 
    (PIPE, pygame.Rect(630,570,70,100)), 
    (END, pygame.Rect(625,540,80,40)), 
    (PIPE, pygame.Rect(770,550,70,120)), 
    (END, pygame.Rect(765,510,80,40)), 
    (PIPE, pygame.Rect(910,530,70,220)), 
    (END, pygame.Rect(905,490,80,40)), 
    (PIPE, pygame.Rect(1050,560,70,220)), 
    (END, pygame.Rect(1045,560,80,40)), 
    (PIPE, pygame.Rect(1190,530,70,220)), 
    (END, pygame.Rect(1185,530,80,40)), 
    (GOLD, pygame.Rect(1330,530,70,220)), 
    (GOLDER, pygame.Rect(1350,530,70,220)), 
    (GOLDEN, pygame.Rect(1325,510,80,40)), 
    (WOOD, pygame.Rect(0,650,1400,50)), 
    (GREEN, pygame.Rect(0,640,1400,10)), 
]

# - mainloop -

state = STATE_INTRO # STATE_GAME, STATE_GAMEOVER

clock = pygame.time.Clock()
done = False

while not done:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if state == STATE_INTRO:
            if event.type == pygame.KEYDOWN:
               state = STATE_GAME

        elif state == STATE_GAME:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    y_change = -5
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    y_change = 5

        elif state == STATE_GAMEOVER:
            if event.type == pygame.KEYDOWN:
               state = STATE_INTRO
               player_y = 200
               player_x = 10

    # - updates (without draws) -

    if state == STATE_GAME:
        player_x += x_change
        player_y += y_change

        # check collisions with all pipes

        for pipe_color, pipe_rect in all_pipes:
            if pipe_rect.collidepoint(player_x, player_y):
                state = STATE_GAMEOVER
                break # no need to check other

    # - draws (without updates) -

    if state in (STATE_INTRO, STATE_GAME):
        screen.fill(BACKGROUND_0)
        pygame.draw.rect(screen, BACKGROUND_1, (0, 350, 1400, 350), 0)

        # draw all pipes

        for pipe_color, pipe_rect in all_pipes:
            pygame.draw.rect(screen, pipe_color, pipe_rect, 0)

        pygame.draw.circle(screen, WHITE, (player_x, player_y), 5)

    if state == STATE_GAMEOVER:
        screen.fill(WHITE)
        screen.blit(text1, (500, 100))
        pygame.draw.rect(screen, GREEN, (600, 400, 190, 60), 0)
        screen.blit(text2, (630, 410))

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

# - end -

pygame.quit()

“检查我的代码并将其修复”并不是那么回事。问一个特定的问题,有人会帮你调试。使用
print()
在变量和消息中显示值,显示执行代码的哪一部分,称为“打印调试”,这有助于查看代码中发生了什么。有时代码并没有达到预期效果,或者变量中有不同的值。您可以使用列表或字典来保持所有管道的位置和大小,然后您可以使用
for
循环来绘制它们并检查冲突。Python甚至必须保持位置和大小—它有方法
colliderect
collidepoint
,等等。目前我不知道在哪里检查冲突—所以它们不起作用。什么意思是
a
b
,等等。使用有意义的名称。现在代码不可读。如果在代码中未使用
Wall
,请将其删除-不要保留未使用的元素。它使代码无法阅读。感谢您修复了我的问题和代码。您不知道这对我有何帮助。顺便说一句:Stackoverflow有门户,大多数更改都应该在此门户上完成。Stackoverflow也有门户