精灵在我的平台上走来走去,被卡住了(pygame)

2024-05-16 09:30:50 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试着跳到一个平台上时,我会穿过它。我什么都试过了,我的计划星期一到期。这也是我的第一个stackoverflow问题,我最近刚刚开始学习pygame和python。提前谢谢

import pygame
import sys
import time
pygame.init()
screenSize = (800,600)
displayScreen = pygame.display.set_mode((screenSize),0)
pygame.display.set_caption("U1A2 Test")


WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)

displayScreen.fill(WHITE)
pygame.display.update()
clock = pygame.time.Clock()
fps= 60 
game = False
menu = True
inst = False
jump= False
x = 53
y = 552
m = 0
n = 0
dx = 0
dy = 0
sprite= pygame.image.load('block sprite.png')
#PlayerRect = pygame.rect(x,y,sprite.get_width(), sprite.get_height())
walls = []
level1 = [

    "WWWWWWWWWWWWWWWWWWWWWWWWWW",
    "                         ",
    "                         ",
    "                         ",
    "                         ",
    "                         ",
    "  WW                     ",    
    "                         ",
    "                         ",    
    "     WW                  ",
    "          WWWW           ",
    "                         ",
    "                WWW      ",
    "                         ",
    "                         ",
    "         WWW             ",
    "   WWW                   ",
    "                         ",
    "WWWWWWWWWWWWWWWWWWWWWWWWWW",
    ]



for row in level1:
    for col in row:
        if col == " ":
            jump = False 
        if col == "W":
            wallRect = pygame.Rect(m,n,32,32)
            walls.append(wallRect)
            jump = True
        m += 32
    n += 32
    m = 0

while menu:


        fontTitle = pygame.font.SysFont("Ariel", 72)
        textTitle = fontTitle.render("change this", True, (255,0,0))
        displayScreen.blit(textTitle,(35, 50))

        fontTitle = pygame.font.SysFont("Ariel", 52)
        textTitle = fontTitle.render("Press 1 to play", True, (255,0,0))
        displayScreen.blit(textTitle,(35, 100))

        fontTitle = pygame.font.SysFont("Ariel", 52)
        textTitle = fontTitle.render("Press 2 for instructions", True, (255,0,0))
        displayScreen.blit(textTitle,(35, 150))
        pygame.display.update()

        fontTitle = pygame.font.SysFont("Ariel", 52)
        textTitle = fontTitle.render("Press 3 to quit", True, (255,0,0))
        displayScreen.blit(textTitle,(35, 200))
        pygame.display.update()


        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    print "ds"
                    game = True

                elif event.key == pygame.K_2:
                    menu = False
                    inst = True
                    displayScreen.fill(WHITE)
                elif event.key == pygame.K_3:
                    menu = False

        while game:


            #pygame.draw.rect(displayScreen,GREEN,PlayerRect,0)
            PlayerRect = pygame.Rect(x,y, sprite.get_width(), sprite.get_height())
            displayScreen.blit(sprite,(x,y))
            pygame.display.flip()

            #pygame.draw.rect(displayScreen, (255, 0, 0), PlayerRect, 1)


            clock.tick(fps)                                 
            for event in pygame.event.get():
                if event.type ==pygame.QUIT:
                    pygame.display.quit()
                    sys.exit()

                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_a:
                        dx = -7

                        displayScreen.fill(WHITE)


                    elif event.key == pygame.K_d:
                        dx = 7
                        displayScreen.fill(WHITE)


                    elif event.key == pygame.K_w:    
                        dy = -25



                    elif event.key == pygame.K_s:
                        dy = 25
                        displayScreen.fill(WHITE)


                    elif event.key == pygame.K_q:
                        stop = True
                        displayScreen.fill(WHITE)

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_a or event.key == pygame.K_d:
                            dx = 0
                    elif event.key == pygame.K_w or event.key == pygame.K_s:    
                            dy = 0            

            oldx = x
            oldy = y                           
            x = x + dx
            y = y + dy

            if game == True:
                dy = dy + 2

            if (x>=795 or x<=33):
                x=oldx


            if (y<=5 or y>=550):
                y=oldy 
            displayScreen.fill(WHITE)

            for wall in walls:
                if PlayerRect.colliderect(wall):
                    if dx > 0:
                        PlayerRect.left = wall.right

                    if dx < 0:
                        PlayerRect.right = wall.left

                    if dy > 0:
                        PlayerRect.bottom = wall.top

                    if dy < 0:
                        PlayerRect.top = wall.bottom

                    dy = 0




            for wall in walls:
                pygame.draw.rect(displayScreen,BLUE,wall,0)
                #pygame.draw.rect(displayScreen, (255, 0, 0), wall, 1)

            time.sleep(0.01)
            pygame.display.update()


        while inst:
                fontTitle = pygame.font.SysFont("Ariel", 72)
                textTitle = fontTitle.render("Instructions", True, (255,0,0))
                displayScreen.blit(textTitle,(35, 50))

                fontTitle = pygame.font.SysFont("Ariel", 52)
                textTitle = fontTitle.render("- Use W to jump,S to fall", True, (255,0,0))
                displayScreen.blit(textTitle,(35, 100))

                fontTitle = pygame.font.SysFont("Ariel", 52)
                textTitle = fontTitle.render("- Control your jump by using the A and D keys", True, (255,0,0))
                displayScreen.blit(textTitle,(35,150))
                pygame.display.update()

                fontTitle = pygame.font.SysFont("Ariel", 52)
                textTitle = fontTitle.render("- Move left and right with the A and D keys", True, (255,0,0))
                displayScreen.blit(textTitle,(35, 200))
                pygame.display.update()

                fontTitle = pygame.font.SysFont("Ariel", 52)
                textTitle = fontTitle.render("Press 3 to exit", True, (255,0,0))
                displayScreen.blit(textTitle,(35, 250))
                pygame.display.update()

                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_3:
                            inst = False
                            displayScreen.fill(WHITE)
                            menu = True

pygame.quit()
sys.exit()

Tags: keyeventtrueifdisplaypygamewhitefont
1条回答
网友
1楼 · 发布于 2024-05-16 09:30:50

冲突解决代码的工作方式与预期不符,因为它修改了PlayerRect,而不是xyPlayerRect在通过主循环的每次传递的开始处被重新创建,因此来自冲突的更改不会持续。您需要修改冲突代码中的xy,或者更改PlayerRect的设置和使用方式,以便在帧之间保持更改。你知道吗

相关问题 更多 >