第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

即使 x 和 y 值正在更新,pygame 也不會(huì)通過(guò)按鍵更新draw.rect

即使 x 和 y 值正在更新,pygame 也不會(huì)通過(guò)按鍵更新draw.rect

慕妹3242003 2023-07-18 15:08:13
我對(duì)編程相當(dāng)陌生,我正在構(gòu)建 Pong 只是一個(gè)初學(xué)者項(xiàng)目。我編寫了以下代碼來(lái)嘗試移動(dòng)兩個(gè)撥片,但是當(dāng)按下向上/向下/w/s 鍵時(shí),它們都不會(huì)移動(dòng)。然而,在嘗試自己調(diào)試時(shí),我將其設(shè)置為將 x 和 y 值打印到控制臺(tái),以查看這是否是問(wèn)題所在,并且 x 和 y 值正在更新,但我的矩形的坐標(biāo)沒(méi)有更新。我確信這是一個(gè)相當(dāng)簡(jiǎn)單的修復(fù),但我對(duì)編程很陌生,只是不知道出了什么問(wèn)題。這是我的代碼:import timepygame.init()screenW = 800screenH = 600win = pygame.display.set_mode((screenW, screenH))pygame.display.set_caption("Pong")clock = pygame.time.Clock()font = pygame.font.SysFont("comicsans", 20)paddleW = 20paddleH = 100ballSpeed = 15class paddle(object):    def __init__(self, x, y, width, height):        self.x = x        self.y = y        self.width = width        self.height = height        self.pad = (self.x, self.y, self.width, self.height)    def draw(self, win):        pygame.draw.rect(win, (255,255,255), self.pad)class ball(object):    def __init__(self, x, y, side):        self.x = x        self.y = y        self.side = side        self.pad = (self.x, self.y, self.side, self.side)    def draw(self, win):        pygame.draw.rect(win, (255,255,255), self.pad)def redrawGameWindow():    win.fill((0,0,0))    paddleLeft.draw(win)    paddleRight.draw(win)    theBall.draw(win)    pygame.display.update()#Main Loop:paddleLeft = paddle(10, screenH//2, paddleW, paddleH)paddleRight = paddle(screenW - 10 - paddleW, screenH//2, paddleW, paddleH)theBall = ball(screenW//2, screenH//2, 30)run = Truewhile run:    clock.tick(30)        for event in pygame.event.get():        if event.type == pygame.QUIT:            run = False    keys = pygame.key.get_pressed()    if keys[pygame.K_UP]:        print("left up")        print(paddleRight.y)        paddleRight.y -= ballSpeed    elif keys[pygame.K_DOWN]:        print("left down")        print(paddleRight.y)        paddleRight.y += ballSpeed    elif keys[pygame.K_w]:        print("right up")        print(paddleLeft.y)        paddleLeft.y -= ballSpeed    elif keys[pygame.K_s]:        print("right down")        print(paddleLeft.y)        paddleLeft.y += ballSpeed    redrawGameWindow()pygame.quit()
查看完整描述

1 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

當(dāng)您更改屬性x和時(shí)y,列表的內(nèi)容pad不會(huì)神奇地改變。


在繪圖之前,您必須更新.pad。例如:


class paddle(object):

    # [...]


    def draw(self, win):

        self.pad = (self.x, self.y, self.width, self.height)

        pygame.draw.rect(win, (255,255,255), self.pad)

無(wú)論如何,我建議使用對(duì)象而pygame.Rect不是屬性x、y和width:height


class paddle(object):

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

        self.rect = pygame.Rect(x, y, width, height)


    def draw(self, win):

        pygame.draw.rect(win, (255,255,255), self.rect)


class ball(object):

    def __init__(self, x, y, side):

        self.rect = pygame.Rect(x, y, side, side)


    def draw(self, win):

        pygame.draw.rect(win, (255,255,255), self.rect)

完整示例:


import pygame

import time

pygame.init()

screenW = 800

screenH = 600

win = pygame.display.set_mode((screenW, screenH))

pygame.display.set_caption("Pong")

clock = pygame.time.Clock()

font = pygame.font.SysFont("comicsans", 20)

paddleW = 20

paddleH = 100

ballSpeed = 15


class paddle(object):

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

        self.rect = pygame.Rect(x, y, width, height)


    def draw(self, win):

        pygame.draw.rect(win, (255,255,255), self.rect)


class ball(object):

    def __init__(self, x, y, side):

        self.rect = pygame.Rect(x, y, side, side)


    def draw(self, win):

        pygame.draw.rect(win, (255,255,255), self.rect)


def redrawGameWindow():

    win.fill((0,0,0))

    paddleLeft.draw(win)

    paddleRight.draw(win)

    theBall.draw(win)

    pygame.display.update()



#Main Loop:

paddleLeft = paddle(10, screenH//2, paddleW, paddleH)

paddleRight = paddle(screenW - 10 - paddleW, screenH//2, paddleW, paddleH)

theBall = ball(screenW//2, screenH//2, 30)

run = True

while run:

    clock.tick(30)

    

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            run = False


    keys = pygame.key.get_pressed()


    if keys[pygame.K_UP]:

        print("left up")

        print(paddleRight.rect.y)

        paddleRight.rect.y -= ballSpeed


    elif keys[pygame.K_DOWN]:

        print("left down")

        print(paddleRight.rect.y)

        paddleRight.rect.y += ballSpeed


    elif keys[pygame.K_w]:

        print("right up")

        print(paddleLeft.rect.y)

        paddleLeft.rect.y -= ballSpeed


    elif keys[pygame.K_s]:

        print("right down")

        print(paddleLeft.rect.y)

        paddleLeft.rect.y += ballSpeed


    redrawGameWindow()



pygame.quit()


查看完整回答
反對(duì) 回復(fù) 2023-07-18
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)