python - 尝试按照教程制作一个超级简单的 pygame 游戏,但它不起作用,据我所知,我们的代码看起来相同
问题描述
我正在尝试通过 pygame 制作一个简单的小游戏,我一直在关注一个教程,出于某种原因,在这个阶段它应该只是打开一个黑色窗口,但无论出于何种原因,它不在这里是我的代码
import pygame
import sys
import math
import random
pygame.init()
pygame.display.set_caption("Squid Slayer")
clock = pygame.time.Clock()
WIDTH =1200
HEIGHT =800
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
#make the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
#Making classes
class Sprite ():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.dx = 0
self.dy = 0
self.width = width
self.height = height
self.color = WHITE
self.friction = 0.8
def goto(self):
self.x = x
self.y = y
def render(self):
pygame.draw.rect(screen, self.color, pygame.Rect(int(self.x-self.width/2.0),int(self.y-self.height/2.0), self.width, self.height))
def is_aabb_collision(self, other):
# Axis Sligned Bounding Box
x_collision = (math.fabs(self.x-other.x)*2)<(self.width+other.width)
y_collision = (math.fabs(self.y-other.y)*2)<(self.height+other.height)
return (x_collision and y_collision)
class Player(Sprite):
def __init__(self, x, y, width, height):
Sprite.__init__(self, x, y, width, height)
# Create font
# Create sounds
# Create game objects
player = Player(600, 0, 20, 40)
#Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Keyboard events
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pass
elif event.key == pygame.K_RIGHT:
pass
elif event.key == pygame.K_SPACE:
pass
# Move/Update objects
# Check for collisions
# Border check the player
# Render (Draw stuff)
player.render()
#fill the background color
screen.fill(BLACK)
#Render objects
#Flip the display
pygame.display.flip()
#Set the FPS
clock.tick(30)
这是教程的链接,非常感谢任何建议: https ://www.youtube.com/watch?v=3ri4f7y8uJI
解决方案
在我看来,while True:
与原始代码相比,循环的缩进是一个额外的标签。
推荐阅读
- javascript - JS/jQuery 冲突
- r - 使用Dplyr从长向宽传播;更优雅的解决方案?
- jquery - 在任何位置使用 jquery 添加选项时如何添加“选定”属性
- c# - 在 WindowsForms C# 中使用 SaveFileDialog 和 StreamWriter/Reader 时出错
- python - 使用 Tkinter 时,错误:TclError: image "pyimage8" doesn't exist
- excel - 有条件地从上面的单元格中填充空白行
- python - python urllib.error.HTTPError
- angularjs - 如何用多个资源实例化角度服务?
- r - ggplot2:当类别低于零时,堆积条形标签显示在不正确的位置和顺序
- qt - 标题栏背景qml