python运行老是无响应_PythonPygame总是在空闲状态下运行模块时崩溃并且没有响应...

这篇博客记录了一位Python和Pygame新手在尝试制作游戏时遇到的问题。代码显示了一个使用Pygame创建窗口并加载图像的简单程序,但窗口没有响应。作者请求帮助找出代码中的错误,并提供了完整的代码示例,包括角色类和事件处理部分。
摘要由CSDN通过智能技术生成

我对Pygame甚至Python都很陌生,但我知道当中的某个错误出现时,它会在pythonshell中显示一些文本,告诉您有一些错误。我实际上遇到过很多这样的问题,这次,它最终运行并显示窗口,但它没有响应。我知道我的整个代码可能有一些错误,所以请随时纠正我(请,请解释一下,因为我对这些东西还是新手)。在

代码如下,但如果它能帮助,如果你要求它,我会看看我是否可以张贴文件。总之,代码如下:#import Modules

import os, sys

import pygame

from pygame.locals import *

background_img="C:/Users/JM/Documents/Python/Pygame_Alpha/background_img.jpg"

cursor_img="C:/Users/JM/Documents/Python/Pygame_Alpha/pygameCursor.png"

def load_image(img_file, colorkey=None):

file_pathname = os.path.join("\Users\JM\Documents\Python\Pygame_Alpha",img_file)

try:

image = pygame.image.load(file_pathname).convert_alpha()

except pygame.error, message:

print "Can't load image:", file_pathname

raise SystemExit, message

image = image.convert()

if colorkey is not None:

if colorkey is -1:

colorkey = image.get_at((0,0))

image.set_colorkey(colorkey, RLEACCEL)

return image, image.get_rect()

#Main character's position and movements

char_x,char_y = 0,0

char_go_x,char_go_y = 0,0

#Main char class

class char(pygame.sprite.Sprite):

"""Main Character"""

def __init__(self):

pygame.sprite.Sprite.__init__(self)#call Sprite initializer

self.image, self.rect = load_image("char_img.png", -1)

self.jumping = 0

def update(self):

self.rect.midtop = char_x,char_y

if self.jumping == 1:

self.rect.move_ip(-35,-3)

def char_no_jump(self):

self.jumping = 0

pygame.init()

pygame.display.set_caption("pygame_Alpha")

screen = pygame.display.set_mode((800,480),0,32)

background = pygame.image.load(background_img).convert()

cursor = pygame.image.load(cursor_img).convert_alpha()

char = char()

clock = pygame.time.Clock()

millisec = clock.tick()

sec = millisec/1000.0

char_fall = sec*25

jump = sec*50

#blit the background

screen.blit(background,(0,0))

#Main Loop

while 1:

#Tell pygame not to exceed 60 FPS

clock.tick(60)

#Events

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

#Events triggered when a key/s is/are pressed

if event.type == KEYDOWN:

if event.key == K_ESCAPE:

pygame.quit()

sys.exit()

elif event.key == K_UP or event.key == K_w:

char.jumping = 1

elif event.key == K_DOWN or event.key == K_s:

char_go_y += 1

elif event.key == K_LEFT or event.key == K_a:

char_go_x -= 0.5

elif event.key == K_RIGHT or event.key == K_d:

char_go_x += 0.75

if char_x > 800:

char_x = 0

#Events triggered when a key/s is/are released

if event.type == KEYUP:

if event.key == K_UP or event.key == K_w:

char_go_y += 1

elif event.key == K_DOWN or event.key == K_s:

char_go_y = 0

elif event.key == K_LEFT or event.key == K_a:

char_go_x = 0

if char_x < 0:

char_x = 0

elif event.key == K_RIGHT or event.key == K_d:

char_go_x = 0

if char_x > 700:

char_x = 0

char.update()

while char_y < 200:

char_go_y += char_fall

if char_y > 200:

char_y = 200

#Update values of position of Main Char

char_x += char_go_x

char_y += char_go_y

#Position Variables of Cursor Image, setting its values equal to cursor pos, and blit it to screen

cursor_x,cursor_y = pygame.mouse.get_pos()

cursor_x -= cursor.get_width()/2

cursor_y -= cursor.get_height()/2

screen.blit(cursor,(cursor_x,cursor_y))

pygame.display.update()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值