[英]Pygame not drawing rectangles
I am new to pygame trying to create a flappy bird.我是 pygame 的新手,试图创造一只飞扬的鸟。 I have written the following code so far.
到目前为止,我已经编写了以下代码。 https://gist.github.com/Devansha2007/fa0e5554547e6b1395e3bf3783a4c53d
https://gist.github.com/Devansha2007/fa0e5554547e6b1395e3bf3783a4c53d
import pygame
import random
pygame.init()
surface = pygame.display.set_mode((320, 568))
bg = pygame.image.load('bg.png')
bird = pygame.image.load('player.png')
pole_width = 70
pole_gap = 100
pole_x = 320
top_pole_height = random.randint(100, 400)
pole_color = (220, 85, 57)
bird_x = 0
bird_y = 0
score = 0
clock = pygame.time.Clock()
while True:
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
bird_y = bird_y + 6
elif keys[pygame.K_UP]:
bird_y = bird_y - 6
surface.blit(bg, (0, 0))
surface.blit(bird, (bird_x, bird_y))
pygame.draw.rect(surface, pole_color, pygame.Rect(pole_x, 0, pole_width, top_pole_height))
# pole_x = pole_x - 500
pygame.display.flip()
pygame.display.update()
clock.tick(60)
The rectangle is out of bounds, because of pole_x = pole_x - 500
and the initial x-coordinate.由于
pole_x = pole_x - 500
和初始x 坐标,矩形超出范围。 Remove pole_x = pole_x - 500
and change the intilial x-coordinate:移除
pole_x = pole_x - 500
并改变初始x坐标:
pole_x = 320 - pole_width
while True:
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
bird_y = bird_y + 6
elif keys[pygame.K_UP]:
bird_y = bird_y - 3
surface.blit(bg, (0, 0))
surface.blit(bird, (bird_x, bird_y))
# pole_x = pole_x - 500 <-- remove or change this
pygame.draw.rect(surface, pole_color, pygame.Rect(pole_x, 0, pole_width, top_pole_height))
pygame.display.flip()
pygame.display.update()
clock.tick(60)
If you want to move the rectangle slowly and smoothly, change the movement:如果要缓慢平稳地移动矩形,请更改移动方式:
pole_x = pole_x - 500
pole_x -= 1
Applicaition loop:应用循环:
while True:
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
bird_y = bird_y + 6
elif keys[pygame.K_UP]:
bird_y = bird_y - 3
surface.blit(bg, (0, 0))
surface.blit(bird, (bird_x, bird_y))
pole_x -= 1
pygame.draw.rect(surface, pole_color, pygame.Rect(pole_x, 0, pole_width, top_pole_height))
pygame.display.flip()
pygame.display.update()
clock.tick(60)`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.