簡體   English   中英

Python 新手嘗試使用 pygame 制作游戲

[英]New to Python trying to make game with pygame

我使用 PyGame 為我的游戲編寫了一些代碼來產生障礙,並且我是編碼新手。 它不會產生障礙,所以我放了一個打印語句來查看它何時產生並且它確實打印但沒有顯示在屏幕上。 這不是全部代碼,而是它的一個簡約版本,僅顯示有問題的代碼。 運行代碼時沒有出現錯誤

import pygame as pg
import random
pg.init()
obstacle1 = pg.image.load('download1.jpeg')
obstacley = 600#tells object to spawn at top of screen
spawn = random.randint(1,10)
spawned = 0
if spawn == 1 and spawned == 0:
    spawn = 0
    Obstaclex = random.randint(600,800)#determines where on the top of the screen it spawns with rng
    obstaclesize = random.randint(1,5)# determines obstacletype because there are 5 obstacle types that i havent included in this to be as simple as possbile
    obstaclespeed = random.randint(3,8)#determines obstaclespeed using rng
    spawned = 1
    if obstaclesize == 1:       
        gameDisplay.blit(obstacle1,(obstacley,Obstaclex))
        obstacley -= obstaclespeed #changes y position to lower it down the screen to hit player
        print ("i have spawned")

你必須在位塊傳輸幀過游戲中的循環中的所有現有的障礙,創造了新的障礙不只是當。

創建一個障礙物列表 ( obstacle_list ) 並將新障礙物的坐標附加到列表中。 繪制主應用循環中的所有障礙物:

obstacle_list = []

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # [...]

    if len(obstacle_list) < spawn:
        x = random.randint(600,800)
        y = 600
        size = random.randint(1,5)
        speed = random.randint(3,8)
        obstacle_list.append((x, y, size, speed))

    # [...]

    # move obstacles
    for i in range(len(obstacle_list)):
        x, y, size, speed = obstacle_list[i]
        new_y = y - speed
        obstacle_list[i] = (x, new_y, size, speed)

    # [...]

    # draw obstacles
    for x, y, size, speed in obstacle_list:
        gameDisplay.blit(obstacle1, (x, y))

    # [...]

我使用 PyGame 為我的游戲編寫了一些代碼來產生障礙,並且我是編碼新手。 它不會產生障礙,所以我放了一個打印語句來查看它何時產生並且它確實打印但沒有顯示在屏幕上。 這不是全部代碼,而是它的一個簡約版本,僅顯示有問題的代碼。 運行代碼時沒有出現錯誤

import pygame as pg
import random
pg.init()
obstacle1 = pg.image.load('download1.jpeg')
obstacley = 600#tells object to spawn at top of screen
spawn = random.randint(1,10)
spawned = 0
if spawn == 1 and spawned == 0:
    spawn = 0
    Obstaclex = random.randint(600,800)#determines where on the top of the screen it spawns with rng
    obstaclesize = random.randint(1,5)# determines obstacletype because there are 5 obstacle types that i havent included in this to be as simple as possbile
    obstaclespeed = random.randint(3,8)#determines obstaclespeed using rng
    spawned = 1
    if obstaclesize == 1:       
        gameDisplay.blit(obstacle1,(obstacley,Obstaclex))
        obstacley -= obstaclespeed #changes y position to lower it down the screen to hit player
        print ("i have spawned")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM